Next.js issue with middleware/catch-all/rewrites/getInitialProps when client side navigation occurs #50212
Labels
bug
Issue was opened via the bug report template.
Navigation
Related to Next.js linking (e.g., <Link>) and navigation.
Pages Router
Related to Pages Router.
Runtime
Related to Node.js or Edge Runtime with Next.js.
Verify canary release
Provide environment information
❯ npx next info Operating System: Platform: linux Arch: x64 Version: #20-Ubuntu SMP PREEMPT_DYNAMIC Thu Apr 6 07:48:48 UTC 2023 Binaries: Node: 18.16.0 npm: 9.5.1 Yarn: 1.22.19 pnpm: N/A Relevant packages: next: 13.4.4-canary.2 eslint-config-next: 13.4.3 react: 18.2.0 react-dom: 18.2.0 typescript: 5.0.4
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://github.com/mabasic/nextjs-middleware-catch-all-issue
To Reproduce
Next.js issue with middleware/catch-all/rewrites/getInitialProps when client side navigation occurs
Must be built with
npm run build
and started withnpm run start
for the issue to appear. It works in development mode withnpm run dev
.Reproduction steps
next.config.js
:Existence of
middleware.ts
file. Can be an empty file to reproduce the issue.src/middleware.ts
:Catch-all page route eg.
src/pages/test/[...slugs].js
.Must have
getInitialProps
for this issue to appear. It works when server rendering, but does not work on client side navigation.src/pages/test/[...slugs].tsx
:Issue
Open the browser on
http://localhost:3000/something1/test2
, and click on the link "something2/test2".If you check the console log (in the browser) after a client side navigation event, you will not see
slugs
, but onlyslug
. On server rendering this works properly. You get bothslugs
andslug
in the console.Solution
There are two solutions:
slugs
to the page params (see example bellow)Example:
next.config.js
:Describe the Bug
Catch-all variable is not passed to context query when middleware file is present and when getInitialProps is used, and when a client-side navigation occurs.
Expected Behavior
Catch-all variable should be passed in this scenario.
Which browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
The text was updated successfully, but these errors were encountered: