Unverified Commit b98d8f83 authored by Andrew Clark's avatar Andrew Clark Committed by GitHub

[Segment Cache] Re-prefetch links on navigation (#76060)

The result of certain kinds of prefetches may depend on the current URL
at the time it is initiated. This is true if for links that are
intercepted, or links that are prefetched using a dynamic request (e.g.
`prefetch={true}`). So, whenever the location changes, we should
re-prefetch all the links using the updated value.

In most cases, this will not result in any new network requests — only
if the prefetch result actually varies on one of these inputs.

For similar reasons, we also re-prefetch links whenever the client cache
is revalidated by a Server Action.

As part of the implementation, we must be able to enumerate over all the
currently visible links. I've added a Set to track this.
parent 502e919e
...@@ -22,6 +22,8 @@ import { ...@@ -22,6 +22,8 @@ import {
import { getCurrentAppRouterState } from '../../shared/lib/router/action-queue' import { getCurrentAppRouterState } from '../../shared/lib/router/action-queue'
import { createCacheKey } from '../components/segment-cache/cache-key' import { createCacheKey } from '../components/segment-cache/cache-key'
import { createPrefetchURL } from '../components/app-router' import { createPrefetchURL } from '../components/app-router'
import type { FlightRouterState } from '../../server/app-render/types'
import { getCurrentCacheVersion } from '../components/segment-cache/cache'
type Url = string | UrlObject type Url = string | UrlObject
type RequiredKeys<T> = { type RequiredKeys<T> = {
...@@ -221,16 +223,25 @@ type LinkInstance = { ...@@ -221,16 +223,25 @@ type LinkInstance = {
// already completed. The same prefetch task object can be reused across // already completed. The same prefetch task object can be reused across
// multiple prefetches of the same link. // multiple prefetches of the same link.
prefetchTask: PrefetchTask | null prefetchTask: PrefetchTask | null
// The cache version at the time the task was initiated. This is used to
// determine if the cache was invalidated since the task was initiated.
cacheVersion: number
} }
// TODO: This is currently a WeakMap because it doesn't need to be enumerable, // Use a WeakMap to associate a Link instance with its DOM element. This is
// but eventually we'll want to be able to re-prefetch all the currently // used by the IntersectionObserver to track the link's visibility.
// visible links, e.g. after a revalidation or refresh.
const links: const links:
| WeakMap<HTMLAnchorElement | SVGAElement, LinkInstance> | WeakMap<HTMLAnchorElement | SVGAElement, LinkInstance>
| Map<Element, LinkInstance> = | Map<Element, LinkInstance> =
typeof WeakMap === 'function' ? new WeakMap() : new Map() typeof WeakMap === 'function' ? new WeakMap() : new Map()
// A Set of the currently visible links. We re-prefetch visible links after a
// cache invalidation, or when the current URL changes. It's a separate data
// structure from the WeakMap above because only the visible links need to
// be enumerated.
const visibleLinks: Set<LinkInstance> = new Set()
// A single IntersectionObserver instance shared by all <Link> components. // A single IntersectionObserver instance shared by all <Link> components.
const observer: IntersectionObserver | null = const observer: IntersectionObserver | null =
typeof IntersectionObserver === 'function' typeof IntersectionObserver === 'function'
...@@ -274,6 +285,7 @@ function mountLinkInstance( ...@@ -274,6 +285,7 @@ function mountLinkInstance(
isVisible: false, isVisible: false,
wasHoveredOrTouched: false, wasHoveredOrTouched: false,
prefetchTask: null, prefetchTask: null,
cacheVersion: -1,
} }
const existingInstance = links.get(element) const existingInstance = links.get(element)
if (existingInstance !== undefined) { if (existingInstance !== undefined) {
...@@ -288,10 +300,11 @@ function mountLinkInstance( ...@@ -288,10 +300,11 @@ function mountLinkInstance(
} }
} }
export function unmountLinkInstance(element: HTMLAnchorElement | SVGAElement) { function unmountLinkInstance(element: HTMLAnchorElement | SVGAElement) {
const instance = links.get(element) const instance = links.get(element)
if (instance !== undefined) { if (instance !== undefined) {
links.delete(element) links.delete(element)
visibleLinks.delete(instance)
const prefetchTask = instance.prefetchTask const prefetchTask = instance.prefetchTask
if (prefetchTask !== null) { if (prefetchTask !== null) {
cancelPrefetchTask(prefetchTask) cancelPrefetchTask(prefetchTask)
...@@ -329,6 +342,11 @@ function onLinkVisibilityChanged( ...@@ -329,6 +342,11 @@ function onLinkVisibilityChanged(
} }
instance.isVisible = isVisible instance.isVisible = isVisible
if (isVisible) {
visibleLinks.add(instance)
} else {
visibleLinks.delete(instance)
}
rescheduleLinkPrefetch(instance) rescheduleLinkPrefetch(instance)
} }
...@@ -389,6 +407,7 @@ function rescheduleLinkPrefetch(instance: LinkInstance) { ...@@ -389,6 +407,7 @@ function rescheduleLinkPrefetch(instance: LinkInstance) {
instance.kind === PrefetchKind.FULL, instance.kind === PrefetchKind.FULL,
priority priority
) )
instance.cacheVersion = getCurrentCacheVersion()
} }
} else { } else {
// We already have an old task object that we can reschedule. This is // We already have an old task object that we can reschedule. This is
...@@ -397,6 +416,49 @@ function rescheduleLinkPrefetch(instance: LinkInstance) { ...@@ -397,6 +416,49 @@ function rescheduleLinkPrefetch(instance: LinkInstance) {
} }
} }
export function pingVisibleLinks(
nextUrl: string | null,
tree: FlightRouterState
) {
// For each currently visible link, cancel the existing prefetch task (if it
// exists) and schedule a new one. This is effectively the same as if all the
// visible links left and then re-entered the viewport.
//
// This is called when the Next-Url or the base tree changes, since those
// may affect the result of a prefetch task. It's also called after a
// cache invalidation.
const currentCacheVersion = getCurrentCacheVersion()
for (const instance of visibleLinks) {
const task = instance.prefetchTask
if (
task !== null &&
instance.cacheVersion === currentCacheVersion &&
task.key.nextUrl === nextUrl &&
task.treeAtTimeOfPrefetch === tree
) {
// The cache has not been invalidated, and none of the inputs have
// changed. Bail out.
continue
}
// Something changed. Cancel the existing prefetch task and schedule a
// new one.
if (task !== null) {
cancelPrefetchTask(task)
}
const cacheKey = createCacheKey(instance.prefetchHref, nextUrl)
const priority = instance.wasHoveredOrTouched
? PrefetchPriority.Intent
: PrefetchPriority.Default
instance.prefetchTask = scheduleSegmentPrefetchTask(
cacheKey,
tree,
instance.kind === PrefetchKind.FULL,
priority
)
instance.cacheVersion = getCurrentCacheVersion()
}
}
function prefetchWithOldCacheImplementation(instance: LinkInstance) { function prefetchWithOldCacheImplementation(instance: LinkInstance) {
// This is the path used when the Segment Cache is not enabled. // This is the path used when the Segment Cache is not enabled.
if (typeof window === 'undefined') { if (typeof window === 'undefined') {
......
...@@ -62,6 +62,7 @@ import { prefetch as prefetchWithSegmentCache } from '../components/segment-cach ...@@ -62,6 +62,7 @@ import { prefetch as prefetchWithSegmentCache } from '../components/segment-cach
import { getRedirectTypeFromError, getURLFromRedirectError } from './redirect' import { getRedirectTypeFromError, getURLFromRedirectError } from './redirect'
import { isRedirectError, RedirectType } from './redirect-error' import { isRedirectError, RedirectType } from './redirect-error'
import { prefetchReducer } from './router-reducer/reducers/prefetch-reducer' import { prefetchReducer } from './router-reducer/reducers/prefetch-reducer'
import { pingVisibleLinks } from '../app-dir/link'
const globalMutable: { const globalMutable: {
pendingMpaPath?: string pendingMpaPath?: string
...@@ -142,6 +143,17 @@ function HistoryUpdater({ ...@@ -142,6 +143,17 @@ function HistoryUpdater({
window.history.replaceState(historyState, '', canonicalUrl) window.history.replaceState(historyState, '', canonicalUrl)
} }
}, [appRouterState]) }, [appRouterState])
useEffect(() => {
// The Next-Url and the base tree may affect the result of a prefetch
// task. Re-prefetch all visible links with the updated values. In most
// cases, this will not result in any new network requests, only if
// the prefetch result actually varies on one of these inputs.
if (process.env.__NEXT_CLIENT_SEGMENT_CACHE) {
pingVisibleLinks(appRouterState.nextUrl, appRouterState.tree)
}
}, [appRouterState.nextUrl, appRouterState.tree])
return null return null
} }
......
...@@ -123,7 +123,7 @@ export function refreshReducer( ...@@ -123,7 +123,7 @@ export function refreshReducer(
undefined undefined
) )
if (process.env.__NEXT_CLIENT_SEGMENT_CACHE) { if (process.env.__NEXT_CLIENT_SEGMENT_CACHE) {
revalidateEntireCache() revalidateEntireCache(state.nextUrl, newTree)
} else { } else {
mutable.prefetchCache = new Map() mutable.prefetchCache = new Map()
} }
......
...@@ -340,7 +340,7 @@ export function serverActionReducer( ...@@ -340,7 +340,7 @@ export function serverActionReducer(
mutable.cache = cache mutable.cache = cache
if (process.env.__NEXT_CLIENT_SEGMENT_CACHE) { if (process.env.__NEXT_CLIENT_SEGMENT_CACHE) {
revalidateEntireCache() revalidateEntireCache(state.nextUrl, newTree)
} else { } else {
mutable.prefetchCache = new Map() mutable.prefetchCache = new Map()
} }
......
...@@ -52,6 +52,7 @@ import type { ...@@ -52,6 +52,7 @@ import type {
} from '../../../server/app-render/types' } from '../../../server/app-render/types'
import { normalizeFlightData } from '../../flight-data-helpers' import { normalizeFlightData } from '../../flight-data-helpers'
import { STATIC_STALETIME_MS } from '../router-reducer/prefetch-cache-utils' import { STATIC_STALETIME_MS } from '../router-reducer/prefetch-cache-utils'
import { pingVisibleLinks } from '../../app-dir/link'
// A note on async/await when working in the prefetch cache: // A note on async/await when working in the prefetch cache:
// //
...@@ -238,13 +239,25 @@ let segmentCacheLru = createLRU<SegmentCacheEntry>( ...@@ -238,13 +239,25 @@ let segmentCacheLru = createLRU<SegmentCacheEntry>(
onSegmentLRUEviction onSegmentLRUEviction
) )
// Incrementing counter used to track cache invalidations.
let currentCacheVersion = 0
export function getCurrentCacheVersion(): number {
return currentCacheVersion
}
/** /**
* Used to clear the client prefetch cache when a server action calls * Used to clear the client prefetch cache when a server action calls
* revalidatePath or revalidateTag. Eventually we will support only clearing the * revalidatePath or revalidateTag. Eventually we will support only clearing the
* segments that were actually affected, but there's more work to be done on the * segments that were actually affected, but there's more work to be done on the
* server before the client is able to do this correctly. * server before the client is able to do this correctly.
*/ */
export function revalidateEntireCache() { export function revalidateEntireCache(
nextUrl: string | null,
tree: FlightRouterState
) {
currentCacheVersion++
// Clearing the cache also effectively rejects any pending requests, because // Clearing the cache also effectively rejects any pending requests, because
// when the response is received, it gets written into a cache entry that is // when the response is received, it gets written into a cache entry that is
// no longer reachable. // no longer reachable.
...@@ -254,6 +267,9 @@ export function revalidateEntireCache() { ...@@ -254,6 +267,9 @@ export function revalidateEntireCache() {
routeCacheLru = createLRU(maxRouteLruSize, onRouteLRUEviction) routeCacheLru = createLRU(maxRouteLruSize, onRouteLRUEviction)
segmentCacheMap = new Map() segmentCacheMap = new Map()
segmentCacheLru = createLRU(maxSegmentLruSize, onSegmentLRUEviction) segmentCacheLru = createLRU(maxSegmentLruSize, onSegmentLRUEviction)
// Prefetch all the currently visible links again, to re-fill the cache.
pingVisibleLinks(nextUrl, tree)
} }
export function readExactRouteCacheEntry( export function readExactRouteCacheEntry(
......
import { connection } from 'next/server'
import { Suspense } from 'react'
async function Content() {
await connection()
return <div id="page-a-content">Page A content</div>
}
export default async function PageA() {
return (
<Suspense fallback="Loading...">
<Content />
</Suspense>
)
}
import { connection } from 'next/server'
import { Suspense } from 'react'
async function Content() {
await connection()
return <div id="page-b-content">Page B content</div>
}
export default async function PageB() {
return (
<Suspense fallback="Loading...">
<Content />
</Suspense>
)
}
import { LinkAccordion } from '../../components/link-accordion'
export default function RefetchOnNewBaseTreeLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<>
<div style={{ backgroundColor: 'lightgray', padding: '1rem' }}>
<p>
This demonstrates what happens when a link is prefetched using{' '}
<code>{'prefetch={true}'}</code> and the URL changes. Next.js should
re-prefetch the link in case the delta between the base tree and the
target tree has changed.
</p>
<p>
Everything in this gray section is part of a shared layout. The links
below are prefetched using <code>{'prefetch={true}'}</code>. If the
first loaded page is "/refetch-on-new-base-tree/a", the prefetch for
this link will be empty, because there's no delta between the base
tree and the target tree.
</p>
<p>
However, if you then navigate to page B, we should re-prefetch the
link to A, because the delta between the base tree and the target tree
is now different.
</p>
<p>Test steps:</p>
<ul>
<li>Load "/refetch-on-new-base-tree/a" in the browser.</li>
<li>
Click the checkboxes to reveal the links. (These exist so the e2e
test can control the timing of the prefetch.)
</li>
<li>
Observe that the prefetch for page A is empty, i.e. the string "Page
A content" should not appear anywhere in the response.
</li>
<li>Click the link to page B to navigate away.</li>
<li>
Check the network tab to confirm that a new prefetch for page A was
requested.
</li>
<li>Click the link to page A</li>
<li>
Observe that no new request was made when navigating to page A,
because it was fully prefetched.
</li>
</ul>
<LinkAccordion prefetch={true} href="/refetch-on-new-base-tree/a">
Page A
</LinkAccordion>
<LinkAccordion prefetch={true} href="/refetch-on-new-base-tree/b">
Page B
</LinkAccordion>
</div>
<div>{children}</div>
</>
)
}
import { redirect } from 'next/navigation'
export default function Page() {
redirect('/refetch-on-new-base-tree/a')
}
...@@ -3,7 +3,15 @@ ...@@ -3,7 +3,15 @@
import Link from 'next/link' import Link from 'next/link'
import { useState } from 'react' import { useState } from 'react'
export function LinkAccordion({ href, children }) { export function LinkAccordion({
href,
children,
prefetch,
}: {
href: string
children: React.ReactNode
prefetch?: boolean
}) {
const [isVisible, setIsVisible] = useState(false) const [isVisible, setIsVisible] = useState(false)
return ( return (
<> <>
...@@ -14,9 +22,11 @@ export function LinkAccordion({ href, children }) { ...@@ -14,9 +22,11 @@ export function LinkAccordion({ href, children }) {
data-link-accordion={href} data-link-accordion={href}
/> />
{isVisible ? ( {isVisible ? (
<Link href={href}>{children}</Link> <Link href={href} prefetch={prefetch}>
{children}
</Link>
) : ( ) : (
`${children} (link is hidden)` <>{children} (link is hidden)</>
)} )}
</> </>
) )
......
...@@ -13,23 +13,24 @@ describe('segment cache (revalidation)', () => { ...@@ -13,23 +13,24 @@ describe('segment cache (revalidation)', () => {
let port = -1 let port = -1
let server let server
let pendingRequests = new Map() let dataVersions = new Map()
let TestLog = createTestLog() let TestLog = createTestLog()
let next let next
beforeAll(async () => { beforeAll(async () => {
port = await findPort() port = await findPort()
let isFinishedBuilding = false
server = createTestDataServer(async (key, res) => { server = createTestDataServer(async (key, res) => {
if (!isFinishedBuilding) { const currentVersion = dataVersions.get(key)
res.resolve('Initial value during build for: ' + key)
return // Increment the version number each time to track how often the
} // server renders.
if (pendingRequests.has(key)) { const nextVersion = currentVersion === undefined ? 1 : currentVersion + 1
throw new Error('Request already pending for: ' + key) dataVersions.set(key, nextVersion)
}
pendingRequests.set(key, res) // Append the version number to the response
const response = `${key} [${nextVersion}]`
TestLog.log('REQUEST: ' + key) TestLog.log('REQUEST: ' + key)
res.resolve(response)
}) })
server.listen(port) server.listen(port)
...@@ -37,11 +38,10 @@ describe('segment cache (revalidation)', () => { ...@@ -37,11 +38,10 @@ describe('segment cache (revalidation)', () => {
files: __dirname, files: __dirname,
env: { TEST_DATA_SERVICE_URL: `http://localhost:${port}` }, env: { TEST_DATA_SERVICE_URL: `http://localhost:${port}` },
}) })
isFinishedBuilding = true
}) })
afterEach(async () => { beforeEach(async () => {
pendingRequests = new Map() dataVersions = new Map()
TestLog = createTestLog() TestLog = createTestLog()
}) })
...@@ -59,7 +59,7 @@ describe('segment cache (revalidation)', () => { ...@@ -59,7 +59,7 @@ describe('segment cache (revalidation)', () => {
}) })
const linkVisibilityToggle = await browser.elementByCss( const linkVisibilityToggle = await browser.elementByCss(
'input[type="checkbox"]' 'input[data-link-accordion="/greeting"]'
) )
// Reveal the link the target page to trigger a prefetch // Reveal the link the target page to trigger a prefetch
...@@ -68,37 +68,23 @@ describe('segment cache (revalidation)', () => { ...@@ -68,37 +68,23 @@ describe('segment cache (revalidation)', () => {
await linkVisibilityToggle.click() await linkVisibilityToggle.click()
}, },
{ {
includes: 'Greeting', includes: 'random-greeting',
} }
) )
// Hide the link so we can reveal it again later to trigger another
// prefetch task
await linkVisibilityToggle.click()
// Perform an action that calls revalidatePath. This should cause the // Perform an action that calls revalidatePath. This should cause the
// corresponding entry to be evicted from the client cache. // corresponding entry to be evicted from the client cache, and a new
await act(async () => { // prefetch to be requested.
const revalidateByPath = await browser.elementById('revalidate-by-path')
await revalidateByPath.click()
})
await act( await act(
async () => { async () => {
// Reveal the link const revalidateByPath = await browser.elementById('revalidate-by-path')
await linkVisibilityToggle.click() await revalidateByPath.click()
// Because the corresponding entry was evicted from the cache, this
// should trigger a new prefetch.
await TestLog.waitFor(['REQUEST: random-greeting'])
// Fulfill the prefetch request.
await pendingRequests.get('random-greeting').resolve('yo!')
}, },
{ {
includes: 'yo!', includes: 'random-greeting [1]',
} }
) )
TestLog.assert(['REQUEST: random-greeting'])
// Navigate to the target page. // Navigate to the target page.
await act(async () => { await act(async () => {
...@@ -107,7 +93,7 @@ describe('segment cache (revalidation)', () => { ...@@ -107,7 +93,7 @@ describe('segment cache (revalidation)', () => {
// Navigation should finish immedately because the page is // Navigation should finish immedately because the page is
// fully prefetched. // fully prefetched.
const greeting = await browser.elementById('greeting') const greeting = await browser.elementById('greeting')
expect(await greeting.innerHTML()).toBe('yo!') expect(await greeting.innerHTML()).toBe('random-greeting [1]')
}, 'no-requests') }, 'no-requests')
}) })
...@@ -120,46 +106,32 @@ describe('segment cache (revalidation)', () => { ...@@ -120,46 +106,32 @@ describe('segment cache (revalidation)', () => {
}) })
const linkVisibilityToggle = await browser.elementByCss( const linkVisibilityToggle = await browser.elementByCss(
'input[type="checkbox"]' 'input[data-link-accordion="/greeting"]'
) )
// Reveal the link the target page to trigger a prefetch // Reveal the link the target page to trigger a prefetch.
await act( await act(
async () => { async () => {
await linkVisibilityToggle.click() await linkVisibilityToggle.click()
}, },
{ {
includes: 'Greeting', includes: 'random-greeting',
} }
) )
// Hide the link so we can reveal it again later to trigger another
// prefetch task
await linkVisibilityToggle.click()
// Perform an action that calls revalidateTag. This should cause the // Perform an action that calls revalidateTag. This should cause the
// corresponding entry to be evicted from the client cache. // corresponding entry to be evicted from the client cache, and a new
await act(async () => { // prefetch to be requested.
const revalidateByPath = await browser.elementById('revalidate-by-tag')
await revalidateByPath.click()
})
await act( await act(
async () => { async () => {
// Reveal the link const revalidateByTag = await browser.elementById('revalidate-by-tag')
await linkVisibilityToggle.click() await revalidateByTag.click()
// Because the corresponding entry was evicted from the cache, this
// should trigger a new prefetch.
await TestLog.waitFor(['REQUEST: random-greeting'])
// Fulfill the prefetch request.
await pendingRequests.get('random-greeting').resolve('hey!')
}, },
{ {
includes: 'hey!', includes: 'random-greeting [1]',
} }
) )
TestLog.assert(['REQUEST: random-greeting'])
// Navigate to the target page. // Navigate to the target page.
await act(async () => { await act(async () => {
...@@ -168,7 +140,75 @@ describe('segment cache (revalidation)', () => { ...@@ -168,7 +140,75 @@ describe('segment cache (revalidation)', () => {
// Navigation should finish immedately because the page is // Navigation should finish immedately because the page is
// fully prefetched. // fully prefetched.
const greeting = await browser.elementById('greeting') const greeting = await browser.elementById('greeting')
expect(await greeting.innerHTML()).toBe('hey!') expect(await greeting.innerHTML()).toBe('random-greeting [1]')
}, 'no-requests') }, 'no-requests')
}) })
it('re-fetch visible links after a navigation, if needed', async () => {
let act: ReturnType<typeof createRouterAct>
const browser = await next.browser('/refetch-on-new-base-tree/a', {
beforePageLoad(page: Playwright.Page) {
act = createRouterAct(page)
},
})
const linkALinkVisibilityToggle = await browser.elementByCss(
'input[data-link-accordion="/refetch-on-new-base-tree/a"]'
)
const linkBLinkVisibilityToggle = await browser.elementByCss(
'input[data-link-accordion="/refetch-on-new-base-tree/b"]'
)
// Reveal the links to trigger prefetches
await act(async () => {
await linkALinkVisibilityToggle.click()
await linkBLinkVisibilityToggle.click()
}, [
// Page B's content should have been prefetched
{
includes: 'Page B content',
},
// Page A's content should not be prefetched because we're already on that
// page. When prefetching with `prefetch={true}`, we only prefetch the
// delta between the current route and the target route.
{
includes: 'Page A content',
block: 'reject',
},
])
// Navigate to page B
await act(
async () => {
const link = await browser.elementByCss(
'a[href="/refetch-on-new-base-tree/b"]'
)
await link.click()
const content = await browser.elementById('page-b-content')
expect(await content.innerHTML()).toBe('Page B content')
},
// The link for page A is re-prefetched again, even though it's an
// existing link, because the delta between the current route and the
// target route has changed.
//
// This time, the response does include the content for page A.
{
includes: 'Page A content',
}
)
// Navigate to page A
await act(
async () => {
const link = await browser.elementByCss(
'a[href="/refetch-on-new-base-tree/a"]'
)
await link.click()
const content = await browser.elementById('page-a-content')
expect(await content.innerHTML()).toBe('Page A content')
},
// There should be no new requests because everything is fully prefetched.
'no-requests'
)
})
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment