Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Segment Cache] Support <Link prefetch={true}> #74172

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/next/src/client/components/app-router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,12 @@ function Router({
? // Unlike the old implementation, the Segment Cache doesn't store its
// data in the router reducer state; it writes into a global mutable
// cache. So we don't need to dispatch an action.
(href) =>
(href, options) =>
prefetchWithSegmentCache(
href,
actionQueue.state.nextUrl,
actionQueue.state.tree
actionQueue.state.tree,
options?.kind === PrefetchKind.FULL
)
: (href, options) => {
// Use the old prefetch implementation.
Expand Down
12 changes: 10 additions & 2 deletions packages/next/src/client/components/segment-cache/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export type RouteCacheEntry =

export const enum FetchStrategy {
PPR,
Full,
LoadingBoundary,
}

Expand Down Expand Up @@ -1005,24 +1006,31 @@ export async function fetchSegmentOnCacheMiss(
}
}

export async function fetchSegmentPrefetchesForPPRDisabledRoute(
export async function fetchSegmentPrefetchesUsingDynamicRequest(
task: PrefetchTask,
route: FulfilledRouteCacheEntry,
fetchStrategy: FetchStrategy,
dynamicRequestTree: FlightRouterState,
spawnedEntries: Map<string, PendingSegmentCacheEntry>
): Promise<PrefetchSubtaskResult<null> | null> {
const href = task.key.href
const nextUrl = task.key.nextUrl
const headers: RequestHeaders = {
[RSC_HEADER]: '1',
[NEXT_ROUTER_PREFETCH_HEADER]: '1',
[NEXT_ROUTER_STATE_TREE_HEADER]: encodeURIComponent(
JSON.stringify(dynamicRequestTree)
),
}
if (nextUrl !== null) {
headers[NEXT_URL] = nextUrl
}
// Only set the prefetch header if we're not doing a "full" prefetch. We
// omit the prefetch header from a full prefetch because it's essentially
// just a navigation request that happens ahead of time — it should include
// all the same data in the response.
if (fetchStrategy !== FetchStrategy.Full) {
headers[NEXT_ROUTER_PREFETCH_HEADER] = '1'
}
try {
const response = await fetchPrefetchResponse(href, headers)
if (!response || !response.ok || !response.body) {
Expand Down
11 changes: 9 additions & 2 deletions packages/next/src/client/components/segment-cache/prefetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,24 @@ import { schedulePrefetchTask } from './scheduler'
* Entrypoint for prefetching a URL into the Segment Cache.
* @param href - The URL to prefetch. Typically this will come from a <Link>,
* or router.prefetch. It must be validated before we attempt to prefetch it.
* @param nextUrl - A special header used by the server for interception routes.
* Roughly corresponds to the current URL.
* @param treeAtTimeOfPrefetch - The FlightRouterState at the time the prefetch
* was requested. This is only used when PPR is disabled.
* @param includeDynamicData - Whether to prefetch dynamic data, in addition to
* static data. This is used by <Link prefetch={true}>.
*/
export function prefetch(
href: string,
nextUrl: string | null,
treeAtTimeOfPrefetch: FlightRouterState
treeAtTimeOfPrefetch: FlightRouterState,
includeDynamicData: boolean
) {
const url = createPrefetchURL(href)
if (url === null) {
// This href should not be prefetched.
return
}
const cacheKey = createCacheKey(url.href, nextUrl)
schedulePrefetchTask(cacheKey, treeAtTimeOfPrefetch)
schedulePrefetchTask(cacheKey, treeAtTimeOfPrefetch, includeDynamicData)
}
Loading
Loading