question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using writeHead to redirect in `getServerSideProps` fails on Safari

See original GitHub issue

Bug report

Describe the bug

Using a Next Link component to route to a page where there is a check to see if a cookie is defined, and if so to redirect the user seems to fail on Safari.

// login.js

export async function getServerSideProps(ctx) {
  // Nookies parse cookies function to get the current cookies
  const cookies = parseCookies(ctx);

  if (cookies.token) {
    ctx.res.writeHead(303, { Location: "/admin" });
    ctx.res.end();
  }

  return { props: {} };
}

The Link component points to /login and is in the global navigation.

It seems that clicking on this on Safari changes the current path to /login however nothing happens.

On Chrome it seems that the page is properly redirected to /admin should the token cookie be defined.

Expected behavior

res.writeHead() behaves the same on Safari as it does on Chrome.

System information

  • OS: macOS Catalina 10.15.4 (19E287)
  • Browser (if applies) Safari Version 13.1 (15609.1.20.111.8)
  • Version of Next.js: 9.3.5
  • Version of Node.js: v14.0.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:17 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
revolunetcommented, Jun 18, 2020

i can confirm this issue on Safari and Edge as well. server-side redirections triggers the error page.

One workaround is to replace Link with a but this downgrades experience for all browsers

3reactions
hmtri1011commented, May 6, 2020

image

Happened to me too, here is the console error log

Read more comments on GitHub >

github_iconTop Results From Across the Web

reactjs - Next.js getServerSideProps redirection ...
If I try to go /private , it redirects to /login . Browser is OK, but my console said, Error [ERR_HTTP_HEADERS_SENT]: Cannot set...
Read more >
How do you deal with public and private routes in a NextJS app?
export async function getServerSideProps(context) { const sendRedirectLocation = (location) => { res.writeHead(302, { Location: location, }); res.end(); ...
Read more >
How to Redirect a Web Page with Node.js - W3docs
Learn How to Redirect a Web Page with Node.js? The first step is to include the HTTP module and create a new server,...
Read more >
Bun: Incredibly Fast JavaScript Runtime, Bundler, Transpiler
The runtime uses JavaScriptCore, the JavaScript engine powering WebKit and Safari. Some web APIs like Headers and URL directly use Safari's implementation.
Read more >
98bfabef3e24c995790e2c819d6...
"body": "When using `!` in a `.env` file it can cause webpack to fail to build ... "title": "Possible bug: 308 redirects for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found