Skip to content

Commit

Permalink
Convert tertiary fetchServerResponse params into options object
Browse files Browse the repository at this point in the history
This is in preparation for adding another option.
  • Loading branch information
unstubbable committed Jul 8, 2024
1 parent 72c0e35 commit 07f0cba
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 37 deletions.
3 changes: 1 addition & 2 deletions packages/next/src/client/components/layout-router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,7 @@ function InnerLayoutRouter({
childNode.lazyData = lazyData = fetchServerResponse(
new URL(url, location.origin),
refetchTree,
includeNextUrl ? context.nextUrl : null,
buildId
{ nextUrl: includeNextUrl ? context.nextUrl : null, buildId }
).then((serverResponse) => {
startTransition(() => {
changeByServerResponse({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ import { callServer } from '../../app-call-server'
import { PrefetchKind } from './router-reducer-types'
import { hexHash } from '../../../shared/lib/hash'

export interface FetchServerResponseOptions {
readonly nextUrl: string | null
readonly buildId: string
readonly prefetchKind?: PrefetchKind
}

export type FetchServerResponseResult = [
flightData: FlightData,
canonicalUrlOverride: URL | undefined,
Expand All @@ -42,15 +48,16 @@ function doMpaNavigation(url: string): FetchServerResponseResult {
}

/**
* Fetch the flight data for the provided url. Takes in the current router state to decide what to render server-side.
* Fetch the flight data for the provided url. Takes in the current router state
* to decide what to render server-side.
*/
export async function fetchServerResponse(
url: URL,
flightRouterState: FlightRouterState,
nextUrl: string | null,
currentBuildId: string,
prefetchKind?: PrefetchKind
options: FetchServerResponseOptions
): Promise<FetchServerResponseResult> {
const { nextUrl, buildId, prefetchKind } = options

const headers: {
[RSC_HEADER]: '1'
[NEXT_ROUTER_STATE_TREE_HEADER]: string
Expand Down Expand Up @@ -153,14 +160,10 @@ export async function fetchServerResponse(
}

// Handle the `fetch` readable stream that can be unwrapped by `React.use`.
const [buildId, flightData]: NextFlightResponse = await createFromFetch(
Promise.resolve(res),
{
callServer,
}
)
const [buildIdFromResponse, flightData]: NextFlightResponse =
await createFromFetch(Promise.resolve(res), { callServer })

if (currentBuildId !== buildId) {
if (buildId !== buildIdFromResponse) {
return doMpaNavigation(res.url)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,19 +195,21 @@ 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, nextUrl, buildId, kind).then(
(prefetchResponse) => {
// TODO: `fetchServerResponse` should be more tighly coupled to these prefetch cache operations
// to avoid drift between this cache key prefixing logic
// (which is currently directly influenced by the server response)
const [, , , intercepted] = prefetchResponse
if (intercepted) {
prefixExistingPrefetchCacheEntry({ url, nextUrl, prefetchCache })
}

return prefetchResponse
fetchServerResponse(url, tree, {
nextUrl,
buildId,
prefetchKind: kind,
}).then((prefetchResponse) => {
// TODO: `fetchServerResponse` should be more tighly coupled to these prefetch cache operations
// to avoid drift between this cache key prefixing logic
// (which is currently directly influenced by the server response)
const [, , , intercepted] = prefetchResponse
if (intercepted) {
prefixExistingPrefetchCacheEntry({ url, nextUrl, prefetchCache })
}
)

return prefetchResponse
})
)

const prefetchEntry = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ function fastRefreshReducerImpl(
cache.lazyData = fetchServerResponse(
new URL(href, origin),
[state.tree[0], state.tree[1], state.tree[2], 'refetch'],
includeNextUrl ? state.nextUrl : null,
state.buildId
{ nextUrl: includeNextUrl ? state.nextUrl : null, buildId: state.buildId }
)

return cache.lazyData.then(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,12 +449,10 @@ function navigateReducer_PPR(
// to the lazy fetching mechanism in that case.)
listenForDynamicRequest(
task,
fetchServerResponse(
url,
currentTree,
state.nextUrl,
state.buildId
)
fetchServerResponse(url, currentTree, {
nextUrl: state.nextUrl,
buildId: state.buildId,
})
)

mutable.cache = newCache
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ export function refreshReducer(
cache.lazyData = fetchServerResponse(
new URL(href, origin),
[currentTree[0], currentTree[1], currentTree[2], 'refetch'],
includeNextUrl ? state.nextUrl : null,
state.buildId
{ nextUrl: includeNextUrl ? state.nextUrl : null, buildId: state.buildId }
)

return cache.lazyData.then(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ async function refreshInactiveParallelSegmentsImpl({
// 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'],
includeNextUrl ? state.nextUrl : null,
state.buildId
{
nextUrl: includeNextUrl ? state.nextUrl : null,
buildId: state.buildId,
}
).then((fetchResponse) => {
const flightData = fetchResponse[0]
if (typeof flightData !== 'string') {
Expand Down

0 comments on commit 07f0cba

Please sign in to comment.