Commit 2cb6a3d1 authored by Josh Story's avatar Josh Story

Refactor `findNavigationsToValidate` to use URL-based navigation boundaries

The navigation parent tracking in `findNavigationsToValidate` previously treated every layout as a navigation boundary, including route group layouts. But route group layouts don't contribute to the URL — they always mount together with the next URL-contributing segment. A user can never navigate "into" a route group layout independently, so treating it as a separate boundary created redundant validation tasks.

This changes `navigationParents.push()` to only include layouts at URL-contributing segments (not route groups, `__PAGE__`, or `__DEFAULT__`). Route group layouts still have their configs processed — if a route group layout has `unstable_instant`, a validation task is created from the parents above it — but they don't advance the boundary for deeper segments. This also adds a guard that skips task creation when `navigationParents` is empty, which naturally deduplicates the case where both a route group layout and the URL-contributing layout below it have configs.
parent 18b94630
......@@ -42,7 +42,12 @@ import { createDebugChannel } from '../debug-channel-server'
import { createFromNodeStream } from 'react-server-dom-webpack/client'
// eslint-disable-next-line import/no-extraneous-dependencies
import { renderToReadableStream } from 'react-server-dom-webpack/server'
import { addSearchParamsIfPageSegment } from '../../../shared/lib/segment'
import {
addSearchParamsIfPageSegment,
isGroupSegment,
PAGE_SEGMENT_KEY,
DEFAULT_SEGMENT_KEY,
} from '../../../shared/lib/segment'
import type { NextParsedUrlQuery } from '../../request-meta'
const filterStackFrame =
......@@ -104,6 +109,9 @@ export async function findNavigationsToValidate(
type ValidationTask = { target: SegmentPath; parents: SegmentPath[] }
const validationTasks: ValidationTask[] = []
// navigationParents tracks the URL-contributing layouts we've passed
// through. When we find a config, we create a task with these as the
// potential "FROM" points for the navigation.
let navigationParents: SegmentPath[] = []
const segmentsWithInstantConfigs: SegmentPath[] = []
......@@ -122,6 +130,16 @@ export async function findNavigationsToValidate(
return query ? addSearchParamsIfPageSegment(segment, query) : segment
}
function isURLContributingSegment(rawSegment: string): boolean {
// Route groups, __PAGE__, and __DEFAULT__ don't contribute to the URL.
// Everything else represents a real URL segment boundary.
return (
!isGroupSegment(rawSegment) &&
rawSegment !== PAGE_SEGMENT_KEY &&
rawSegment !== DEFAULT_SEGMENT_KEY
)
}
async function visit(
loaderTree: LoaderTree,
parentPath: SegmentPath | null,
......@@ -133,6 +151,7 @@ export async function findNavigationsToValidate(
const { mod: layoutOrPageMod, modType } =
await getLayoutOrPageModule(loaderTree)
const rawSegment = loaderTree[0]
const segment = getSegmentFromLoaderTree(loaderTree)
const segmentPath =
parentPath === null
......@@ -156,8 +175,8 @@ export async function findNavigationsToValidate(
}
if (isInsideParallelSlot) {
// For now, we ignore parallel routes for purposes of finding configs to validate
// and finding shared layout parents.
// For now, we ignore parallel routes for purposes of finding configs
// to validate and finding shared layout parents.
if (instantConfig !== null) {
console.error(
`${conventionPath}: \`unstable_instant\` validation is not fully implemented for parallel routes yet.`
......@@ -186,20 +205,28 @@ export async function findNavigationsToValidate(
throw error
}
const task: ValidationTask = {
target: segmentPath,
parents: navigationParents,
if (navigationParents.length > 0) {
const task: ValidationTask = {
target: segmentPath,
parents: navigationParents,
}
validationTasks.push(task)
navigationParents = []
}
validationTasks.push(task)
navigationParents = []
}
}
navigationParents.push(segmentPath)
// Only URL-contributing layouts advance the navigation boundary.
// Route group layouts are part of the same navigation as their
// parent URL segment — they always mount together.
if (isURLContributingSegment(rawSegment)) {
navigationParents.push(segmentPath)
}
} else if (modType === 'page') {
if (instantConfig !== null) {
if (instantConfig === false) {
navigationParents = []
} else {
} else if (navigationParents.length > 0) {
const task: ValidationTask = {
target: segmentPath,
parents: navigationParents,
......
......@@ -159,6 +159,25 @@ export default async function Page() {
</li>
</ul>
<h2>Route Groups</h2>
<ul>
<li>
<DebugLinks href="/suspense-in-root/static/route-group-config-only" />
</li>
<li>
<DebugLinks href="/suspense-in-root/static/route-group-config-and-segment-config" />
</li>
<li>
<DebugLinks href="/suspense-in-root/static/route-group-segment-config-only" />
</li>
<li>
<DebugLinks href="/suspense-in-root/static/route-group-config-with-deeper-segment/inner" />
</li>
<li>
<DebugLinks href="/suspense-in-root/static/route-group-deeper-segment-config/inner" />
</li>
</ul>
<h2>Disable Validation</h2>
<ul>
<li>
......
import { ReactNode } from 'react'
export const unstable_instant = { prefetch: 'static' }
export default function GroupLayout({ children }: { children: ReactNode }) {
return (
<div>
<em>
This is a route group layout that also has unstable_instant (static)
</em>
<hr />
{children}
</div>
)
}
import { cookies } from 'next/headers'
export default async function Page() {
await cookies()
return (
<main>
<p>This page awaits cookies() without Suspense</p>
</main>
)
}
import { ReactNode } from 'react'
export const unstable_instant = { prefetch: 'static' }
export default function Layout({ children }: { children: ReactNode }) {
return (
<div>
<em>This is a URL-contributing layout with unstable_instant (static)</em>
<hr />
{children}
</div>
)
}
import { ReactNode } from 'react'
export const unstable_instant = { prefetch: 'static' }
export default function GroupLayout({ children }: { children: ReactNode }) {
return (
<div>
<em>This is a route group layout with unstable_instant (static)</em>
<hr />
{children}
</div>
)
}
import { cookies } from 'next/headers'
export default async function Page() {
await cookies()
return (
<main>
<p>This page awaits cookies() without Suspense</p>
</main>
)
}
import { ReactNode } from 'react'
export default function InnerLayout({ children }: { children: ReactNode }) {
return (
<div>
<em>This is a URL-contributing layout with no config</em>
<hr />
{children}
</div>
)
}
import { cookies } from 'next/headers'
export default async function Page() {
await cookies()
return (
<main>
<p>This page awaits cookies() without Suspense</p>
</main>
)
}
import { ReactNode } from 'react'
export const unstable_instant = { prefetch: 'static' }
export default function GroupLayout({ children }: { children: ReactNode }) {
return (
<div>
<em>This is a route group layout with unstable_instant (static)</em>
<hr />
{children}
</div>
)
}
import { ReactNode } from 'react'
export const unstable_instant = { prefetch: 'static' }
export default function InnerLayout({ children }: { children: ReactNode }) {
return (
<div>
<em>This is a URL-contributing layout with unstable_instant (static)</em>
<hr />
{children}
</div>
)
}
import { cookies } from 'next/headers'
export default async function Page() {
await cookies()
return (
<main>
<p>This page awaits cookies() without Suspense</p>
</main>
)
}
import { ReactNode } from 'react'
export default function GroupLayout({ children }: { children: ReactNode }) {
return (
<div>
<em>This is a route group layout with no config</em>
<hr />
{children}
</div>
)
}
import { ReactNode } from 'react'
export default function GroupLayout({ children }: { children: ReactNode }) {
return (
<div>
<em>This is a route group layout with no config</em>
<hr />
{children}
</div>
)
}
import { cookies } from 'next/headers'
export default async function Page() {
await cookies()
return (
<main>
<p>This page awaits cookies() without Suspense</p>
</main>
)
}
import { ReactNode } from 'react'
export const unstable_instant = { prefetch: 'static' }
export default function Layout({ children }: { children: ReactNode }) {
return (
<div>
<em>This is a URL-contributing layout with unstable_instant (static)</em>
<hr />
{children}
</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