Commit f0f1c03a authored by Jimmy Lai's avatar Jimmy Lai

fix: guard dynamic segment propagation to prevent static-siblings regression

When a parallel slot has both a static and dynamic subdirectory matching
the target path (e.g. @panel/settings and @panel/[id] for /dashboard/settings),
the static match should take priority. Without this guard, BTreeMap iteration
order causes [id] to be processed before settings, and the dynamic match
incorrectly wins.

The fix checks if any static sibling at the current directory level directly
matches the next target path segment. If so, dynamic segment subdirectories
skip dynamic matching, ensuring the static route is used instead.
parent e783e3be
......@@ -18,7 +18,7 @@ use turbopack_core::issue::{
use crate::{
mode::NextMode,
next_app::{
AppPage, AppPath, PageSegment, PageType,
AppPage, AppPath, PageSegment, PageType, PathSegment,
metadata::{
GlobalMetadataFileMatch, MetadataFileMatch, match_global_metadata_file,
match_local_metadata_file, normalize_metadata_route,
......@@ -1303,6 +1303,30 @@ async fn directory_tree_to_loader_tree_internal(
let mut duplicate = FxHashMap::default();
// When match_dynamic is true and we're inside a parallel slot subtree,
// check if any static subdirectory at the current level directly matches
// the next segment of for_app_path. If so, dynamic segment subdirectories
// at this level should NOT use dynamic matching, because the static match
// should take priority. For example, if @panel has both "settings" and
// "[id]" subdirectories, and we're building for /dashboard/settings, the
// "settings" directory should be used, not "[id]".
let has_static_match_at_this_level = if match_dynamic {
let current_app_path = AppPath::from(app_page.clone());
if let Some(target_segment) = for_app_path.0.get(current_app_path.0.len()) {
if let PathSegment::Static(target_name) = target_segment {
directory_tree
.subdirectories
.contains_key(target_name.as_str())
} else {
false
}
} else {
false
}
} else {
false
};
for (subdir_name, subdirectory) in &directory_tree.subdirectories {
let parallel_route_key = match_parallel_route(subdir_name);
......@@ -1327,7 +1351,17 @@ async fn directory_tree_to_loader_tree_internal(
// For parallel route slots, use dynamic matching so that a slot with
// e.g. @parallel/test/[param] can match the static path /test/static.
// The match_dynamic flag propagates down the entire parallel subtree.
let child_match_dynamic = match_dynamic || parallel_route_key.is_some();
// However, if a static subdirectory at this level directly matches the
// target path, dynamic segment subdirectories should not use dynamic
// matching (static routes take priority over dynamic ones).
let child_match_dynamic = if has_static_match_at_this_level
&& is_dynamic_segment(subdir_name)
&& parallel_route_key.is_none()
{
false
} else {
match_dynamic || parallel_route_key.is_some()
};
let subtree = Box::pin(directory_tree_to_loader_tree_internal(
app_dir.clone(),
......
......@@ -219,4 +219,48 @@ describe('normalizeCatchallRoutes', () => {
// ensure values are correct after normalizing
expect(appPaths).toMatchObject(initialAppPaths)
})
// Test to verify normalizeDynamicRoutes doesn't break static-siblings parallel route test
it('should not propagate dynamic parallel routes when the slot already has a static match', () => {
// Mimics the static-siblings dashboard scenario:
// /dashboard/@panel/settings/page.tsx (static)
// /dashboard/@panel/[id]/page.tsx (dynamic)
const appPaths: Record<string, string[]> = {
'/dashboard': ['/dashboard/page'],
'/dashboard/settings': ['/dashboard/@panel/settings/page'],
'/dashboard/[id]': ['/dashboard/@panel/[id]/page'],
}
const initialAppPaths = JSON.parse(JSON.stringify(appPaths))
normalizeCatchAllRoutes(appPaths)
// /dashboard/settings should NOT get /dashboard/@panel/[id]/page added
// because @panel already has a match (settings)
expect(appPaths).toMatchObject(initialAppPaths)
})
// Test for the actual fix: dynamic parallel route should be propagated when no static match exists
it('should propagate dynamic parallel routes when the slot has no match', () => {
// Mimics the #62656 scenario:
// /test/[testParam]/page.tsx (children dynamic)
// /test/static/page.tsx (children static)
// /@parallel/test/[testParam]/page.tsx (parallel dynamic, no static match)
const appPaths: Record<string, string[]> = {
'/': ['/page'],
'/test/static': ['/test/static/page'],
'/test/[testParam]': [
'/test/[testParam]/page',
'/@parallel/test/[testParam]/page',
],
}
normalizeCatchAllRoutes(appPaths)
// /test/static SHOULD get /@parallel/test/[testParam]/page added
// because @parallel has no match for /test/static
expect(appPaths['/test/static']).toContain(
'/@parallel/test/[testParam]/page'
)
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment