[Pages Router] - Middleware breaks URL masking
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 or a replay of the bug
https://codesandbox.io/p/github/ValenCassa/bug-next/main?file=/middleware.ts:1,1
To Reproduce
- Start the application in development
- Click on the link
- Remove the middleware file
- Try again
Current vs. Expected behavior
Current behavior:
When using middleware, the link redirects you to whatever the as prop has been set to instead of browsing to the href and masking the URL.
Expected behavior:
The link should should browse to the href and mask the URL based on the as prop.
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 22.5.0: Mon Apr 24 20:40:17 PDT 2023; root:xnu-8796.121.2~3/RELEASE_ARM64_T8112
Binaries:
Node: 20.5.0
npm: 9.8.0
Yarn: N/A
pnpm: N/A
Relevant Packages:
next: 13.4.20-canary.23
eslint-config-next: N/A
react: 18.2.0
react-dom: 18.2.0
typescript: 5.1.3
Next.js Config:
output: N/A
Which area(s) are affected? (Select all that apply)
Middleware / Edge (API routes, runtime), Routing (next/router, next/navigation, next/link)
Additional context
I spent several hours trying to debug what was causing the issue and was going crazy. It seems the nextUrl from the request object is returning the pathname as /test/1?id=1, so it's actually appending both whatever you're using for the as prop and the href. I tried setting the pathname manually too with no success.
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 reproduction and inspect middleware.ts and the link using href and as. Run the app in development, reproduce the redirect, and trace the request nextUrl pathname. Done means middleware preserves navigation to href while the browser URL remains masked by as, with the behavior covered by a regression test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, next.js, react
- Domain
- frontend, web-dev
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100