Unverified Commit c6cefce4 authored by Joshua Hannaford's avatar Joshua Hannaford Committed by GitHub

fix(Turbopack): Fix duplicated layout rendering in edge cases (#81948)

## Fix module reference handling in app_structure.rs creating duplicated layouts

### What?
Fixed a bug in the `check_and_update_module_references` function by
making the code always overrides the `not-found`, `unauthorized`, etc
files at the top level group route layer.

### Why?
The previous implementation had a bug that when a directory was setup
like the added test case where the layouts from a lower directory would
render when only the layouts at the level of the not-found page should
have rendered.

### How?
- Added the ability to force overriding of the `module` when running
`check_and_update_module_references`
- Split the logic between global and layer-specific modules

Fixes #PACK-5081
parent e2b6a50a
...@@ -923,18 +923,31 @@ async fn directory_tree_to_loader_tree( ...@@ -923,18 +923,31 @@ async fn directory_tree_to_loader_tree(
/// set. /// set.
/// * `file_path` - The file path to the default page if neither the current module nor the parent /// * `file_path` - The file path to the default page if neither the current module nor the parent
/// module is set. /// module is set.
/// * `is_first_layer_group_route` - If true, the module will be overridden with the parent module
/// if it is not set.
async fn check_and_update_module_references( async fn check_and_update_module_references(
app_dir: FileSystemPath, app_dir: FileSystemPath,
module: &mut Option<FileSystemPath>, module: &mut Option<FileSystemPath>,
parent_module: &mut Option<FileSystemPath>, parent_module: &mut Option<FileSystemPath>,
file_path: &str, file_path: &str,
is_first_layer_group_route: bool,
) -> Result<()> { ) -> Result<()> {
match (module.as_mut(), parent_module.as_mut()) { match (module.as_mut(), parent_module.as_mut()) {
// If the module is set, update the parent module to the same value
(Some(module), _) => *parent_module = Some(module.clone()), (Some(module), _) => *parent_module = Some(module.clone()),
(None, Some(parent_module)) => *module = Some(parent_module.clone()), // If we are in a first layer group route and we have a parent module, we want to override
// a nonexistent module with the parent module
(None, Some(parent_module)) if is_first_layer_group_route => {
*module = Some(parent_module.clone())
}
// If we are not in a first layer group route, and the module is not set, and the parent
// module is set, we do nothing
(None, Some(_)) => {}
// If the module is not set, and the parent module is not set, we override with the default
// page. This can only happen in the root directory because after this the parent module
// will always be set.
(None, None) => { (None, None) => {
let default_page = get_next_package(app_dir.clone()).await?.join(file_path)?; let default_page = get_next_package(app_dir).await?.join(file_path)?;
*module = Some(default_page.clone()); *module = Some(default_page.clone());
*parent_module = Some(default_page); *parent_module = Some(default_page);
} }
...@@ -943,6 +956,25 @@ async fn check_and_update_module_references( ...@@ -943,6 +956,25 @@ async fn check_and_update_module_references(
Ok(()) Ok(())
} }
/// Checks if the current directory is the root directory and if the module is not set.
/// If the module is not set, it will be set to the default page.
///
/// # Arguments
/// * `app_dir` - The application directory.
/// * `module` - The module to check and update if it is not set.
/// * `file_path` - The file path to the default page if the module is not set.
async fn check_and_update_global_module_references(
app_dir: FileSystemPath,
module: &mut Option<FileSystemPath>,
file_path: &str,
) -> Result<()> {
if module.is_none() {
*module = Some(get_next_package(app_dir).await?.join(file_path)?);
}
Ok(())
}
async fn directory_tree_to_loader_tree_internal( async fn directory_tree_to_loader_tree_internal(
app_dir: FileSystemPath, app_dir: FileSystemPath,
global_metadata: Vc<GlobalMetadata>, global_metadata: Vc<GlobalMetadata>,
...@@ -967,16 +999,19 @@ async fn directory_tree_to_loader_tree_internal( ...@@ -967,16 +999,19 @@ async fn directory_tree_to_loader_tree_internal(
// the root directory in the app dir. // the root directory in the app dir.
let is_root_directory = app_page.is_root(); let is_root_directory = app_page.is_root();
// an alternative root layout (in a route group which affects the page, but not
// the path).
let is_root_layout = app_path.is_root() && modules.layout.is_some();
if is_root_directory || is_root_layout { // If the first layer is a group route, we treat it as root layer
let is_first_layer_group_route = app_page.is_first_layer_group_route();
// Handle the non-global modules that should always be overridden for top level groups or set to
// the default page if they are not set.
if is_root_directory || is_first_layer_group_route {
check_and_update_module_references( check_and_update_module_references(
app_dir.clone(), app_dir.clone(),
&mut modules.not_found, &mut modules.not_found,
&mut parent_modules.not_found, &mut parent_modules.not_found,
"dist/client/components/builtin/not-found.js", "dist/client/components/builtin/not-found.js",
is_first_layer_group_route,
) )
.await?; .await?;
...@@ -985,6 +1020,7 @@ async fn directory_tree_to_loader_tree_internal( ...@@ -985,6 +1020,7 @@ async fn directory_tree_to_loader_tree_internal(
&mut modules.forbidden, &mut modules.forbidden,
&mut parent_modules.forbidden, &mut parent_modules.forbidden,
"dist/client/components/builtin/forbidden.js", "dist/client/components/builtin/forbidden.js",
is_first_layer_group_route,
) )
.await?; .await?;
...@@ -993,13 +1029,15 @@ async fn directory_tree_to_loader_tree_internal( ...@@ -993,13 +1029,15 @@ async fn directory_tree_to_loader_tree_internal(
&mut modules.unauthorized, &mut modules.unauthorized,
&mut parent_modules.unauthorized, &mut parent_modules.unauthorized,
"dist/client/components/builtin/unauthorized.js", "dist/client/components/builtin/unauthorized.js",
is_first_layer_group_route,
) )
.await?; .await?;
}
check_and_update_module_references( if is_root_directory {
check_and_update_global_module_references(
app_dir.clone(), app_dir.clone(),
&mut modules.global_error, &mut modules.global_error,
&mut parent_modules.global_error,
"dist/client/components/builtin/global-error.js", "dist/client/components/builtin/global-error.js",
) )
.await?; .await?;
......
...@@ -293,6 +293,11 @@ impl AppPage { ...@@ -293,6 +293,11 @@ impl AppPage {
) )
} }
/// Returns true if there is only one segment and it is a group.
pub fn is_first_layer_group_route(&self) -> bool {
self.0.len() == 1 && matches!(self.0.last(), Some(PageSegment::Group(_)))
}
pub fn complete(&self, page_type: PageType) -> Result<Self> { pub fn complete(&self, page_type: PageType) -> Result<Self> {
self.clone_push(PageSegment::PageType(page_type)) self.clone_push(PageSegment::PageType(page_type))
} }
......
import Header from '../../components/Header'
import Footer from '../../components/Footer'
export default function DefaultLayout({ children }) {
return (
<>
<Header />
{children}
<Footer />
</>
)
}
import { notFound } from 'next/navigation'
export default function NotFoundTemp() {
notFound()
}
export default function SolutionsLayout({ children }) {
return (
<>
<h1>Solutions Layout</h1>
<main>{children}</main>
<h1>Solutions Footer Test</h1>
</>
)
}
export default function Layout({ children }) {
return (
<>
<h1>Default Layout</h1>
{children}
<h1>Default Footer Test</h1>
</>
)
}
import { NotFoundTemp } from '../components/NotFoundTemp'
import DefaultLayout from './(default)/layout'
export default function NotFound() {
return (
<>
<DefaultLayout>
<NotFoundTemp />
</DefaultLayout>
</>
)
}
export default function Footer() {
return (
<footer id="footer">
<h1>Footer</h1>
</footer>
)
}
export default function Header() {
return (
<header>
<h1 id="header">Header</h1>
</header>
)
}
export function NotFoundTemp() {
return <div>Not Found</div>
}
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
import Link from 'next/link'
export default function Page() {
return (
<main>
<h1>Home Page</h1>
<Link href="/solutions/404" id="to-404">
Go to 404
</Link>
</main>
)
}
import { nextTestSetup } from 'e2e-utils'
describe('app dir - duplicate layout components', () => {
const { next, skipped } = nextTestSetup({
files: __dirname,
skipDeployment: true,
})
if (skipped) {
return
}
it('should not duplicate layout elements when navigating to 404', async () => {
const browser = await next.browser('/solutions/404')
// Verify counts haven't changed - no duplication
expect((await browser.elementsByCss('body')).length).toBe(1)
expect((await browser.elementsByCss('#header')).length).toBe(1)
expect((await browser.elementsByCss('#footer')).length).toBe(1)
})
})
...@@ -2348,6 +2348,15 @@ ...@@ -2348,6 +2348,15 @@
"flakey": [], "flakey": [],
"runtimeError": false "runtimeError": false
}, },
"test/e2e/app-dir/duplicate-layout-components/duplicate-layout-components.test.js": {
"passed": [
"app dir - duplicate layout components should not duplicate layout elements when navigating to 404"
],
"failed": [],
"pending": [],
"flakey": [],
"runtimeError": false
},
"test/e2e/app-dir/dynamic-requests/dynamic-requests.test.ts": { "test/e2e/app-dir/dynamic-requests/dynamic-requests.test.ts": {
"passed": [ "passed": [
"dynamic-requests should not error for dynamic requests in pages", "dynamic-requests should not error for dynamic requests in pages",
...@@ -6162,6 +6162,15 @@ ...@@ -6162,6 +6162,15 @@
"flakey": [], "flakey": [],
"runtimeError": false "runtimeError": false
}, },
"test/e2e/app-dir/duplicate-layout-components/duplicate-layout-components.test.js": {
"passed": [
"app dir - duplicate layout components should not duplicate layout elements when navigating to 404"
],
"failed": [],
"pending": [],
"flakey": [],
"runtimeError": false
},
"test/e2e/app-dir/dynamic-requests/dynamic-requests.test.ts": { "test/e2e/app-dir/dynamic-requests/dynamic-requests.test.ts": {
"passed": [ "passed": [
"dynamic-requests should not error for dynamic requests in pages", "dynamic-requests should not error for dynamic requests in pages",
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