Next.js: Browser does not set preview mode cookies even though they are present in the response headers. (Only in production mode!)

Created on 7 Aug 2020  路  1Comment  路  Source: vercel/next.js

Bug report

Describe the bug

I have a preview.js file in my api/pages folder which calls

res.setPreviewData({}); res.end();

This works in development (starting the server with the "next" command and open up 'api/preview' in a browser), I get a Set-Cookie header from the server, and the browser successfully stores the cookie and context.preview is true in getStaticProps().

However, when I make a production build (next build && next start), the browser will not save the cookie. My api/preview route still returns a Set-Cookie header, though.

Am I misunderstanding how this is supposed to work? This could seem to have nothing to do with Next.js, considering that the headers are set correctly. Any ideas on what might cause this?

To Reproduce

  1. Clone https://github.com/mskutle/next-preview-bug-reproduction
  2. npm install
  3. npm run build && npm start
  4. go to http://localhost:3000/api/preview
  5. Inspect cookies in browser

Expected behavior

I expected the cookies to be set exactly like in development mode.

Screenshots

If applicable, add screenshots to help explain your problem.

System information

  • OS: macOS Catalina 10.15.6
  • Browser: Happens in Chrome, Safari & Edge (haven't tried other browsers, assuming the same behavior)
  • Version of Next.js: 9.5.1
  • Version of Node.js: 12.18.1
bug

Most helpful comment

Production mode requires your application be served from a secure connection! Otherwise, the cookies will not be set. Please try this behind a HTTPS proxy or on Vercel!

>All comments

Production mode requires your application be served from a secure connection! Otherwise, the cookies will not be set. Please try this behind a HTTPS proxy or on Vercel!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

havefive picture havefive  路  3Comments

YarivGilad picture YarivGilad  路  3Comments

renatorib picture renatorib  路  3Comments

kenji4569 picture kenji4569  路  3Comments

swrdfish picture swrdfish  路  3Comments