When the ServerResponse instance (reply.raw) is used to respond to the client after setting a cookie, the cookie doesn't appear in the browser.
Reproducible example: https://codesandbox.io/s/adoring-solomon-n61yz?file=/src/build.js:589-592
/cookie-2But if you go to /cookie-1 instead, the cookie will appear. ✅
// expected
app.get('/cookie-1', (req, reply) => {
reply
.setCookie('session', 'value', { secure: false })
.header('Content-Type', 'text/plain')
.send('ok')
})
// unexpected
app.get('/cookie-2', (req, reply) => {
reply.setCookie('session', 'value', { secure: false })
reply.raw.writeHead(200, { 'Content-Type': 'text/plain' })
reply.raw.write('ok')
reply.raw.end()
})
In both cases, the client should have the cookie saved on the server-side.
It works as planned. Cookies as stored inside fastify own Reply object. If you use reply.raw, you are sidestepping all of fastify.
This likely needs some docs.
Hey @mcollina, thanks for the answer. Let me know if I could contribute to clarify the docs ✋
Go for it! Contributions are always welcome!
Most helpful comment
Hey @mcollina, thanks for the answer. Let me know if I could contribute to clarify the docs ✋