The issue involves middleware evaluation and caching behavior in Next.js routing.
The issue occurs when `router.push()` reuses a cached redirect from a prefetched route without re-evaluating middleware, causing incorrect navigation behavior. The fix likely involves modifying how redirects are cached and ensuring middleware is re-evaluated when necessary. The scope is somewhat clear but requires understanding of Next.js routing and middleware internals.
https://github.com/KumarNitin19/nextjs-prefetch-redirect-bug
I'm running middleware that conditionally redirects based on a cookie value. The / route is protected and redirects to /process when the processingComplete cookie is not set.
Steps to Reproduce:
/process page (no cookie set)<Link href="/">Dashboard</Link>/ route/process (because cookie is false)processingComplete=truerouter.push('/') to navigate to dashboard/process page, no navigation occursLive Demo (Vercel): https://nextjs-prefetch-redirect-bug.vercel.app/
Expected:
When router.push('/') is called after the cookie changes:
/Current:
//process pageThe router appears to cache the redirect response from the prefetch and reuses it on router.push() without checking if the state has changed.
Relevant Packages:
Linking and Navigating, Middleware
Vercel (Deployed), Other (Deployed), next build (local)
Workarounds:
Claim this issue to let others know you're working on it. You'll earn 10 points when you complete it!