From 9b4a05503380f7d522aa284e025dc4be8d7ce941 Mon Sep 17 00:00:00 2001 From: Zack Tanner Date: Tue, 26 Mar 2024 14:41:55 -0700 Subject: [PATCH] prevent router markers from leaking (#63606) While looking into bugfixes related to router refreshing (the other PRs in this stack), I noticed there were situations where the `Next-URL` header would include `/children`, or where `page$` would be present in LoaderTree for a segment. This updates a few spots to prevent these markers from leaking into places they shouldn't, and shouldn't have any behavioral changes. Closes NEXT-2902 --- packages/next/src/build/webpack/loaders/next-app-loader.ts | 5 +++++ .../components/router-reducer/compute-changed-path.ts | 6 +++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/next/src/build/webpack/loaders/next-app-loader.ts b/packages/next/src/build/webpack/loaders/next-app-loader.ts index bc6b6deae5ca1..b2f4d6279adae 100644 --- a/packages/next/src/build/webpack/loaders/next-app-loader.ts +++ b/packages/next/src/build/webpack/loaders/next-app-loader.ts @@ -401,9 +401,14 @@ async function createTreeCodeFromPath( ? parallelSegment[0] : parallelSegment + // normalize the parallel segment key to remove any special markers that we inserted in the + // earlier logic (such as children$ and page$). These should never appear in the loader tree, and + // should instead be the corresponding segment keys (ie `__PAGE__`) or the `children` parallel route. parallelSegmentKey = parallelSegmentKey === PARALLEL_CHILDREN_SEGMENT ? 'children' + : parallelSegmentKey === PAGE_SEGMENT + ? PAGE_SEGMENT_KEY : parallelSegmentKey const normalizedParallelKey = normalizeParallelKey(parallelKey) diff --git a/packages/next/src/client/components/router-reducer/compute-changed-path.ts b/packages/next/src/client/components/router-reducer/compute-changed-path.ts index 1f0f760cd9edf..61495fcffa7a4 100644 --- a/packages/next/src/client/components/router-reducer/compute-changed-path.ts +++ b/packages/next/src/client/components/router-reducer/compute-changed-path.ts @@ -16,6 +16,10 @@ const removeLeadingSlash = (segment: string): string => { const segmentToPathname = (segment: Segment): string => { if (typeof segment === 'string') { + // 'children' is not a valid path -- it's technically a parallel route that corresponds with the current segment's page + // if we don't skip it, then the computed pathname might be something like `/children` which doesn't make sense. + if (segment === 'children') return '' + return segment } @@ -50,7 +54,7 @@ export function extractPathFromFlightRouterState( if (segment.startsWith(PAGE_SEGMENT_KEY)) return '' - const segments = [segment] + const segments = [segmentToPathname(segment)] const parallelRoutes = flightRouterState[1] ?? {} const childrenPath = parallelRoutes.children