Skip to content
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

redirect with empty string causes infinite loop #77599

Open
mbranch opened this issue Mar 28, 2025 · 1 comment · May be fixed by #77606
Open

redirect with empty string causes infinite loop #77599

mbranch opened this issue Mar 28, 2025 · 1 comment · May be fixed by #77606
Labels
Redirects Related to redirecting.

Comments

@mbranch
Copy link

mbranch commented Mar 28, 2025

Link to the code that reproduces this issue

https://github.com/mbranch/next.js-redirect-bug

To Reproduce

// page.tsx
import { redirect } from "next/navigation";

export default function Home() {
	redirect("");
}
# Browser response headers
HTTP/1.1 200 OK
Vary: RSC, Next-Router-State-Tree, Next-Router-Prefetch, Next-Router-Segment-Prefetch, Accept-Encoding
Cache-Control: no-store, must-revalidate
Content-Type: text/x-component
Content-Encoding: gzip
Date: Fri, 28 Mar 2025 22:18:40 GMT
Connection: keep-alive
Keep-Alive: timeout=5
Transfer-Encoding: chunked
# curl response headers
HTTP/1.1 307 Temporary Redirect
Vary: RSC, Next-Router-State-Tree, Next-Router-Prefetch, Next-Router-Segment-Prefetch, Accept-Encoding
link: </_next/static/media/gyByhwUxId8gMEwcGFWNOITd-s.p.da1ebef7.woff2>; rel=preload; as="font"; crossorigin=""; type="font/woff2", </_next/static/media/or3nQ6H_1_WfwkMZI_qYFrcdmhHkjko-s.p.be19f591.woff2>; rel=preload; as="font"; crossorigin=""; type="font/woff2"
location: 
Cache-Control: no-store, must-revalidate
X-Powered-By: Next.js
Content-Type: text/html; charset=utf-8
Date: Fri, 28 Mar 2025 22:15:21 GMT
Connection: keep-alive
Keep-Alive: timeout=5

Current vs. Expected behavior

While this example is contrived, what's surprising about the behavior is that Next.js gets into an infinite loop, but only when making the request with a browser.

This suggests to me that the client-side router is misbehaving when the redirect response's location header is empty (notable in the browser response headers above). It should probably attempt to detect redirect loops like modern browsers do.

Expected behavior:

  • redirect("") should maybe throw an error?
  • The client-side router should detect simple redirect loops like this.

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.3.0: Thu Jan  2 20:24:23 PST 2025; root:xnu-11215.81.4~3/RELEASE_ARM64_T6020
  Available memory (MB): 65536
  Available CPU cores: 12
Binaries:
  Node: 23.3.0
  npm: 10.9.0
  Yarn: 1.22.22
  pnpm: 10.2.0
Relevant Packages:
  next: 15.2.4 // Latest available version is detected (15.2.4).
  eslint-config-next: N/A
  react: 19.1.0
  react-dom: 19.1.0
  typescript: 5.8.2
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Redirects

Which stage(s) are affected? (Select all that apply)

next dev (local), next start (local), next build (local)

Additional context

I tested on v15.2.4, and v15.1.7.

@github-actions github-actions bot added the Redirects Related to redirecting. label Mar 28, 2025
@petejodo
Copy link

This impacts redirects in next.config.js as well. If using the config below and then visiting /beta, an infinite loop occurs. Confirmed Location header in the 308 response is an empty string which seems like the same issue as this

module.exports = {
  // ...

  async redirects() {
    return [
      {
        source: "/beta/:path*",
        destination: "/:path*",
        permanent: true,
      },
    ];
  },

  // ...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Redirects Related to redirecting.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants