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

[Segment Cache] Background segment revalidation (#74057)

This implements background revalidation of partial segment entries in
the client Segment Cache.

Until this PR, entries were never replaced in the Segment Cache, they
were only evicted upon becoming stale, or if the LRU overflowed. But
there are cases where we'll want to replace an existing entry with a new
one; for example, if the current segment has more dynamic holes than the
new one.

Most commonly this happens when multiple fetching strategies are mixed
within the same app. For example: when a shared layout belongs to both a
PPR-enabled route and a non-PPR enabled route, the layout data might be
omitted when prefetching the non-PPR enabled route, if it's wrapped in a
loading boundary. But that shouldn't prevent it from being included when
prefetching the route that has PPR enabled.

(*Another example is when a shared layout is prefetched both on viewport
entry and also via `<Link prefetch={true}>`. A viewport prefetch will
only include the static data, but `prefetch={true}` includes both the
static and the dynamic.*

*`<Link prefetch={true}>` is not yet implemented by the Segment Cache
but it will use a similar background revalidation strategy as the one
implemented in this PR.*)
parent 7b614701
...@@ -64,14 +64,15 @@ export function createLRU<T extends LRUNode>( ...@@ -64,14 +64,15 @@ export function createLRU<T extends LRUNode>(
} }
function updateSize(node: T, newNodeSize: number) { function updateSize(node: T, newNodeSize: number) {
// This is a separate function so that we can resize the entry after it's // This is a separate function from `put` so that we can resize the entry
// already been inserted. // regardless of whether it's currently being tracked by the LRU.
const prevNodeSize = node.size
node.size = newNodeSize
if (node.next === null) { if (node.next === null) {
// No longer part of LRU. // This entry is not currently being tracked by the LRU.
return return
} }
const prevNodeSize = node.size // Update the total LRU size
node.size = newNodeSize
lruSize = lruSize - prevNodeSize + newNodeSize lruSize = lruSize - prevNodeSize + newNodeSize
ensureCleanupIsScheduled() ensureCleanupIsScheduled()
} }
......
...@@ -213,7 +213,6 @@ function readRenderSnapshotFromCache( ...@@ -213,7 +213,6 @@ function readRenderSnapshotFromCache(
isPartial = segmentEntry.isPartial isPartial = segmentEntry.isPartial
break break
} }
case EntryStatus.Empty:
case EntryStatus.Pending: { case EntryStatus.Pending: {
// We haven't received data for this segment yet, but there's already // We haven't received data for this segment yet, but there's already
// an in-progress request. Since it's extremely likely to arrive // an in-progress request. Since it's extremely likely to arrive
...@@ -231,12 +230,11 @@ function readRenderSnapshotFromCache( ...@@ -231,12 +230,11 @@ function readRenderSnapshotFromCache(
isPartial = true isPartial = true
break break
} }
case EntryStatus.Empty:
case EntryStatus.Rejected: case EntryStatus.Rejected:
break break
default: { default:
const _exhaustiveCheck: never = segmentEntry segmentEntry satisfies never
break
}
} }
} }
......
'use client'
import Link from 'next/link'
import { useState } from 'react'
export function LinkAccordion({
href,
children,
prefetch,
id,
}: {
href: string
children: string
prefetch?: boolean
id?: string
}) {
const [isVisible, setIsVisible] = useState(false)
return (
<>
<input
type="checkbox"
checked={isVisible}
onChange={() => setIsVisible(!isVisible)}
data-link-accordion={href}
id={id}
/>
{isVisible ? (
<Link href={href} prefetch={prefetch}>
{children}
</Link>
) : (
`${children} (link is hidden)`
)}
</>
)
}
import { Suspense } from 'react'
import { connection } from 'next/server'
async function DynamicContentInSharedLayout() {
await connection()
return (
<div id="dynamic-content-in-shared-layout">
Dynamic content in shared layout
</div>
)
}
export default function SharedLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<div id="shared-layout">
<Suspense
fallback={
<div id="shared-layout-ppr-boundary">
Loading (PPR shell of shared-layout)...
</div>
}
>
<DynamicContentInSharedLayout />
</Suspense>
{children}
</div>
)
}
import { Suspense } from 'react'
import { connection } from 'next/server'
async function Content() {
await connection()
return <div id="page-content">Dynamic page content</div>
}
export default async function Page() {
return (
<Suspense fallback={<div id="page-loading-boundary">Loading page...</div>}>
<Content />
</Suspense>
)
}
import { Suspense } from 'react'
import { connection } from 'next/server'
export const experimental_ppr = true
async function Content() {
await connection()
return <div id="page-content">Dynamic page content</div>
}
export default async function Page() {
return (
<Suspense fallback={<div id="page-loading-boundary">Loading page...</div>}>
<Content />
</Suspense>
)
}
export default function SharedLayout({
children,
}: {
children: React.ReactNode
}) {
return <div id="has-loading-boundary">{children}</div>
}
export default function Loading() {
return (
<div id="loading-boundary">
Loading (has-loading-boundary/loading.tsx)...
</div>
)
}
import { LinkAccordion } from '../link-accordion'
export default function MixedFetchStrategies() {
return (
<>
<p>
This page tests what happens when a shared layout belongs to both a
PPR-enabled route and a non-PPR enabled route. The layout data should be
omitted when prefetching the non-PPR enabled route, because it's inside
another layout that has a loading boundary. But it should be included
when prefetching the route that has PPR enabled.
</p>
<ul>
<li>
<LinkAccordion
id="ppr-enabled"
href="/mixed-fetch-strategies/has-loading-boundary/a/b/shared-layout/ppr-enabled"
>
Link to PPR enabled page
</LinkAccordion>
</li>
<li>
<LinkAccordion
id="ppr-disabled"
href="/mixed-fetch-strategies/has-loading-boundary/a/b/shared-layout/ppr-disabled"
>
Link to PPR disabled page
</LinkAccordion>
</li>
</ul>
</>
)
}
'use client' import { LinkAccordion } from './link-accordion'
import Link from 'next/link'
import { useState } from 'react'
function LinkAccordion({ href, children }) {
const [isVisible, setIsVisible] = useState(false)
return (
<>
<input
type="checkbox"
checked={isVisible}
onChange={() => setIsVisible(!isVisible)}
data-link-accordion={href}
/>
{isVisible ? (
<Link href={href}>{children}</Link>
) : (
`${children} (link is hidden)`
)}
</>
)
}
export default function Page() { export default function Page() {
return ( return (
......
...@@ -92,4 +92,43 @@ describe('segment cache (incremental opt in)', () => { ...@@ -92,4 +92,43 @@ describe('segment cache (incremental opt in)', () => {
it('page with PPR disabled, and has a loading boundary', () => it('page with PPR disabled, and has a loading boundary', () =>
testPrefetchDeduping('/ppr-disabled-with-loading-boundary')) testPrefetchDeduping('/ppr-disabled-with-loading-boundary'))
}) })
it(
'prefetches a shared layout on a PPR-enabled route that was previously ' +
'omitted from a non-PPR-enabled route',
async () => {
let act
const browser = await next.browser('/mixed-fetch-strategies', {
beforePageLoad(p: Playwright.Page) {
act = createRouterAct(p)
},
})
// Initiate a prefetch for the PPR-disabled route first. This will not
// include the /shared-layout/ segment, because it's inside the
// loading boundary.
await act(async () => {
const checkbox = await browser.elementById('ppr-disabled')
await checkbox.click()
})
// Then initiate a prefetch for the PPR-enabled route. This prefetch
// should include the /shared-layout/ segment despite the presence of
// the loading boundary, and despite the earlier non-PPR attempt
await act(async () => {
const checkbox = await browser.elementById('ppr-enabled')
await checkbox.click()
})
// Navigate to the PPR-enabled route
await act(async () => {
const link = await browser.elementByCss('#ppr-enabled + a')
await link.click()
// If we prefetched all the segments correctly, we should be able to
// reveal the page's loading state, before the server responds.
await browser.elementById('page-loading-boundary')
})
}
)
}) })
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