-
Notifications
You must be signed in to change notification settings - Fork 27.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support redirects in getServerSideProps #13470
Conversation
I’d say definitely yes, since the response code (301 or 302) makes a lot of difference for SEO. We use both in our application currently, depending on semantics of the redirect. |
This should probably be a RFC first given that it's unclear what the correct behavior is. It's been on my to-do list. Here's some of the requirements:
|
@timneutkens You're right; it should've been an RFC first. Sorry about that. I think I'll have to let you write it. I'm not very familiar with |
@justincy @timneutkens I would say we could have more of an HTTP looking object like so: {
props: { },
status: number,
headers: { }
} And Next could just check for the status and the headers.Location to know if it has to redirect or not. |
This would only work for getServerSideProps so that would not be an ideal api. Features have to work for both SSG and SSR, otherwise people deoptimize their application where it isn't needed. |
It would seem the solution would have to...
You need redirection logic in both server side and client side code to handle (elegantly) all of the cases above with a single approach. @timneutkens what do you think? |
I wrote the RFC for this: #14890 |
Ah thanks, yes I had already read that. Since 9.5 is now out, do you think it will get some attention? Just trying to assess timing as we might have to hack out an alternative in the meantime. Thanks Tim. |
This PR recommends adding support for redirects in
getServerSideProps
, as requested, like this:There is an existing pattern for redirects but it has some problems. SSR requests work as expected because the browser is requesting HTML, follows the redirect, and receives HTML. But data requests fail because the browser is requesting JSON but receives HTML. This causes an error which is sometimes caught by Next which triggers a full page reload, giving the appearance of it working properly, but this fails in Safari.
The pattern proposed here avoids the problems stated above with data requests while maintaining the same behavior for SSR requests. A similar pattern has been proposed for custom status codes and error handling.
I don't know whether this is the best way to solve the problem. I'm sure you all have some idea of how to expand the functionality of
getServerSideProps
. I look forward to the discussion.If this seems like an acceptable solution, there are some details that need to be addressed:
redirect
andprops
to be used at the same time?redirect
to support bothas
andhref
?