When transitioning from page A to page B with the Link component, there is a bug that causes the page component of page A to be rendered using pageProps derived from getServerSideProps of page B. This can result in runtime errors.
This only seems to happen when cleverly combining basePath, rewrites and Middleware (see the reproduction repository for details). The following changes to the Reproduction Repository will prevent the problem.
Remove basePath from next.config.js.
Remove rewrites from next.config.js.
Delete middleware.ts.
Return NextResponse.next() instead of NextResponse.rewrite(req.nextUrl) from middleware.ts
In addition, the problem is reproduced when the length of the path to be transitioned to is less than 4, but not when it is greater than 5. For example, the problem reproduces for transitions to /1234, but not for /12345.
Expected Behavior
When transitioning from page A to page B with a Link component, the page component of page B should be rendered using pageProps derived from getServerSideProps of page B.
Which browser are you using? (if relevant)
Chrome 112.0.5615.137(Official Build) (arm64)
How are you deploying your application? (if relevant)
Verify canary release
Provide environment information
Which area(s) of Next.js are affected? (leave empty if unsure)
Data fetching (gS(S)P, getInitialProps), Middleware / Edge (API routes, runtime), Routing (next/router, next/navigation, next/link)
Link to the code that reproduces this issue or a replay of the bug
https://codesandbox.io/s/github/mizdra/reproduction-nextjs-middleware-basepath-rewrites
https://github.com/mizdra/reproduction-nextjs-middleware-basepath-rewrites
To Reproduce
npm i && npm run dev
./1234 (Runtime error occurs)
https://github.com/vercel/next.js/assets/9639995/e0b934b5-fdbc-490d-b7a5-79fd09cb7522
Describe the Bug
When transitioning from page A to page B with the
Link
component, there is a bug that causes the page component of page A to be rendered usingpageProps
derived fromgetServerSideProps
of page B. This can result in runtime errors.This only seems to happen when cleverly combining
basePath
,rewrites
and Middleware (see the reproduction repository for details). The following changes to the Reproduction Repository will prevent the problem.basePath
fromnext.config.js
.rewrites
fromnext.config.js
.middleware.ts
.NextResponse.next()
instead ofNextResponse.rewrite(req.nextUrl)
frommiddleware.ts
In addition, the problem is reproduced when the length of the path to be transitioned to is less than 4, but not when it is greater than 5. For example, the problem reproduces for transitions to
/1234
, but not for/12345
.Expected Behavior
When transitioning from page A to page B with a Link component, the page component of page B should be rendered using
pageProps
derived fromgetServerSideProps
of page B.Which browser are you using? (if relevant)
Chrome 112.0.5615.137(Official Build) (arm64)
How are you deploying your application? (if relevant)
next dev