-
Notifications
You must be signed in to change notification settings - Fork 15
Fix Router.getPageHref race condition in parallel page generation #466
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
@copilot fix the following build error: Error occurred prerendering page "/about". Read more: https://nextjs.org/docs/messages/prerender-error |
Fixed! The build error was caused by a race condition where global static variables were being overwritten during parallel page generation. Solution: Replaced global variables with React's Key change in commit 47dc1bb:
Verified: Example app now builds successfully with all localized pages rendering correctly, including Build output shows all pages compiling successfully: Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
Problem
During Next.js parallel page generation (
next build),StaticRouterwas using static class properties (LOCALE,PAGE_HREF,PARAMS) that are shared globally across all server components. This caused race conditions where multiple pages being generated simultaneously would overwrite each other's values, resulting in inconsistentRouter.getPageHref()results and build failures with empty locale errors.Example of the Issue
During parallel build:
/en/stuffbegins:Router.setPageHref('/en/stuff')/fr/trucbegins:Router.setPageHref('/fr/truc')← overwrites global state/en/stuffrenders:Router.getPageHref()returns'/fr/truc'or empty string ❌This resulted in build errors like:
Solution
Replaced global static variables with React's
cache()function to create request-scoped storage. React'scache()is specifically designed for React Server Components and ensures that each page render gets its own isolated context, preventing race conditions during parallel builds.How It Works
React's
cache()creates a per-request memoized function. During a single page render, all calls togetRequestContext()return the same object. Different page renders get different objects, eliminating race conditions:Changes Made
Core Implementation
src/router/static-router.ts- Replaced global static variables with React'scache()for request-scoped storage; includes graceful fallback if React cache is unavailableKey Features
✅ Request Isolation - Each page render has its own isolated state
✅ Backward Compatible - Falls back gracefully if React's cache is unavailable
✅ Zero Template Changes - Existing
setLocale/setPageHrefcalls work without modification✅ Well Tested - All 129 existing tests pass
✅ Production Ready - Example app builds successfully without errors
Verification
Fixes race conditions during parallel page generation by using React's
cache()for request-scoped Router state isolation.Fixes #401
Original prompt
Fixes #401
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.