Using writeHead to redirect in `getServerSideProps` fails on Safari
See original GitHub issueBug 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:
- Created 3 years ago
- Reactions:1
- Comments:17 (9 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
i can confirm this issue on Safari and Edge as well. server-side redirections triggers the error page.
One workaround is to replace
Link
witha
but this downgrades experience for all browsersHappened to me too, here is the console error log