Support server side redirects
See original GitHub issue🚀 Feature Proposal
We are using SSR with “authenticated” middleware to protect authenticated routes.
This middleware will check if user is authenticated and if not redirect him to another page, e.g. /login
The logic inside getServerSideProps might look something like this:
export const getServerSideProps: GetServerSideProps<Props> = async (
context
) => {
if (someSpecialCase) {
context.res.writeHead(302, { Location: "/login" });
context.res.end();
return undefined;
}
}
This means that the I would expect /login page to be returned from the getPage function.
Issue Analytics
- State:
- Created 3 years ago
- Comments:9
Top Results From Across the Web
SVR1: Implementing automatic redirects on the server side ...
Server -side technologies provide methods to implement redirects in a way that does not confuse users. A server-side script or configuration file can...
Read more >Best practices for client side vs. server side redirects
Redirects are to be initiated from the server side with a 301/302 response. All webapp languages/frameworks defaults to 302. You can usually ...
Read more >Redirects & SEO - The Complete Guide - Audisto
A server side redirect happens, when the HTTP request to a web server is answered with a server status code, which indicates the...
Read more >Client-Side and Server-Side Redirects in Next.js
js supports both client-side and server-side rendering (SSR) and unfortunately the method for redirecting is very different in both contexts.
Read more >Redirections in HTTP - MDN Web Docs - Mozilla
In HTTP, redirection is triggered by a server sending a special redirect response to a request. Redirect responses have status codes that ...
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 Free
Top 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

Yeah, its very high level and abstract, but I think this is the experience most people would expect and would probably suit most of the use cases. I like the fact that there is no implementation details leaked, you don’t even have to know its NextJS underneath.
In theory, library could have multiple “levels” of abstraction and if more fine grained control would be needed some lower level API could be exposed (if there was some use cases for it).
Just thinking out loud: there could be some lower level API exposed that could return more stuff, e.g. page itself + “props”, “redirect”… and it implementation would be used internally by the “higher” level.
Totally agree. That is much easier to implement and a way forward 👍
In that case I would just mention somewhere once it is implemented that only those SSR redirects are supported and should be preferred anyway.