Skip to content

Commit

Permalink
fix(turbopack): use correct layout for 404 page
Browse files Browse the repository at this point in the history
  • Loading branch information
ForsakenHarmony committed Jan 24, 2024
1 parent 1c793aa commit 58ee3ab
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 57 deletions.
106 changes: 52 additions & 54 deletions packages/next-swc/crates/next-core/src/app_structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,13 @@ async fn directory_tree_to_loader_tree(

components.metadata.base_page = Some(app_page.clone());

if app_page.is_root() && components.not_found.is_none() {
// the root directory in the app dir.
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() && components.layout.is_some();

if (is_root_directory || is_root_layout) && components.not_found.is_none() {
components.not_found = Some(
get_next_package(app_dir).join("dist/client/components/not-found-error.js".to_string()),
);
Expand Down Expand Up @@ -985,66 +991,58 @@ async fn directory_tree_to_entrypoints_internal_untraced(
*meta,
);
}

// Next.js has this logic in "collect-app-paths", where the root not-found page
// is considered as its own entry point.
if let Some(_not_found) = components.not_found {
let dev_not_found_tree = LoaderTree {
page: app_page.clone(),
segment: directory_name.clone(),
parallel_routes: indexmap! {
"children".to_string() => LoaderTree {
page: app_page.clone(),
segment: "__DEFAULT__".to_string(),
parallel_routes: IndexMap::new(),
components: Components {
default: Some(get_next_package(app_dir).join("dist/client/components/parallel-route-default.js".to_string())),
..Default::default()
}
.cell(),
global_metadata,
}
.cell(),
},
components: components.without_leafs().cell(),
global_metadata,
}
.cell();

{
let app_page = app_page.clone_push_str("not-found")?;
add_app_page(app_dir, &mut result, app_page, dev_not_found_tree).await?;
}
{
let app_page = app_page.clone_push_str("_not-found")?;
add_app_page(app_dir, &mut result, app_page, dev_not_found_tree).await?;
}
let not_found_tree = if components.not_found.is_some() {
LoaderTree {
page: app_page.clone(),
segment: directory_name.clone(),
parallel_routes: indexmap! {
"children".to_string() => LoaderTree {
page: app_page.clone(),
segment: "__DEFAULT__".to_string(),
parallel_routes: IndexMap::new(),
components: Components {
default: Some(get_next_package(app_dir).join("dist/client/components/parallel-route-default.js".to_string())),
..Default::default()
}.cell(),
global_metadata,
}.cell(),
},
components: components.without_leafs().cell(),
global_metadata,
}.cell()
} else {
// Create default not-found page for production if there's no customized
// not-found
let prod_not_found_tree = LoaderTree {
page: app_page.clone(),
segment: directory_name.to_string(),
parallel_routes: indexmap! {
"children".to_string() => LoaderTree {
page: app_page.clone(),
segment: "__PAGE__".to_string(),
parallel_routes: IndexMap::new(),
components: Components {
page: Some(get_next_package(app_dir).join("dist/client/components/not-found-error.js".to_string())),
..Default::default()
}
.cell(),
global_metadata,
}
.cell(),
},
components: components.without_leafs().cell(),
global_metadata,
}
.cell();
LoaderTree {
page: app_page.clone(),
segment: directory_name.to_string(),
parallel_routes: indexmap! {
"children".to_string() => LoaderTree {
page: app_page.clone(),
segment: "__PAGE__".to_string(),
parallel_routes: IndexMap::new(),
components: Components {
page: Some(get_next_package(app_dir).join("dist/client/components/not-found-error.js".to_string())),
..Default::default()
}.cell(),
global_metadata,
}.cell(),
},
components: components.without_leafs().cell(),
global_metadata,
}.cell()
};

{
let app_page = app_page.clone_push_str("not-found")?;
add_app_page(app_dir, &mut result, app_page, not_found_tree).await?;
}
{
let app_page = app_page.clone_push_str("_not-found")?;
add_app_page(app_dir, &mut result, app_page, prod_not_found_tree).await?;
add_app_page(app_dir, &mut result, app_page, not_found_tree).await?;
}
}

Expand Down
4 changes: 4 additions & 0 deletions packages/next-swc/crates/next-core/src/next_app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,10 @@ impl AppPath {
})
}

pub fn is_root(&self) -> bool {
self.0.is_empty()
}

pub fn contains(&self, other: &AppPath) -> bool {
for (i, segment) in other.0.iter().enumerate() {
if self.0.get(i) != Some(segment) {
Expand Down
1 change: 1 addition & 0 deletions test/development/basic/next-rs-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ describe('next.rs api', () => {
'/app',
'/app-edge',
'/app-nodejs',
'/not-found',
'/page-edge',
'/page-nodejs',
'/route-edge',
Expand Down
5 changes: 2 additions & 3 deletions test/turbopack-tests-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3874,12 +3874,11 @@
"app dir - not found with default 404 page should be able to navigate to page calling not-found",
"app dir - not found with default 404 page should be able to navigate to page with calling not-found in metadata",
"app dir - not found with default 404 page should error on client notFound from root layout in browser",
"app dir - not found with default 404 page should error on server notFound from root layout on server-side"
],
"failed": [
"app dir - not found with default 404 page should error on server notFound from root layout on server-side",
"app dir - not found with default 404 page should render default 404 with root layout for non-existent page",
"app dir - not found with default 404 page should render default not found for group routes if not found is not defined"
],
"failed": [],
"pending": [],
"flakey": [],
"runtimeError": false
Expand Down

0 comments on commit 58ee3ab

Please sign in to comment.