-
Notifications
You must be signed in to change notification settings - Fork 28.3k
New router behavior around middleware breaks redirects from getInitialProps on CSR #39126
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
Comments
This also occurs with |
This issue still persists in latest canary v13. It still prevents us from upgrading our apps. |
This issue still persists on the new v14.0.0. We still cannot upgrade our apps without a major rewrite. |
For anyone concerned with this issue - I have investigated further and found that this issue can be worked around in user-land. TL;DR: The problem is prevented by not redirecting on a data request in the server-side part of a custom redirect function. Example below:
To elaborate - the issue is caused by a redirected url being returned as an effect from the
fetchData request returns with the redirected url in the x-nextjs-matched-path header if a redirect happens in getInitialProps, when performing a data request.In case of my demo from the reproduction, the request url is "/_next/data/development/content.json" .Such request can be regognized in the getInitialProps function by the incoming x-nextjs-data: "1" header and then not redirecting in such case.
It would be great to have this handled internally in next, so userland does not have to rely on fixing a common pattern used for routing in nextjs < 10. |
This issue has been automatically marked as stale due to two years of inactivity. It will be closed in 7 days unless there’s further input. If you believe this issue is still relevant, please leave a comment or provide updated details. Thank you. |
Verify canary release
Provide environment information
What browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
Describe the Bug
The redirect happens as a rewrite instead of a redirect. The correct redirected page displays, but the URL stays as the page we've initially navigated to.
SSR still works fine as can be seen when going straight to
/content
in the codesandbox demo from the url bar.Expected Behavior
A normal redirect happens.
Link to reproduction
https://codesandbox.io/s/hungry-glitter-isub6z?file=/pages/content.tsx
To Reproduce
Redirect the user inside a
getInitialProps
call while also using middleware in your project.A sample redirection logic can be seen in the codesandbox in
/routing.ts
The text was updated successfully, but these errors were encountered: