Fastify: Unexpected cookies behaviour

Created on 5 Jul 2020  ·  3Comments  ·  Source: fastify/fastify

🐛 Bug Report

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.

To Reproduce

Reproducible example: https://codesandbox.io/s/adoring-solomon-n61yz?file=/src/build.js:589-592

  1. Go to the provided server on CodeSanbox: https://n61yz.sse.codesandbox.io/
  2. Open the dev tools and enter the cookies viewer in the application tab.
  3. Go to /cookie-2
  4. There is no cookie ❌

But 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()
})

Expected behavior

In both cases, the client should have the cookie saved on the server-side.

Your Environment

  • node version: 10.20.1
  • fastify version: 3.0.0-rc.5
  • os: Windows
documentation good first issue

Most helpful comment

Hey @mcollina, thanks for the answer. Let me know if I could contribute to clarify the docs ✋

All 3 comments

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!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lgertel picture lgertel  ·  3Comments

bhanuc picture bhanuc  ·  4Comments

jsumners picture jsumners  ·  4Comments

valentinvichnal picture valentinvichnal  ·  3Comments

avilang picture avilang  ·  3Comments