Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2afe8f0

Browse files
authored
[Segment Cache] set fetchStrategy on segments from a dynamic request (#82059)
For non-PPR routes and full prefetches, we fetch multiple segments with one dynamic request. Then, we deconstruct the response into segments and create segment cache entries for them. However, when doing this, we weren't correctly setting the fetch strategy for these cache entries, leaving it at the default (`PPR`). The most correct way to model this is to simulate the flow that a normal segment prefetch would go through -- we create an empty segment, upgrade it to a pending one (which requires setting `fetchStrategy`) and finally fulfill it. I've adjusted `fulfillSegmentCacheEntry` to only accept pending segments so that it's not possible to have a fulfilled segment whose fetch strategy wasn't set, like we were doing before.
1 parent 75ce1d5 commit 2afe8f0

File tree

1 file changed

+23
-4
lines changed
  • packages/next/src/client/components/segment-cache-impl

1 file changed

+23
-4
lines changed

packages/next/src/client/components/segment-cache-impl/cache.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,7 @@ function fulfillRouteCacheEntry(
805805
}
806806

807807
function fulfillSegmentCacheEntry(
808-
segmentCacheEntry: EmptySegmentCacheEntry | PendingSegmentCacheEntry,
808+
segmentCacheEntry: PendingSegmentCacheEntry,
809809
rsc: React.ReactNode,
810810
loading: LoadingModuleData | Promise<LoadingModuleData>,
811811
staleAt: number,
@@ -1186,6 +1186,9 @@ export async function fetchRouteOnCacheMiss(
11861186
writeDynamicTreeResponseIntoCache(
11871187
Date.now(),
11881188
task,
1189+
// The non-PPR response format is what we'd get if we prefetched these segments
1190+
// using the LoadingBoundary fetch strategy, so mark their cache entries accordingly.
1191+
FetchStrategy.LoadingBoundary,
11891192
response,
11901193
serverData,
11911194
entry,
@@ -1347,7 +1350,7 @@ export async function fetchSegmentOnCacheMiss(
13471350
export async function fetchSegmentPrefetchesUsingDynamicRequest(
13481351
task: PrefetchTask,
13491352
route: FulfilledRouteCacheEntry,
1350-
fetchStrategy: FetchStrategy,
1353+
fetchStrategy: FetchStrategy.LoadingBoundary | FetchStrategy.Full,
13511354
dynamicRequestTree: FlightRouterState,
13521355
spawnedEntries: Map<string, PendingSegmentCacheEntry>
13531356
): Promise<PrefetchSubtaskResult<null> | null> {
@@ -1427,6 +1430,7 @@ export async function fetchSegmentPrefetchesUsingDynamicRequest(
14271430
fulfilledEntries = writeDynamicRenderResponseIntoCache(
14281431
Date.now(),
14291432
task,
1433+
fetchStrategy,
14301434
response,
14311435
serverData,
14321436
isResponsePartial,
@@ -1446,6 +1450,7 @@ export async function fetchSegmentPrefetchesUsingDynamicRequest(
14461450
function writeDynamicTreeResponseIntoCache(
14471451
now: number,
14481452
task: PrefetchTask,
1453+
fetchStrategy: FetchStrategy.LoadingBoundary | FetchStrategy.Full,
14491454
response: RSCResponse,
14501455
serverData: NavigationFlightResponse,
14511456
entry: PendingRouteCacheEntry,
@@ -1516,6 +1521,7 @@ function writeDynamicTreeResponseIntoCache(
15161521
writeDynamicRenderResponseIntoCache(
15171522
now,
15181523
task,
1524+
fetchStrategy,
15191525
response,
15201526
serverData,
15211527
isResponsePartial,
@@ -1542,6 +1548,7 @@ function rejectSegmentEntriesIfStillPending(
15421548
function writeDynamicRenderResponseIntoCache(
15431549
now: number,
15441550
task: PrefetchTask,
1551+
fetchStrategy: FetchStrategy.LoadingBoundary | FetchStrategy.Full,
15451552
response: RSCResponse,
15461553
serverData: NavigationFlightResponse,
15471554
isResponsePartial: boolean,
@@ -1600,6 +1607,7 @@ function writeDynamicRenderResponseIntoCache(
16001607
writeSeedDataIntoCache(
16011608
now,
16021609
task,
1610+
fetchStrategy,
16031611
route,
16041612
staleAt,
16051613
seedData,
@@ -1648,6 +1656,7 @@ function writeDynamicRenderResponseIntoCache(
16481656
function writeSeedDataIntoCache(
16491657
now: number,
16501658
task: PrefetchTask,
1659+
fetchStrategy: FetchStrategy.LoadingBoundary | FetchStrategy.Full,
16511660
route: FulfilledRouteCacheEntry,
16521661
staleAt: number,
16531662
seedData: CacheNodeSeedData,
@@ -1684,12 +1693,21 @@ function writeSeedDataIntoCache(
16841693
if (possiblyNewEntry.status === EntryStatus.Empty) {
16851694
// Confirmed this is a new entry. We can fulfill it.
16861695
const newEntry = possiblyNewEntry
1687-
fulfillSegmentCacheEntry(newEntry, rsc, loading, staleAt, isPartial)
1696+
fulfillSegmentCacheEntry(
1697+
upgradeToPendingSegment(newEntry, fetchStrategy),
1698+
rsc,
1699+
loading,
1700+
staleAt,
1701+
isPartial
1702+
)
16881703
} else {
16891704
// There was already an entry in the cache. But we may be able to
16901705
// replace it with the new one from the server.
16911706
const newEntry = fulfillSegmentCacheEntry(
1692-
createDetachedSegmentCacheEntry(staleAt),
1707+
upgradeToPendingSegment(
1708+
createDetachedSegmentCacheEntry(staleAt),
1709+
fetchStrategy
1710+
),
16931711
rsc,
16941712
loading,
16951713
staleAt,
@@ -1712,6 +1730,7 @@ function writeSeedDataIntoCache(
17121730
writeSeedDataIntoCache(
17131731
now,
17141732
task,
1733+
fetchStrategy,
17151734
route,
17161735
staleAt,
17171736
childSeedData,

0 commit comments

Comments
 (0)