Unverified Commit 291aefd9 authored by Wyatt Johnson's avatar Wyatt Johnson Committed by GitHub

Fix 404 responses for interception routes with missing children slots (#85779)

### What?

Introduces a null-rendering default component for interception routes to
prevent 404 responses when children slots are missing.

### Why?

When rendering interception routes like `(.)[id]`, there's a segment
mismatch where the interception route lacks a children slot that the
base `[id]` route has.

**The Bug:**
Previously, the framework injected a 404 page into the missing default
slot for the children route. On platforms like Vercel, this caused full
404 RSC responses because the HTTP status code has semantic meaning for
route behavior when using cache components. This resulted in the
"unknown segment value fallback page" issue.

**Impact:**
- RSC requests for interception routes would return 404 status codes
- Platform routing behavior was incorrectly triggered by these 404
responses
- Only affects pages rendering with cache components

### How?

- Created new `default-null.tsx` builtin component that renders `null`
- Updated Rust code (`app_structure.rs`) to detect interception routes
and use the null default
- Updated webpack loader (`next-app-loader/index.ts`) to check
`isInterceptionRouteAppPath()` and inject the null default
- Removed test-specific default component that was working around this
bug
- The old `page.tsx` is still used during client navigation, so there's
no visible change to users

This ensures minimal RSC payload changes and prevents 404 status codes
while maintaining existing client-side behavior.

NAR-483
parent 1f0322f2
......@@ -1226,17 +1226,19 @@ async fn directory_tree_to_loader_tree_internal(
if let Some(subtree) = subtree {
if let Some(key) = parallel_route_key {
let is_inside_catchall = app_page.is_catchall();
// Validate that parallel routes (except "children") have a default.js file.
// Skip this validation if the slot is UNDER a catch-all route (i.e., the
// parallel route is a child of a catch-all segment).
// This validation matches the webpack loader's logic but is implemented
// differently due to Turbopack's single-pass recursive processing.
// Check if we're inside a catch-all route (i.e., the parallel route is a child
// of a catch-all segment). Only skip validation if the slot is UNDER a catch-all.
// For example:
// /[...catchAll]/@slot - is_inside_catchall = true (skip validation) ✓
// /@slot/[...catchAll] - is_inside_catchall = false (require default) ✓
// The catch-all provides fallback behavior, so default.js is not required.
//
// Also skip validation if this is a leaf segment (no child routes).
let is_inside_catchall = app_page.is_catchall();
// Check if this is a leaf segment (no child routes).
// Leaf segments don't need default.js because there are no child routes
// that could cause the parallel slot to unmatch. For example:
// /repo-overview/@slot/page with no child routes - is_leaf_segment = true (skip
......@@ -1245,10 +1247,23 @@ async fn directory_tree_to_loader_tree_internal(
// This also handles route groups correctly by filtering them out.
let is_leaf_segment = !has_child_routes(directory_tree);
// Turbopack-specific: Check if the parallel slot has matching child routes.
// In webpack, this is checked implicitly via the two-phase processing:
// slots with content are processed first and skip validation in the second phase.
// In Turbopack's single-pass approach, we check directly if the slot has child
// routes. If the slot has child routes that match the parent's
// child routes, it can render content for those routes and doesn't
// need a default. For example:
// /parent/@slot/page + /parent/@slot/child + /parent/child - slot_has_children =
// true (skip validation) ✓ /parent/@slot/page + /parent/child (no
// @slot/child) - slot_has_children = false (require default) ✓
let slot_has_children = has_child_routes(subdirectory);
if key != "children"
&& subdirectory.modules.default.is_none()
&& !is_inside_catchall
&& !is_leaf_segment
&& !slot_has_children
{
missing_default_parallel_route_issue(
app_dir.clone(),
......@@ -1341,8 +1356,15 @@ async fn directory_tree_to_loader_tree_internal(
tree.parallel_routes.insert(
key.clone(),
default_route_tree(app_dir.clone(), global_metadata, app_page.clone(), default)
.await?,
default_route_tree(
app_dir.clone(),
global_metadata,
app_page.clone(),
default,
key.clone(),
for_app_path.clone(),
)
.await?,
);
}
}
......@@ -1352,8 +1374,10 @@ async fn directory_tree_to_loader_tree_internal(
tree = default_route_tree(
app_dir.clone(),
global_metadata,
app_page,
app_page.clone(),
modules.default.clone(),
rcstr!("children"),
for_app_path.clone(),
)
.await?;
} else {
......@@ -1365,8 +1389,10 @@ async fn directory_tree_to_loader_tree_internal(
default_route_tree(
app_dir.clone(),
global_metadata,
app_page,
app_page.clone(),
modules.default.clone(),
rcstr!("children"),
for_app_path.clone(),
)
.await?,
);
......@@ -1388,6 +1414,8 @@ async fn default_route_tree(
global_metadata: Vc<GlobalMetadata>,
app_page: AppPage,
default_component: Option<FileSystemPath>,
slot_name: RcStr,
for_app_path: AppPath,
) -> Result<AppPageLoaderTree> {
Ok(AppPageLoaderTree {
page: app_page.clone(),
......@@ -1399,13 +1427,16 @@ async fn default_route_tree(
..Default::default()
}
} else {
// default fallback component
let contains_interception = for_app_path.contains_interception();
let default_file = if contains_interception && slot_name == "children" {
"dist/client/components/builtin/default-null.js"
} else {
"dist/client/components/builtin/default.js"
};
AppDirModules {
default: Some(
get_next_package(app_dir)
.await?
.join("dist/client/components/builtin/default.js")?,
),
default: Some(get_next_package(app_dir).await?.join(default_file)?),
..Default::default()
}
},
......
......@@ -460,6 +460,18 @@ impl AppPath {
true
}
/// Returns true if ANY segment in the entire path is an interception route.
/// This is different from `is_intercepting()` which only checks the last
/// segment.
pub fn contains_interception(&self) -> bool {
self.iter().any(|segment| {
matches!(
segment,
PathSegment::Static(s) if s.starts_with("(.)") || s.starts_with("(..)") || s.starts_with("(...)")
)
})
}
}
impl Deref for AppPath {
......
......@@ -35,9 +35,11 @@ import {
import { getFilesInDir } from '../../../../lib/get-files-in-dir'
import type { PageExtensions } from '../../../page-extensions-type'
import { PARALLEL_ROUTE_DEFAULT_PATH } from '../../../../client/components/builtin/default'
import { PARALLEL_ROUTE_DEFAULT_NULL_PATH } from '../../../../client/components/builtin/default-null'
import type { Compilation } from 'webpack'
import { createAppRouteCode } from './create-app-route-code'
import { MissingDefaultParallelRouteError } from '../../../../shared/lib/errors/missing-default-parallel-route-error'
import { isInterceptionRouteAppPath } from '../../../../shared/lib/router/utils/interception-routes'
import { normalizePathSep } from '../../../../shared/lib/page-path/normalize-path-sep'
import { installBindings } from '../../../swc/install-bindings'
......@@ -558,7 +560,18 @@ async function createTreeCodeFromPath(
let defaultPath = await resolver(`${fullSegmentPath}/default`)
if (!defaultPath) {
if (adjacentParallelSegment === 'children') {
defaultPath = PARALLEL_ROUTE_DEFAULT_PATH
// When we host applications on Vercel, the status code affects the
// underlying behavior of the route, which when we are missing the
// children slot of an interception route, will yield a full 404
// response for the RSC request instead. For this reason, we expect
// that if a default file is missing when we're rendering an
// interception route, we instead always render null for the default
// slot to avoid the full 404 response.
if (isInterceptionRouteAppPath(page)) {
defaultPath = PARALLEL_ROUTE_DEFAULT_NULL_PATH
} else {
defaultPath = PARALLEL_ROUTE_DEFAULT_PATH
}
} else {
// Check if we're inside a catch-all route (i.e., the parallel route is a child
// of a catch-all segment). Only skip validation if the slot is UNDER a catch-all.
......
export const PARALLEL_ROUTE_DEFAULT_NULL_PATH =
'next/dist/client/components/builtin/default-null.js'
export default function ParallelRouteDefaultNull() {
return null
}
export default function Page() {
return (
<div>
<h3>Deeper page under explicit layout</h3>
<p>This page is nested under the explicit layout</p>
</div>
)
}
/**
* TEST CASE: Interception route WITH explicit layout.tsx but NO parallel routes
*
* This is the KEY test to determine if we need to check for:
* - Implicit layout (from parallel routes) OR
* - Explicit layout (layout.tsx file)
*/
export default function Layout({ children }: { children: React.ReactNode }) {
return (
<div>
<div>Explicit layout at (.)explicit-layout</div>
<div>
<div>children:</div>
{children}
</div>
</div>
)
}
export default function Page() {
return (
<div>
<strong>@sidebar content</strong>
</div>
)
}
/**
* TEST CASE 3: Interception route WITH parallel routes AND page.tsx
*
* Expected: NO default.tsx needed for children slot
* Reason: Has page.tsx which fills the children slot
* The @sidebar slot exists but children slot has content
*/
export default function Page() {
return (
<div>
<h3>✅ TEST CASE 3: Has both @sidebar AND page.tsx</h3>
<p>This level has @sidebar parallel route AND a page.tsx</p>
<p>The page.tsx fills the children slot.</p>
<p>NO default.tsx needed!</p>
</div>
)
}
/**
* TEST CASE 1: Interception route WITH page.tsx
*
* Expected: NO default.tsx needed
* Reason: Has a page at this level, so no children slot exists
*/
export default function Page() {
return (
<div>
<h3>✅ TEST CASE 1: Has page.tsx</h3>
<p>This interception route has a page.tsx at this level.</p>
<p>No children slot exists, so NO default.tsx needed!</p>
</div>
)
}
/**
* TEST CASE 2: Interception route WITHOUT parallel routes
*
* Expected: NO default.tsx needed at (.)no-parallel-routes level
* Reason: No @parallel routes exist, so no implicit layout created
* Only has a nested page, no children slot at root level
*/
export default function Page() {
return (
<div>
<h3>✅ TEST CASE 2: No parallel routes</h3>
<p>This is a nested page under (.)no-parallel-routes/deeper</p>
<p>No @sidebar or other parallel routes exist at parent level.</p>
<p>NO default.tsx needed at (.)no-parallel-routes!</p>
</div>
)
}
/**
* TEST CASE: Interception route WITHOUT parallel routes (just a page)
*
* Expected: Should work WITHOUT special null default handling
* Reason: No parallel routes at this level means no children slot structure
* The page itself is the content, no implicit layout needed
*/
export default function Page() {
return (
<div>
<h3>✅ Simple interception page (no parallel routes)</h3>
<p>This is just a regular page.tsx at the interception route level.</p>
<p>No @sidebar or other parallel routes.</p>
<p>Should NOT need null default logic!</p>
</div>
)
}
export default function Page() {
return <div>Intercepted test-nested sidebar/deeper</div>
}
export default function Page() {
return <div>Intercepted test-nested sidebar</div>
}
export default function Page() {
return <div>Panel at deeper level (deeper than marker)</div>
}
export default function Layout({
children,
sidebar,
}: {
children: React.ReactNode
sidebar: React.ReactNode
}) {
return (
<div>
<div>{sidebar}</div>
<div>{children}</div>
</div>
)
}
export default function Page() {
return <div>Regular explicit-layout/deeper route</div>
}
export default function Layout({ children }: { children: React.ReactNode }) {
return (
<div>
<div>Regular explicit-layout layout</div>
{children}
</div>
)
}
export default function Page() {
return <div>Sidebar for regular has-both route</div>
}
export default function Layout({
children,
sidebar,
}: {
children: React.ReactNode
sidebar: React.ReactNode
}) {
return (
<div>
<div>has-both layout</div>
<div>
<div>@sidebar:</div>
{sidebar}
</div>
<div>
<div>children:</div>
{children}
</div>
</div>
)
}
export default function Page() {
return <div>Regular has-both route</div>
}
export default function Page() {
return <div>Regular has-page route</div>
}
......@@ -5,8 +5,14 @@ export default function Layout(props: {
return (
<html>
<body>
<div id="children">{props.children}</div>
<div id="modal">{props.modal}</div>
<div id="children">
<div>CHILDREN SLOT:</div>
{props.children}
</div>
<div id="modal">
<div>MODAL SLOT:</div>
{props.modal}
</div>
</body>
</html>
)
......
export default function Page() {
return <div>Regular no-parallel-routes/deeper route</div>
}
......@@ -2,8 +2,88 @@ import Link from 'next/link'
export default function Page() {
return (
<div>
<Link href="/foo/1">Foo</Link> <Link href="/bar/1">Foo</Link>
<div id="children">
<section>
<h2>✅ Should Work WITHOUT default.tsx</h2>
<div>
<h3>Test Case 1a: Simple page (no parallel routes)</h3>
<Link href="/simple-page" style={{ color: 'blue', fontSize: '16px' }}>
→ /simple-page
</Link>
<p>
Interception route with just a page.tsx, no parallel routes at all.
</p>
</div>
<div>
<h3>Test Case 1b: Has page.tsx</h3>
<Link href="/has-page">→ /has-page</Link>
<p>
Interception route has page.tsx at root level. No children slot
exists.
</p>
</div>
<div>
<h3>Test Case 2: No parallel routes</h3>
<Link href="/no-parallel-routes/deeper">
→ /no-parallel-routes/deeper
</Link>
<p>No @parallel routes exist, so no implicit layout created.</p>
</div>
<div>
<h3>Test Case 3: Has both @sidebar AND page.tsx</h3>
<Link href="/has-both">→ /has-both</Link>
<p>
Has @sidebar parallel route BUT page.tsx fills the children slot.
</p>
</div>
</section>
<section>
<h2>🔬 Test Cases - Require Null Default Logic</h2>
<div>
<h3>Test Case 4a: Has @sidebar but NO page.tsx (implicit layout)</h3>
<Link href="/test-nested">→ /test-nested</Link>
<p>Has @sidebar (creates implicit layout) but NO page.tsx.</p>
<p>✓ Auto-uses null default (no explicit files needed)</p>
</div>
<div>
<h3>Test Case 4b: Has explicit layout.tsx but NO parallel routes</h3>
<Link href="/explicit-layout/deeper">→ /explicit-layout/deeper</Link>
<p>
Has explicit layout.tsx with children slot, but NO parallel routes
like @sidebar.
</p>
<p>
? Should it 404 or 200? This determines if we need to check for
explicit layouts!
</p>
</div>
</section>
<section>
<h2>Original Tests</h2>
<ul>
<li>
<Link href="/foo/1">/foo/1</Link>
</li>
<li>
<Link href="/bar/1">/bar/1</Link>
</li>
<li>
<Link href="/test-nested/deeper">/test-nested/deeper</Link>
</li>
<li>
<Link href="/generate-static-params/foo">
/generate-static-params/foo
</Link>
</li>
</ul>
</section>
</div>
)
}
export default function Page() {
return (
<div>
<strong>Regular route @sidebar</strong>
</div>
)
}
/**
* TEST CASE 5: Regular route (NO interception) WITH parallel routes but NO page.tsx
*
* Expected: REQUIRES default.tsx at regular-route level OR will 404
* Reason: This is NOT an interception route, so missing children should 404
* Currently uses PARALLEL_ROUTE_DEFAULT_PATH (calls notFound())
* This is the CORRECT behavior for non-interception routes
*/
export default function Page() {
return (
<div>
<h3>❌ TEST CASE 5: Regular route without default.tsx</h3>
<p>This is deeper/page.tsx</p>
<p>Parent has @sidebar but no page.tsx or default.tsx</p>
<p>Should 404 when navigating directly to /regular-route!</p>
</div>
)
}
export default function Layout({
children,
sidebar,
}: {
children: React.ReactNode
sidebar: React.ReactNode
}) {
return (
<div>
<div>Regular Route Layout</div>
<div>{sidebar}</div>
<div>{children}</div>
</div>
)
}
export default function Page() {
return <div>Regular simple-page route</div>
}
export default function Page() {
return <div>Actual test-nested sidebar/deeper</div>
}
export default function Page() {
return <div>Actual test-nested sidebar</div>
}
export default function Page() {
return <div>Actual test-nested/deeper page</div>
}
export default function Layout({
children,
sidebar,
}: {
children: React.ReactNode
sidebar: React.ReactNode
}) {
return (
<div>
<div>test-nested layout</div>
<div>
<div>@sidebar slot:</div>
{sidebar}
</div>
<div>
<div>children slot:</div>
{children}
</div>
</div>
)
}
export default function Page() {
return <div>Actual test-nested page</div>
}
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