Client-side navigation error with rewrites and catch-all routes #74922
Labels
Middleware
Related to Next.js Middleware.
Navigation
Related to Next.js linking (e.g., <Link>) and navigation.
Pages Router
Related to Pages Router.
Link to the code that reproduces this issue
repository: https://github.com/klaasman/nextjs-rewrite-catchall-conflict
preview deployment: https://nextjs-rewrite-catchall-conflict.vercel.app/
To Reproduce
A routing precedence issue arises when URL rewrites conflict with catch-all routes during client-side navigation, but only when middleware exists AND the catch-all route has
fallback: false
AND the resource served by the proxied host returns a 2xx response.To reproduce, either clone the repository and run it, or open the preview deployment at https://nextjs-rewrite-catchall-conflict.vercel.app, or follow the steps below:
middleware.ts
file (can be an empty function).pages/[...segments].tsx
wheregetStaticPaths
is configured withfallback: false
.next.config.js
rewrite rule to proxy incoming requests (usingfallback
rewrites)/200
using client-side navigation (via Link component).Current vs. Expected behavior
Current behavior:
During client-side navigation, the catch-all route takes precedence over the configured rewrite when the proxied endpoint returns a 2xx response. This results in incorrect routing behavior. (see preview-deployment)
Expected behavior:
The client-side navigation should follow the rewrite rule, correctly routing to the proxied endpoint regardless of the 2xx response. The catch-all route should not interfere with the rewrite when the endpoint returns a successful response.
Provide environment information
Operating System: Platform: darwin Arch: arm64 Version: Darwin Kernel Version 23.6.0: Mon Jul 29 21:14:30 PDT 2024; root:xnu-10063.141.2~1/RELEASE_ARM64_T6000 Available memory (MB): 32768 Available CPU cores: 10 Binaries: Node: 22.12.0 npm: 10.9.0 Yarn: 1.22.19 pnpm: 9.14.2 Relevant Packages: next: 15.2.0-canary.11 // Latest available version is detected (15.2.0-canary.11). eslint-config-next: N/A react: 19.0.0 react-dom: 19.0.0 typescript: 5.7.3 Next.js Config: output: N/A
Which area(s) are affected? (Select all that apply)
Middleware, Pages Router, Navigation
Which stage(s) are affected? (Select all that apply)
next dev (local), next start (local), Vercel (Deployed)
Additional context
No response
The text was updated successfully, but these errors were encountered: