vercel / next.js

The React Framework
https://nextjs.org
MIT License
125.96k stars 26.87k forks source link

Client-side navigation prefetches wrong data JSON on Vercel when using Middleware, SSG and Rewrites #60383

Open jkondela opened 9 months ago

jkondela commented 9 months ago

Link to the code that reproduces this issue

https://github.com/jkondela/next-rewrites-repro

To Reproduce

  1. Open https://next-rewrites-repro.vercel.app/
  2. Click on the link
  3. Next.js prefetches book.json, but page has url books, so Router will render page with empty props.
  4. Try reload the current page, it will print correct props. Only client-side navigation does not work.

Current vs. Expected behavior

Expected: When using client-side navigation (for route which has rewrite, middleware and dynamic SSG), it should work. Current: Client-side navigation does not work, page has empty props. Hard refresh has correct props.

Verify canary release

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP Thu Oct 5 21:02:42 UTC 2023
Binaries:
  Node: 18.17.1
  npm: 9.6.7
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 14.0.5-canary.43
  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)

Data fetching (gS(S)P, getInitialProps), Middleware / Edge (API routes, runtime), Routing (next/router, next/navigation, next/link)

Which stage(s) are affected? (Select all that apply)

Vercel (Deployed)

Additional context

After upgrade to Next 13, it stopped working. Also tested on 14 and 14-canary.

It works correctly on localhost, but not on Vercel.

It only does not work when using combination of SSG dynamic page with middleware and Next config rewrite. If I remove middleware.ts (which has only Response.next()), it will start working as expected.

aleehedl commented 8 months ago

I really hope this bug would be addressed some time soon.