Skip to content

Commit 4498f95

Browse files
authored
Convert secondary fetchServerResponse params into options object (#67526)
This is in preparation for adding another option in #67527.
1 parent 7673889 commit 4498f95

File tree

7 files changed

+62
-50
lines changed

7 files changed

+62
-50
lines changed

packages/next/src/client/components/layout-router.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,11 @@ function InnerLayoutRouter({
406406
const includeNextUrl = hasInterceptionRouteInCurrentTree(fullTree)
407407
childNode.lazyData = lazyData = fetchServerResponse(
408408
new URL(url, location.origin),
409-
refetchTree,
410-
includeNextUrl ? context.nextUrl : null,
411-
buildId
409+
{
410+
flightRouterState: refetchTree,
411+
nextUrl: includeNextUrl ? context.nextUrl : null,
412+
buildId,
413+
}
412414
).then((serverResponse) => {
413415
startTransition(() => {
414416
changeByServerResponse({

packages/next/src/client/components/router-reducer/fetch-server-response.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ import { callServer } from '../../app-call-server'
2929
import { PrefetchKind } from './router-reducer-types'
3030
import { hexHash } from '../../../shared/lib/hash'
3131

32+
export interface FetchServerResponseOptions {
33+
readonly flightRouterState: FlightRouterState
34+
readonly nextUrl: string | null
35+
readonly buildId: string
36+
readonly prefetchKind?: PrefetchKind
37+
}
38+
3239
export type FetchServerResponseResult = [
3340
flightData: FlightData,
3441
canonicalUrlOverride: URL | undefined,
@@ -58,15 +65,15 @@ function doMpaNavigation(url: string): FetchServerResponseResult {
5865
}
5966

6067
/**
61-
* Fetch the flight data for the provided url. Takes in the current router state to decide what to render server-side.
68+
* Fetch the flight data for the provided url. Takes in the current router state
69+
* to decide what to render server-side.
6270
*/
6371
export async function fetchServerResponse(
6472
url: URL,
65-
flightRouterState: FlightRouterState,
66-
nextUrl: string | null,
67-
currentBuildId: string,
68-
prefetchKind?: PrefetchKind
73+
options: FetchServerResponseOptions
6974
): Promise<FetchServerResponseResult> {
75+
const { flightRouterState, nextUrl, buildId, prefetchKind } = options
76+
7077
const headers: {
7178
[RSC_HEADER]: '1'
7279
[NEXT_ROUTER_STATE_TREE_HEADER]: string
@@ -169,14 +176,10 @@ export async function fetchServerResponse(
169176
}
170177

171178
// Handle the `fetch` readable stream that can be unwrapped by `React.use`.
172-
const [buildId, flightData]: NextFlightResponse = await createFromFetch(
173-
Promise.resolve(res),
174-
{
175-
callServer,
176-
}
177-
)
179+
const [buildIdFromResponse, flightData]: NextFlightResponse =
180+
await createFromFetch(Promise.resolve(res), { callServer })
178181

179-
if (currentBuildId !== buildId) {
182+
if (buildId !== buildIdFromResponse) {
180183
return doMpaNavigation(res.url)
181184
}
182185

packages/next/src/client/components/router-reducer/prefetch-cache-utils.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -195,19 +195,22 @@ function createLazyPrefetchEntry({
195195
// initiates the fetch request for the prefetch and attaches a listener
196196
// to the promise to update the prefetch cache entry when the promise resolves (if necessary)
197197
const data = prefetchQueue.enqueue(() =>
198-
fetchServerResponse(url, tree, nextUrl, buildId, kind).then(
199-
(prefetchResponse) => {
200-
// TODO: `fetchServerResponse` should be more tighly coupled to these prefetch cache operations
201-
// to avoid drift between this cache key prefixing logic
202-
// (which is currently directly influenced by the server response)
203-
const [, , , intercepted] = prefetchResponse
204-
if (intercepted) {
205-
prefixExistingPrefetchCacheEntry({ url, nextUrl, prefetchCache })
206-
}
207-
208-
return prefetchResponse
198+
fetchServerResponse(url, {
199+
flightRouterState: tree,
200+
nextUrl,
201+
buildId,
202+
prefetchKind: kind,
203+
}).then((prefetchResponse) => {
204+
// TODO: `fetchServerResponse` should be more tighly coupled to these prefetch cache operations
205+
// to avoid drift between this cache key prefixing logic
206+
// (which is currently directly influenced by the server response)
207+
const [, , , intercepted] = prefetchResponse
208+
if (intercepted) {
209+
prefixExistingPrefetchCacheEntry({ url, nextUrl, prefetchCache })
209210
}
210-
)
211+
212+
return prefetchResponse
213+
})
211214
)
212215

213216
const prefetchEntry = {

packages/next/src/client/components/router-reducer/reducers/fast-refresh-reducer.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,11 @@ function fastRefreshReducerImpl(
3434

3535
// TODO-APP: verify that `href` is not an external url.
3636
// Fetch data from the root of the tree.
37-
cache.lazyData = fetchServerResponse(
38-
new URL(href, origin),
39-
[state.tree[0], state.tree[1], state.tree[2], 'refetch'],
40-
includeNextUrl ? state.nextUrl : null,
41-
state.buildId
42-
)
37+
cache.lazyData = fetchServerResponse(new URL(href, origin), {
38+
flightRouterState: [state.tree[0], state.tree[1], state.tree[2], 'refetch'],
39+
nextUrl: includeNextUrl ? state.nextUrl : null,
40+
buildId: state.buildId,
41+
})
4342

4443
return cache.lazyData.then(
4544
([flightData, canonicalUrlOverride]) => {

packages/next/src/client/components/router-reducer/reducers/navigate-reducer.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -452,12 +452,11 @@ function navigateReducer_PPR(
452452
// to the lazy fetching mechanism in that case.)
453453
listenForDynamicRequest(
454454
task,
455-
fetchServerResponse(
456-
url,
457-
currentTree,
458-
state.nextUrl,
459-
state.buildId
460-
)
455+
fetchServerResponse(url, {
456+
flightRouterState: currentTree,
457+
nextUrl: state.nextUrl,
458+
buildId: state.buildId,
459+
})
461460
)
462461

463462
mutable.cache = newCache

packages/next/src/client/components/router-reducer/reducers/refresh-reducer.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,16 @@ export function refreshReducer(
3737

3838
// TODO-APP: verify that `href` is not an external url.
3939
// Fetch data from the root of the tree.
40-
cache.lazyData = fetchServerResponse(
41-
new URL(href, origin),
42-
[currentTree[0], currentTree[1], currentTree[2], 'refetch'],
43-
includeNextUrl ? state.nextUrl : null,
44-
state.buildId
45-
)
40+
cache.lazyData = fetchServerResponse(new URL(href, origin), {
41+
flightRouterState: [
42+
currentTree[0],
43+
currentTree[1],
44+
currentTree[2],
45+
'refetch',
46+
],
47+
nextUrl: includeNextUrl ? state.nextUrl : null,
48+
buildId: state.buildId,
49+
})
4650

4751
return cache.lazyData.then(
4852
async ([flightData, canonicalUrlOverride]) => {

packages/next/src/client/components/router-reducer/refetch-inactive-parallel-segments.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,13 @@ async function refreshInactiveParallelSegmentsImpl({
6464
// independently on their own cache nodes, and `applyFlightData` will copy anything it doesn't care about from the existing cache.
6565
const fetchPromise = fetchServerResponse(
6666
new URL(refetchPath, location.origin),
67-
// refetch from the root of the updated tree, otherwise it will be scoped to the current segment
68-
// and might not contain the data we need to patch in interception route data (such as dynamic params from a previous segment)
69-
[rootTree[0], rootTree[1], rootTree[2], 'refetch'],
70-
includeNextUrl ? state.nextUrl : null,
71-
state.buildId
67+
{
68+
// refetch from the root of the updated tree, otherwise it will be scoped to the current segment
69+
// and might not contain the data we need to patch in interception route data (such as dynamic params from a previous segment)
70+
flightRouterState: [rootTree[0], rootTree[1], rootTree[2], 'refetch'],
71+
nextUrl: includeNextUrl ? state.nextUrl : null,
72+
buildId: state.buildId,
73+
}
7274
).then((fetchResponse) => {
7375
const flightData = fetchResponse[0]
7476
if (typeof flightData !== 'string') {

0 commit comments

Comments
 (0)