Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Open
1 task done
ParallelUniv3rse opened this issue Jul 28, 2022 · 5 comments
Open
1 task done
Labels
bug Issue was opened via the bug report template. stale The issue has not seen recent activity.

Comments

@ParallelUniv3rse
Copy link

ParallelUniv3rse commented Jul 28, 2022

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 21.4.0: Fri Mar 18 00:47:26 PDT 2022; root:xnu-8020.101.4~15/RELEASE_ARM64_T8101
Binaries:
  Node: 16.10.0
  npm: 7.24.0
  Yarn: 1.22.15
  pnpm: 6.11.0
Relevant packages:
  next: 12.2.0
  eslint-config-next: N/A
  react: 17.0.2
  react-dom: 17.0.2

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

@ParallelUniv3rse ParallelUniv3rse added the bug Issue was opened via the bug report template. label Jul 28, 2022
@codynova
Copy link

codynova commented Oct 5, 2022

This also occurs with redirects defined in next.config.js, see reproductions for Next 12.2 and Next 12.3 here https://github.com/codynova/next-middleware-redirects

@ParallelUniv3rse
Copy link
Author

This issue still persists in latest canary v13. It still prevents us from upgrading our apps.

@ParallelUniv3rse
Copy link
Author

This issue still persists on the new v14.0.0. We still cannot upgrade our apps without a major rewrite.

@ParallelUniv3rse
Copy link
Author

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:

if (res) {
    const isDataReq = ctx.req?.headers["x-nextjs-data"]; // checking for a middleware data request prevents this issue

    if (!isDataReq) {
      // Server redirect
      res.writeHead(push ? 307 : 302, {
        Location: as || location
      });
    }

    res.end();
  } else {
  ...

To elaborate - the issue is caused by a redirected url being returned as an effect from the fetchData request in the withMiddlewareEffects function in router (

const data = await options.fetchData()
). 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.

@nextjs-bot
Copy link
Collaborator

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.

@nextjs-bot nextjs-bot added the stale The issue has not seen recent activity. label May 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. stale The issue has not seen recent activity.
Projects
None yet
Development

No branches or pull requests

3 participants