next.js
43c6ccfd - Fix parallel routes ignoring generateStaticParams from primary route (#84889)

Commit
193 days ago
Fix parallel routes ignoring generateStaticParams from primary route (#84889) ### What? Fixes a bug where `generateStaticParams` from the primary route was being ignored when dynamic segments existed in both regular routes (`children`) and parallel routes (e.g., `@breadcrumbs`), causing routes that should have been statically generated to return 404 errors. Also fixes a related redirect loop issue that occurred when serving 404 pages with RSC request header validation enabled. ### Why? **Primary Issue: Segment Deduplication** The `collectAppPageSegments` function had two code paths for deduplicating segments when the same dynamic parameter (like `[slug]`) appeared in both the primary route tree and parallel routes: 1. **Individual segment processing** - Correctly preferred non-parallel segments 2. **Batch processing at page boundaries** - Blindly overwrote existing segments without preference checking Due to BFS traversal order, parallel route segments would overwrite the correct `children` route segments during batch processing. This marked parameters as `isParallelRouteSegment: true`, which: - Prevented the build system from recognizing them as regular route parameters - Caused `generateStaticParams` to be ignored during prerendering - Resulted in all routes being treated as dynamic-only (404s with `dynamicParams = false`) This violated the architectural invariant that parallel routes are "secondary" to the main route hierarchy - the primary route tree should determine URL structure and static generation behavior. **Secondary Issue: 404 Page Redirect Loop** When serving a 404 page (NoFallbackError), the experimental RSC request header validation would cause an infinite redirect loop. This happened because: - Headers are already stripped when serving the 404 page - The validation comparison would always fail - This triggered a redirect, which would hit the same 404, creating a loop ### How? **Segment Preference Fix (`app-segments.ts`):** Applied consistent segment preference logic to both deduplication code paths in `collectAppPageSegments` and `collectFallbackRouteParams`, ensuring segments from the `children` route are always preferred over parallel route segments. This preserves the following guarantees: 1. **URL structure is determined by the primary route tree** - Parallel routes derive their parameters from `children`, not vice versa 2. **`generateStaticParams` works correctly** - Static generation is controlled by the primary route's configuration 3. **Parallel routes remain decorative** - They enhance the page without affecting core routing behavior **Redirect Loop Fix (`base-server.ts`):** Skip RSC request header validation when serving 404 pages (`!is404Page` condition), preventing the redirect loop caused by already-stripped headers failing the validation check. **Test Coverage:** Added comprehensive e2e test suite covering: - Root parameters with `generateStaticParams` and `dynamicParams = false` - Dynamic child routes with and without `generateStaticParams` - Parallel route slots with shared dynamic segments - Prefetch behavior to ensure no redirect loops during preloading Fixes # NAR-446
Author
Parents
Loading