Open the home page and click on the "Dynamic" nav link (/1)
Current vs. Expected behavior
Current behavior
"Page 1" content is loaded on navigation.
Expected behavior
"Page 2" content is loaded on navigation.
If you reload the page, you'll see "Page 2" loaded properly.
Additional details
PR https://github.com/vercel/next.js/pull/32767 introduced a way for middleware to opt-out of next/link prefetch caching by setting the x-middleware-cache header to no-cache. However, this doesn't seem to be working when using NextResponse.rewrite with dynamic routes (in this example, [id].tsx with ids 1 and 2). The middleware example simulates a change in behavior between prefetch and navigation by rewriting to different paths based on the purpose header === prefetch.
Compare this behavior with static routes (in this example, a.tsx and b.tsx) by clicking on the "Static" nav link (/a). You'll see the "Page B" content is loaded on navigation as expected.
Also compare this behavior with use of redirect instead of rewrite. If you change this line in the middleware from
Link to the code that reproduces this issue
https://github.com/ambrauer/next-prefetch-cache-issue
To Reproduce
yarn install
,yarn build
, thenyarn start
/1
)Current vs. Expected behavior
Current behavior "Page 1" content is loaded on navigation.
Expected behavior "Page 2" content is loaded on navigation.
If you reload the page, you'll see "Page 2" loaded properly.
Additional details PR https://github.com/vercel/next.js/pull/32767 introduced a way for middleware to opt-out of
next/link
prefetch caching by setting thex-middleware-cache
header tono-cache
. However, this doesn't seem to be working when usingNextResponse.rewrite
with dynamic routes (in this example,[id].tsx
with ids1
and2
). The middleware example simulates a change in behavior between prefetch and navigation by rewriting to different paths based on thepurpose
header ===prefetch
.Compare this behavior with static routes (in this example,
a.tsx
andb.tsx
) by clicking on the "Static" nav link (/a
). You'll see the "Page B" content is loaded on navigation as expected.Also compare this behavior with use of redirect instead of rewrite. If you change this line in the middleware from
to
and try the same "Dynamic" nav link, you'll see "Page 2" content is loaded on navigation as expected.
Provide environment information
Which area(s) are affected? (Select all that apply)
Middleware, Navigation, Pages Router
Which stage(s) are affected? (Select all that apply)
next start (local)
Additional context
Note this is Pages router; I have not verified if the issue exists for App router. See also related discussion here: https://github.com/vercel/next.js/discussions/43675