Skip to content

Commit

Permalink
Move flightRouterState into FetchServerResponseOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
unstubbable committed Jul 12, 2024
1 parent e6ac61d commit 8a06418
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 19 deletions.
7 changes: 5 additions & 2 deletions packages/next/src/client/components/layout-router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -53,10 +54,9 @@ function doMpaNavigation(url: string): FetchServerResponseResult {
*/
export async function fetchServerResponse(
url: URL,
flightRouterState: FlightRouterState,
options: FetchServerResponseOptions
): Promise<FetchServerResponseResult> {
const { nextUrl, buildId, prefetchKind } = options
const { flightRouterState, nextUrl, buildId, prefetchKind } = options

const headers: {
[RSC_HEADER]: '1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down

0 comments on commit 8a06418

Please sign in to comment.