| | import type { FlightRouterState } from '../../server/app-render/types' |
| | import type { AppRouterInstance } from '../../shared/lib/app-router-context.shared-runtime' |
| | import { getCurrentAppRouterState } from './app-router-instance' |
| | import { createPrefetchURL } from './app-router' |
| | import { |
| | FetchStrategy, |
| | isPrefetchTaskDirty, |
| | type PrefetchTaskFetchStrategy, |
| | } from './segment-cache' |
| | import { createCacheKey } from './segment-cache' |
| | import { |
| | type PrefetchTask, |
| | PrefetchPriority, |
| | schedulePrefetchTask as scheduleSegmentPrefetchTask, |
| | cancelPrefetchTask, |
| | reschedulePrefetchTask, |
| | } from './segment-cache' |
| | import { startTransition } from 'react' |
| | import { PrefetchKind } from './router-reducer/router-reducer-types' |
| |
|
| | type LinkElement = HTMLAnchorElement | SVGAElement |
| |
|
| | type Element = LinkElement | HTMLFormElement |
| |
|
| | |
| | |
| | type LinkOrFormInstanceShared = { |
| | router: AppRouterInstance |
| | fetchStrategy: PrefetchTaskFetchStrategy |
| |
|
| | isVisible: boolean |
| |
|
| | |
| | |
| | |
| | prefetchTask: PrefetchTask | null |
| | } |
| |
|
| | export type FormInstance = LinkOrFormInstanceShared & { |
| | prefetchHref: string |
| | setOptimisticLinkStatus: null |
| | } |
| |
|
| | type PrefetchableLinkInstance = LinkOrFormInstanceShared & { |
| | prefetchHref: string |
| | setOptimisticLinkStatus: (status: { pending: boolean }) => void |
| | } |
| |
|
| | type NonPrefetchableLinkInstance = LinkOrFormInstanceShared & { |
| | prefetchHref: null |
| | setOptimisticLinkStatus: (status: { pending: boolean }) => void |
| | } |
| |
|
| | type PrefetchableInstance = PrefetchableLinkInstance | FormInstance |
| |
|
| | export type LinkInstance = |
| | | PrefetchableLinkInstance |
| | | NonPrefetchableLinkInstance |
| |
|
| | |
| | |
| | let linkForMostRecentNavigation: LinkInstance | null = null |
| |
|
| | |
| | export const PENDING_LINK_STATUS = { pending: true } |
| |
|
| | |
| | export const IDLE_LINK_STATUS = { pending: false } |
| |
|
| | |
| | |
| | |
| | |
| | export function setLinkForCurrentNavigation(link: LinkInstance | null) { |
| | startTransition(() => { |
| | linkForMostRecentNavigation?.setOptimisticLinkStatus(IDLE_LINK_STATUS) |
| | link?.setOptimisticLinkStatus(PENDING_LINK_STATUS) |
| | linkForMostRecentNavigation = link |
| | }) |
| | } |
| |
|
| | |
| | export function unmountLinkForCurrentNavigation(link: LinkInstance) { |
| | if (linkForMostRecentNavigation === link) { |
| | linkForMostRecentNavigation = null |
| | } |
| | } |
| |
|
| | |
| | |
| | const prefetchable: |
| | | WeakMap<Element, PrefetchableInstance> |
| | | Map<Element, PrefetchableInstance> = |
| | typeof WeakMap === 'function' ? new WeakMap() : new Map() |
| |
|
| | |
| | |
| | |
| | |
| | const prefetchableAndVisible: Set<PrefetchableInstance> = new Set() |
| |
|
| | |
| | const observer: IntersectionObserver | null = |
| | typeof IntersectionObserver === 'function' |
| | ? new IntersectionObserver(handleIntersect, { |
| | rootMargin: '200px', |
| | }) |
| | : null |
| |
|
| | function observeVisibility(element: Element, instance: PrefetchableInstance) { |
| | const existingInstance = prefetchable.get(element) |
| | if (existingInstance !== undefined) { |
| | |
| | |
| | |
| | unmountPrefetchableInstance(element) |
| | } |
| | |
| | prefetchable.set(element, instance) |
| | if (observer !== null) { |
| | observer.observe(element) |
| | } |
| | } |
| |
|
| | function coercePrefetchableUrl(href: string): URL | null { |
| | try { |
| | return createPrefetchURL(href) |
| | } catch { |
| | |
| | |
| | |
| | |
| | |
| | const reportErrorFn = |
| | typeof reportError === 'function' ? reportError : console.error |
| | reportErrorFn( |
| | `Cannot prefetch '${href}' because it cannot be converted to a URL.` |
| | ) |
| | return null |
| | } |
| | } |
| |
|
| | export function mountLinkInstance( |
| | element: LinkElement, |
| | href: string, |
| | router: AppRouterInstance, |
| | fetchStrategy: PrefetchTaskFetchStrategy, |
| | prefetchEnabled: boolean, |
| | setOptimisticLinkStatus: (status: { pending: boolean }) => void |
| | ): LinkInstance { |
| | if (prefetchEnabled) { |
| | const prefetchURL = coercePrefetchableUrl(href) |
| | if (prefetchURL !== null) { |
| | const instance: PrefetchableLinkInstance = { |
| | router, |
| | fetchStrategy, |
| | isVisible: false, |
| | prefetchTask: null, |
| | prefetchHref: prefetchURL.href, |
| | setOptimisticLinkStatus, |
| | } |
| | |
| | |
| | observeVisibility(element, instance) |
| | return instance |
| | } |
| | } |
| | |
| | |
| | const instance: NonPrefetchableLinkInstance = { |
| | router, |
| | fetchStrategy, |
| | isVisible: false, |
| | prefetchTask: null, |
| | prefetchHref: null, |
| | setOptimisticLinkStatus, |
| | } |
| | return instance |
| | } |
| |
|
| | export function mountFormInstance( |
| | element: HTMLFormElement, |
| | href: string, |
| | router: AppRouterInstance, |
| | fetchStrategy: PrefetchTaskFetchStrategy |
| | ): void { |
| | const prefetchURL = coercePrefetchableUrl(href) |
| | if (prefetchURL === null) { |
| | |
| | |
| | |
| | |
| | return |
| | } |
| | const instance: FormInstance = { |
| | router, |
| | fetchStrategy, |
| | isVisible: false, |
| | prefetchTask: null, |
| | prefetchHref: prefetchURL.href, |
| | setOptimisticLinkStatus: null, |
| | } |
| | observeVisibility(element, instance) |
| | } |
| |
|
| | export function unmountPrefetchableInstance(element: Element) { |
| | const instance = prefetchable.get(element) |
| | if (instance !== undefined) { |
| | prefetchable.delete(element) |
| | prefetchableAndVisible.delete(instance) |
| | const prefetchTask = instance.prefetchTask |
| | if (prefetchTask !== null) { |
| | cancelPrefetchTask(prefetchTask) |
| | } |
| | } |
| | if (observer !== null) { |
| | observer.unobserve(element) |
| | } |
| | } |
| |
|
| | function handleIntersect(entries: Array<IntersectionObserverEntry>) { |
| | for (const entry of entries) { |
| | |
| | |
| | |
| | const isVisible = entry.intersectionRatio > 0 |
| | onLinkVisibilityChanged(entry.target as HTMLAnchorElement, isVisible) |
| | } |
| | } |
| |
|
| | export function onLinkVisibilityChanged(element: Element, isVisible: boolean) { |
| | if (process.env.NODE_ENV !== 'production') { |
| | |
| | |
| | |
| | return |
| | } |
| |
|
| | const instance = prefetchable.get(element) |
| | if (instance === undefined) { |
| | return |
| | } |
| |
|
| | instance.isVisible = isVisible |
| | if (isVisible) { |
| | prefetchableAndVisible.add(instance) |
| | } else { |
| | prefetchableAndVisible.delete(instance) |
| | } |
| | rescheduleLinkPrefetch(instance, PrefetchPriority.Default) |
| | } |
| |
|
| | export function onNavigationIntent( |
| | element: HTMLAnchorElement | SVGAElement, |
| | unstable_upgradeToDynamicPrefetch: boolean |
| | ) { |
| | const instance = prefetchable.get(element) |
| | if (instance === undefined) { |
| | return |
| | } |
| | |
| | if (instance !== undefined) { |
| | if ( |
| | process.env.__NEXT_DYNAMIC_ON_HOVER && |
| | unstable_upgradeToDynamicPrefetch |
| | ) { |
| | |
| | instance.fetchStrategy = FetchStrategy.Full |
| | } |
| | rescheduleLinkPrefetch(instance, PrefetchPriority.Intent) |
| | } |
| | } |
| |
|
| | function rescheduleLinkPrefetch( |
| | instance: PrefetchableInstance, |
| | priority: PrefetchPriority.Default | PrefetchPriority.Intent |
| | ) { |
| | const existingPrefetchTask = instance.prefetchTask |
| |
|
| | if (!instance.isVisible) { |
| | |
| | |
| | if (existingPrefetchTask !== null) { |
| | cancelPrefetchTask(existingPrefetchTask) |
| | } |
| | |
| | |
| | |
| | |
| | return |
| | } |
| |
|
| | if (!process.env.__NEXT_CLIENT_SEGMENT_CACHE) { |
| | |
| | |
| | prefetchWithOldCacheImplementation(instance) |
| | return |
| | } |
| |
|
| | const appRouterState = getCurrentAppRouterState() |
| | if (appRouterState !== null) { |
| | const treeAtTimeOfPrefetch = appRouterState.tree |
| | if (existingPrefetchTask === null) { |
| | |
| | const nextUrl = appRouterState.nextUrl |
| | const cacheKey = createCacheKey(instance.prefetchHref, nextUrl) |
| | instance.prefetchTask = scheduleSegmentPrefetchTask( |
| | cacheKey, |
| | treeAtTimeOfPrefetch, |
| | instance.fetchStrategy, |
| | priority, |
| | null |
| | ) |
| | } else { |
| | |
| | |
| | reschedulePrefetchTask( |
| | existingPrefetchTask, |
| | treeAtTimeOfPrefetch, |
| | instance.fetchStrategy, |
| | priority |
| | ) |
| | } |
| | } |
| | } |
| |
|
| | export function pingVisibleLinks( |
| | nextUrl: string | null, |
| | tree: FlightRouterState |
| | ) { |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | for (const instance of prefetchableAndVisible) { |
| | const task = instance.prefetchTask |
| | if (task !== null && !isPrefetchTaskDirty(task, nextUrl, tree)) { |
| | |
| | |
| | continue |
| | } |
| | |
| | |
| | if (task !== null) { |
| | cancelPrefetchTask(task) |
| | } |
| | const cacheKey = createCacheKey(instance.prefetchHref, nextUrl) |
| | instance.prefetchTask = scheduleSegmentPrefetchTask( |
| | cacheKey, |
| | tree, |
| | instance.fetchStrategy, |
| | PrefetchPriority.Default, |
| | null |
| | ) |
| | } |
| | } |
| |
|
| | function prefetchWithOldCacheImplementation(instance: PrefetchableInstance) { |
| | |
| | if (typeof window === 'undefined') { |
| | return |
| | } |
| |
|
| | const doPrefetch = async () => { |
| | |
| | |
| |
|
| | let prefetchKind: PrefetchKind |
| | switch (instance.fetchStrategy) { |
| | case FetchStrategy.PPR: { |
| | prefetchKind = PrefetchKind.AUTO |
| | break |
| | } |
| | case FetchStrategy.Full: { |
| | prefetchKind = PrefetchKind.FULL |
| | break |
| | } |
| | default: { |
| | instance.fetchStrategy satisfies never |
| | |
| | prefetchKind = undefined! |
| | } |
| | } |
| |
|
| | return instance.router.prefetch(instance.prefetchHref, { |
| | kind: prefetchKind, |
| | }) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | doPrefetch().catch((err) => { |
| | if (process.env.NODE_ENV !== 'production') { |
| | |
| | throw err |
| | } |
| | }) |
| | } |
| |
|