Anchor links include rewritten part of the url in Pages Router
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://codesandbox.io/p/devbox/thirsty-cartwright-t8dhtw
To Reproduce
- Start an empty next pages router application
- Setup a middleware based on the documentation Next Docs
- Instrument the middleware to rewrite all the pages by adding the prefix
en.
export function middleware(request: NextRequest) {
const newUrl = request.nextUrl.clone();
newUrl.pathname = `/en${newUrl.pathname}`;
return NextResponse.rewrite(newUrl);
}
- Include a page in
/en/index.tsx - Add an anchor Link in the created page
<Link href="#about"> - Try to access the index of the application
Current vs. Expected behavior
Following the steps from the previous section, I would expect that the link in the page would be only #about, but the prefix added in the middleware is included in the output of the page, the navigation works fine, but this affects the SEO of the page. In App Router the prefix is not included.
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_T6031
Available memory (MB): 49152
Available CPU cores: 16
Binaries:
Node: 18.19.0
npm: 10.2.3
Yarn: 1.22.4
pnpm: 10.4.0
Relevant Packages:
next: 15.3.0-canary.17 // Latest available version is detected (15.3.0-canary.17).
eslint-config-next: 15.2.3
react: 19.0.0
react-dom: 19.0.0
typescript: 5.8.2
Next.js Config:
output: N/A
Which area(s) are affected? (Select all that apply)
Middleware, Internationalization (i18n)
Which stage(s) are affected? (Select all that apply)
next dev (local), Vercel (Deployed)
Additional context
No response
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 CodeSandbox and reproduce the issue using the Pages Router, the middleware rewrite, and the /en/index.tsx page. Read the middleware and Link behavior around rewritten URLs; done means an anchor Link renders only #about while navigation still works, matching the App Router behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, nextjs, react
- Domain
- frontend, internationalization, web-dev
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100