Skip to content
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
2 changes: 1 addition & 1 deletion packages/next/src/client/app-dir/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
mountFormInstance,
unmountPrefetchableInstance,
} from '../components/links'
import { FetchStrategy } from '../components/segment-cache'
import { FetchStrategy } from '../components/segment-cache/types'

export type { FormProps }

Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/client/app-dir/link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { isLocalURL } from '../../shared/lib/router/utils/is-local-url'
import {
FetchStrategy,
type PrefetchTaskFetchStrategy,
} from '../components/segment-cache'
} from '../components/segment-cache/types'
import { errorOnce } from '../../shared/lib/utils/error-once'

type Url = string | UrlObject
Expand Down
4 changes: 2 additions & 2 deletions packages/next/src/client/components/app-router-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import { startTransition } from 'react'
import { isThenable } from '../../shared/lib/is-thenable'
import {
FetchStrategy,
prefetch as prefetchWithSegmentCache,
type PrefetchTaskFetchStrategy,
} from './segment-cache'
} from './segment-cache/types'
import { prefetch as prefetchWithSegmentCache } from './segment-cache/prefetch'
import { dispatchAppRouterAction } from './use-action-queue'
import { addBasePath } from '../add-base-path'
import { isExternalURL } from './app-router-utils'
Expand Down
10 changes: 5 additions & 5 deletions packages/next/src/client/components/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ import type { FlightRouterState } from '../../shared/lib/app-router-types'
import type { AppRouterInstance } from '../../shared/lib/app-router-context.shared-runtime'
import {
FetchStrategy,
isPrefetchTaskDirty,
type PrefetchTaskFetchStrategy,
} from './segment-cache'
import { createCacheKey } from './segment-cache'
PrefetchPriority,
} from './segment-cache/types'
import { createCacheKey } from './segment-cache/cache-key'
import {
type PrefetchTask,
PrefetchPriority,
schedulePrefetchTask as scheduleSegmentPrefetchTask,
cancelPrefetchTask,
reschedulePrefetchTask,
} from './segment-cache'
isPrefetchTaskDirty,
} from './segment-cache/scheduler'
import { startTransition } from 'react'

type LinkElement = HTMLAnchorElement | SVGAElement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {
getRenderedSearch,
urlToUrlWithoutFlightMarker,
} from '../../route-params'
import type { NormalizedSearch } from '../segment-cache'
import type { NormalizedSearch } from '../segment-cache/cache-key'

const createFromReadableStream =
createFromReadableStreamBrowser as (typeof import('react-server-dom-webpack/client.browser'))['createFromReadableStream']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import { handleMutable } from '../handle-mutable'

import {
navigate as navigateUsingSegmentCache,
NavigationResultTag,
type NavigationResult,
getStaleTimeMs,
} from '../../segment-cache'
} from '../../segment-cache/navigation'
import { NavigationResultTag } from '../../segment-cache/types'
import { getStaleTimeMs } from '../../segment-cache/cache'

// These values are set by `define-env-plugin` (based on `nextConfig.experimental.staleTimes`)
// and default to 5 minutes (static) / 0 seconds (dynamic)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { createEmptyCacheNode } from '../../app-router'
import { handleSegmentMismatch } from '../handle-segment-mismatch'
import { hasInterceptionRouteInCurrentTree } from './has-interception-route-in-current-tree'
import { refreshInactiveParallelSegments } from '../refetch-inactive-parallel-segments'
import { revalidateEntireCache } from '../../segment-cache'
import { revalidateEntireCache } from '../../segment-cache/cache'

export function refreshReducer(
state: ReadonlyReducerState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import {
extractInfoFromServerReferenceId,
omitUnusedArgs,
} from '../../../../shared/lib/server-reference-info'
import { revalidateEntireCache } from '../../segment-cache'
import { revalidateEntireCache } from '../../segment-cache/cache'

const createFromFetch =
createFromFetchBrowser as (typeof import('react-server-dom-webpack/client.browser'))['createFromFetch']
Expand Down
151 changes: 0 additions & 151 deletions packages/next/src/client/components/segment-cache.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,17 @@ import {
DOC_PREFETCH_RANGE_HEADER_VALUE,
doesExportedHtmlMatchBuildId,
} from '../../../shared/lib/segment-cache/output-export-prefetch-encoding'
import { FetchStrategy, getStaleTimeMs } from '../segment-cache'
import { FetchStrategy } from './types'
import { createPromiseWithResolvers } from '../../../shared/lib/promise-with-resolvers'

/**
* Ensures a minimum stale time of 30s to avoid issues where the server sends a too
* short-lived stale time, which would prevent anything from being prefetched.
*/
export function getStaleTimeMs(staleTimeSeconds: number): number {
return Math.max(staleTimeSeconds, 30) * 1000
}

// A note on async/await when working in the prefetch cache:
//
// Most async operations in the prefetch cache should *not* use async/await,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
} from './cache'
import { createCacheKey } from './cache-key'
import { addSearchParamsIfPageSegment } from '../../../shared/lib/segment'
import { NavigationResultTag } from '../segment-cache'
import { NavigationResultTag } from './types'

type MPANavigationResult = {
tag: NavigationResultTag.MPA
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import type { FlightRouterState } from '../../../shared/lib/app-router-types'
import { createPrefetchURL } from '../app-router-utils'
import { createCacheKey } from './cache-key'
import { schedulePrefetchTask } from './scheduler'
import {
PrefetchPriority,
type PrefetchTaskFetchStrategy,
} from '../segment-cache'
import { PrefetchPriority, type PrefetchTaskFetchStrategy } from './types'

/**
* Entrypoint for prefetching a URL into the Segment Cache.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ import { createCacheKey } from './cache-key'
import {
FetchStrategy,
type PrefetchTaskFetchStrategy,
getCurrentCacheVersion,
PrefetchPriority,
} from '../segment-cache'
} from './types'
import { getCurrentCacheVersion } from './cache'
import {
addSearchParamsIfPageSegment,
PAGE_SEGMENT_KEY,
Expand Down
53 changes: 53 additions & 0 deletions packages/next/src/client/components/segment-cache/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Shared types and constants for the Segment Cache.
*/

export const enum NavigationResultTag {
MPA,
Success,
NoOp,
Async,
}

/**
* The priority of the prefetch task. Higher numbers are higher priority.
*/
export const enum PrefetchPriority {
/**
* Assigned to the most recently hovered/touched link. Special network
* bandwidth is reserved for this task only. There's only ever one Intent-
* priority task at a time; when a new Intent task is scheduled, the previous
* one is bumped down to Default.
*/
Intent = 2,
/**
* The default priority for prefetch tasks.
*/
Default = 1,
/**
* Assigned to tasks when they spawn non-blocking background work, like
* revalidating a partially cached entry to see if more data is available.
*/
Background = 0,
}

export const enum FetchStrategy {
// Deliberately ordered so we can easily compare two segments
// and determine if one segment is "more specific" than another
// (i.e. if it's likely that it contains more data)
LoadingBoundary = 0,
PPR = 1,
PPRRuntime = 2,
Full = 3,
}

/**
* A subset of fetch strategies used for prefetch tasks.
* A prefetch task can't know if it should use `PPR` or `LoadingBoundary`
* until we complete the initial tree prefetch request, so we use `PPR` to signal both cases
* and adjust it based on the route when actually fetching.
* */
export type PrefetchTaskFetchStrategy =
| FetchStrategy.PPR
| FetchStrategy.PPRRuntime
| FetchStrategy.Full
2 changes: 1 addition & 1 deletion packages/next/src/client/flight-data-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
InitialRSCPayload,
} from '../shared/lib/app-router-types'
import { PAGE_SEGMENT_KEY } from '../shared/lib/segment'
import type { NormalizedSearch } from './components/segment-cache'
import type { NormalizedSearch } from './components/segment-cache/cache-key'
import {
getCacheKeyForDynamicParam,
parseDynamicParamFromURLPart,
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/client/route-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import type {
NormalizedPathname,
NormalizedSearch,
} from './components/segment-cache'
} from './components/segment-cache/cache-key'
import type { RSCResponse } from './components/router-reducer/fetch-server-response'
import type { ParsedUrlQuery } from 'querystring'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PAGE_SEGMENT_KEY } from '../segment'
import type { Segment as FlightRouterStateSegment } from '../app-router-types'
import type { NormalizedPathname } from '../../../client/components/segment-cache'
import type { NormalizedPathname } from '../../../client/components/segment-cache/cache-key'

// TypeScript trick to simulate opaque types, like in Flow.
type Opaque<K, T> = T & { __brand: K }
Expand Down
Loading