-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Description
What version of Next.js are you using?
11.1.2
What version of Node.js are you using?
16.8.0
What browser are you using?
Chrome
What operating system are you using?
Windows 10/Alpine Linux
How are you deploying your application?
next start
Describe the Bug
This bug has been observed when redirecting via a SSR or SSG dynamic route under these conditions:
- The client-side source and redirect destination paths are the same
- The path which triggers the redirect is handled by same route as the source/destination path
- The source and/or destination URL contains a hash (including an empty hash)
If all these conditions are true, the component exported from the route does not receive the props returned from the getServerSideProps/getStaticProps function. Instead, an internal next.js redirect object is received, i.e. { __N_REDIRECT: "/myPage", __N_REDIRECT_STATUS: 307 }
Example: the user is currently viewing /myPage#myHash, and clicks a link which redirects to /myPage (with or without a hash). If this redirect happens within the same dynamic route that handles /myPage, the component exported from this route will receive incorrect props as described above. However, if the redirect is handled in a separate route, no problem seems to occur and the correct props are received.
Our current workaround for the issue is to check for the __N_REDIRECT value in the props of the exported component, and run router.reload() when it is present.
Expected Behavior
Components exported from an SSR/SSG route should always receive the props returned from getServerSideProps/getStaticProps.
To Reproduce
- Clone https://github.com/anders-nom/nextjs-hash-redirect-bug
npm run buildnpm run start- Go to http://localhost:3000
- Click the link which leads to /myPage#myHash
- Observe the aforementioned behaviour when clicking the second link on this page, which triggers a redirect through the same catch-all route that handles /myPage. The first link will however work as expected, as the redirect happens in a separate route.