From 8a064185a8a96717c9f7b7474e176a49d09b669b Mon Sep 17 00:00:00 2001 From: Hendrik Liebau Date: Tue, 9 Jul 2024 15:42:08 +0200 Subject: [PATCH] Move `flightRouterState` into `FetchServerResponseOptions` --- .../next/src/client/components/layout-router.tsx | 7 +++++-- .../router-reducer/fetch-server-response.ts | 4 ++-- .../router-reducer/prefetch-cache-utils.ts | 3 ++- .../reducers/fast-refresh-reducer.ts | 10 +++++----- .../router-reducer/reducers/navigate-reducer.ts | 3 ++- .../router-reducer/reducers/refresh-reducer.ts | 15 ++++++++++----- .../refetch-inactive-parallel-segments.ts | 6 +++--- 7 files changed, 29 insertions(+), 19 deletions(-) diff --git a/packages/next/src/client/components/layout-router.tsx b/packages/next/src/client/components/layout-router.tsx index 70f0d38fec72c..defe766fd6fb8 100644 --- a/packages/next/src/client/components/layout-router.tsx +++ b/packages/next/src/client/components/layout-router.tsx @@ -406,8 +406,11 @@ function InnerLayoutRouter({ const includeNextUrl = hasInterceptionRouteInCurrentTree(fullTree) childNode.lazyData = lazyData = fetchServerResponse( new URL(url, location.origin), - refetchTree, - { nextUrl: includeNextUrl ? context.nextUrl : null, buildId } + { + flightRouterState: refetchTree, + nextUrl: includeNextUrl ? context.nextUrl : null, + buildId, + } ).then((serverResponse) => { startTransition(() => { changeByServerResponse({ diff --git a/packages/next/src/client/components/router-reducer/fetch-server-response.ts b/packages/next/src/client/components/router-reducer/fetch-server-response.ts index 469947fdd68f5..df3f19b85da98 100644 --- a/packages/next/src/client/components/router-reducer/fetch-server-response.ts +++ b/packages/next/src/client/components/router-reducer/fetch-server-response.ts @@ -31,6 +31,7 @@ import { PrefetchKind } from './router-reducer-types' import { hexHash } from '../../../shared/lib/hash' export interface FetchServerResponseOptions { + readonly flightRouterState: FlightRouterState readonly nextUrl: string | null readonly buildId: string readonly prefetchKind?: PrefetchKind @@ -53,10 +54,9 @@ function doMpaNavigation(url: string): FetchServerResponseResult { */ export async function fetchServerResponse( url: URL, - flightRouterState: FlightRouterState, options: FetchServerResponseOptions ): Promise { - const { nextUrl, buildId, prefetchKind } = options + const { flightRouterState, nextUrl, buildId, prefetchKind } = options const headers: { [RSC_HEADER]: '1' diff --git a/packages/next/src/client/components/router-reducer/prefetch-cache-utils.ts b/packages/next/src/client/components/router-reducer/prefetch-cache-utils.ts index e4732db68af75..45d5607d963e6 100644 --- a/packages/next/src/client/components/router-reducer/prefetch-cache-utils.ts +++ b/packages/next/src/client/components/router-reducer/prefetch-cache-utils.ts @@ -195,7 +195,8 @@ function createLazyPrefetchEntry({ // initiates the fetch request for the prefetch and attaches a listener // to the promise to update the prefetch cache entry when the promise resolves (if necessary) const data = prefetchQueue.enqueue(() => - fetchServerResponse(url, tree, { + fetchServerResponse(url, { + flightRouterState: tree, nextUrl, buildId, prefetchKind: kind, diff --git a/packages/next/src/client/components/router-reducer/reducers/fast-refresh-reducer.ts b/packages/next/src/client/components/router-reducer/reducers/fast-refresh-reducer.ts index cb7d5508c21d3..9ce93a5f7fa34 100644 --- a/packages/next/src/client/components/router-reducer/reducers/fast-refresh-reducer.ts +++ b/packages/next/src/client/components/router-reducer/reducers/fast-refresh-reducer.ts @@ -34,11 +34,11 @@ function fastRefreshReducerImpl( // TODO-APP: verify that `href` is not an external url. // Fetch data from the root of the tree. - cache.lazyData = fetchServerResponse( - new URL(href, origin), - [state.tree[0], state.tree[1], state.tree[2], 'refetch'], - { nextUrl: includeNextUrl ? state.nextUrl : null, buildId: state.buildId } - ) + cache.lazyData = fetchServerResponse(new URL(href, origin), { + flightRouterState: [state.tree[0], state.tree[1], state.tree[2], 'refetch'], + nextUrl: includeNextUrl ? state.nextUrl : null, + buildId: state.buildId, + }) return cache.lazyData.then( ([flightData, canonicalUrlOverride]) => { diff --git a/packages/next/src/client/components/router-reducer/reducers/navigate-reducer.ts b/packages/next/src/client/components/router-reducer/reducers/navigate-reducer.ts index 6f7118a2a56f2..4d3bc0c718f72 100644 --- a/packages/next/src/client/components/router-reducer/reducers/navigate-reducer.ts +++ b/packages/next/src/client/components/router-reducer/reducers/navigate-reducer.ts @@ -452,7 +452,8 @@ function navigateReducer_PPR( // to the lazy fetching mechanism in that case.) listenForDynamicRequest( task, - fetchServerResponse(url, currentTree, { + fetchServerResponse(url, { + flightRouterState: currentTree, nextUrl: state.nextUrl, buildId: state.buildId, }) diff --git a/packages/next/src/client/components/router-reducer/reducers/refresh-reducer.ts b/packages/next/src/client/components/router-reducer/reducers/refresh-reducer.ts index 62e3fb7d8a011..d156be25a878b 100644 --- a/packages/next/src/client/components/router-reducer/reducers/refresh-reducer.ts +++ b/packages/next/src/client/components/router-reducer/reducers/refresh-reducer.ts @@ -37,11 +37,16 @@ export function refreshReducer( // TODO-APP: verify that `href` is not an external url. // Fetch data from the root of the tree. - cache.lazyData = fetchServerResponse( - new URL(href, origin), - [currentTree[0], currentTree[1], currentTree[2], 'refetch'], - { nextUrl: includeNextUrl ? state.nextUrl : null, buildId: state.buildId } - ) + cache.lazyData = fetchServerResponse(new URL(href, origin), { + flightRouterState: [ + currentTree[0], + currentTree[1], + currentTree[2], + 'refetch', + ], + nextUrl: includeNextUrl ? state.nextUrl : null, + buildId: state.buildId, + }) return cache.lazyData.then( async ([flightData, canonicalUrlOverride]) => { diff --git a/packages/next/src/client/components/router-reducer/refetch-inactive-parallel-segments.ts b/packages/next/src/client/components/router-reducer/refetch-inactive-parallel-segments.ts index 745a8186f01dd..70190c1c8a8fa 100644 --- a/packages/next/src/client/components/router-reducer/refetch-inactive-parallel-segments.ts +++ b/packages/next/src/client/components/router-reducer/refetch-inactive-parallel-segments.ts @@ -64,10 +64,10 @@ async function refreshInactiveParallelSegmentsImpl({ // independently on their own cache nodes, and `applyFlightData` will copy anything it doesn't care about from the existing cache. const fetchPromise = fetchServerResponse( new URL(refetchPath, location.origin), - // refetch from the root of the updated tree, otherwise it will be scoped to the current segment - // and might not contain the data we need to patch in interception route data (such as dynamic params from a previous segment) - [rootTree[0], rootTree[1], rootTree[2], 'refetch'], { + // refetch from the root of the updated tree, otherwise it will be scoped to the current segment + // and might not contain the data we need to patch in interception route data (such as dynamic params from a previous segment) + flightRouterState: [rootTree[0], rootTree[1], rootTree[2], 'refetch'], nextUrl: includeNextUrl ? state.nextUrl : null, buildId: state.buildId, }