Commit 2f3e930d authored by Jimmy Lai's avatar Jimmy Lai

fix: trigger not-found on client-side nav when children slot uses built-in default (#79352)

parent 4a2e8e7f
......@@ -459,7 +459,16 @@ function updateCacheNodeOnNavigation(
// was stashed in the history entry as-is.
freshness !== FreshnessPolicy.HistoryTraversal &&
newSegmentChild === DEFAULT_SEGMENT_KEY &&
oldSegmentChild !== DEFAULT_SEGMENT_KEY
oldSegmentChild !== DEFAULT_SEGMENT_KEY &&
// Do not reuse the previously active segment if this __DEFAULT__
// segment uses the built-in default component that calls notFound().
// The built-in default means there is no matching page or
// user-provided default.tsx for this slot. Reusing old content would
// hide the 404 that should be triggered by the built-in default.
!(
newRouteTreeChild.prefetchHints &
PrefetchHint.IsBuiltinNotFoundDefault
)
) {
// This is a "default" segment. These are never sent by the server during
// a soft navigation; instead, the client reuses whatever segment was
......
......@@ -5,8 +5,12 @@ import {
type PrefetchHints,
} from '../../shared/lib/app-router-types'
import type { GetDynamicParamFromSegment } from './app-render'
import { addSearchParamsIfPageSegment } from '../../shared/lib/segment'
import {
DEFAULT_SEGMENT_KEY,
addSearchParamsIfPageSegment,
} from '../../shared/lib/segment'
import type { AppSegmentConfig } from '../../build/segment-config/app/app-segment-config'
import { PARALLEL_ROUTE_DEFAULT_PATH } from '../../client/components/builtin/default'
async function createFlightRouterStateFromLoaderTreeImpl(
loaderTree: LoaderTree,
......@@ -94,6 +98,26 @@ async function createFlightRouterStateFromLoaderTreeImpl(
prefetchHints |= PrefetchHint.SegmentHasLoadingBoundary
}
// Check if this __DEFAULT__ segment uses the built-in default component
// that calls notFound(). This tells the client router not to reuse the
// previous page content when navigating to a URL with no matching page.
// The path comparison uses endsWith because the loader tree stores
// absolute filesystem paths while PARALLEL_ROUTE_DEFAULT_PATH is a
// package-relative path (e.g., "next/dist/client/components/builtin/default.js").
if (segment === DEFAULT_SEGMENT_KEY) {
const { defaultPage } = loaderTree[2]
if (
defaultPage !== undefined &&
typeof defaultPage[1] === 'string' &&
defaultPage[1].endsWith(
// Strip the leading "next/" from the constant to match absolute paths.
PARALLEL_ROUTE_DEFAULT_PATH.slice('next/'.length)
)
) {
prefetchHints |= PrefetchHint.IsBuiltinNotFoundDefault
}
}
const children: FlightRouterState[1] = {}
for (const parallelRouteKey in parallelRoutes) {
// Look up the child hint node by parallel route key, traversing the
......
......@@ -209,6 +209,12 @@ export const enum PrefetchHint {
// (HasRuntimePrefetch). Propagates upward so the root reflects the
// entire subtree.
SubtreeHasRuntimePrefetch = 0b100000000000,
// This __DEFAULT__ segment uses the built-in default component that
// calls notFound(). Set so the client can distinguish between a
// user-provided default.tsx (where old content should be reused during
// navigation) and the built-in not-found default (where a 404 should
// be shown).
IsBuiltinNotFoundDefault = 0b1000000000000,
}
/**
......
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