`redirect` with empty string causes infinite loop
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 142k
- Forks
- 32.4k
- Avg merge
- 2d 14h
- Merged PRs (30d)
- 351
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked reproduction repository and its page.tsx, then compare browser and curl behavior for redirect("") in Next.js 15.2.4. Read the redirect handling and client-side router paths related to the empty location header; done means the browser no longer loops and the intended handling of an empty redirect is covered by a regression test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, nextjs, react
- Domain
- frontend, full-stack
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100