Unverified Commit 954354f2 authored by Andrew Clark's avatar Andrew Clark Committed by GitHub

[Segment Cache] Re-implement refresh reducer (#84426)

This re-implements the refresh reducer to use the Segment Cache-style
flow, similar to how the navigate reducer already works.

Most of the logic can be reused from what was already implemented for
navigations. The bulk of the changes are related to the case where a
"default" parallel route slot is not present on the current page, and so
needs to be refreshed from an older page's URL.

Theoretically, this should be the last remaining case where we rely on
the lazy fetching logic in LayoutRouter. I think there may still be some
error cases where we still rely on that, though. Once these are all
addressed, we can remove the lazy-fetching mechanism entirely.
parent 83ae64fe
......@@ -8,7 +8,6 @@ import { fillLazyItemsTillLeafWithHead } from './fill-lazy-items-till-leaf-with-
import { extractPathFromFlightRouterState } from './compute-changed-path'
import type { AppRouterState } from './router-reducer-types'
import { addRefreshMarkerToActiveParallelSegments } from './refetch-inactive-parallel-segments'
import { getFlightDataPartsFromPath } from '../../flight-data-helpers'
export interface InitialRouterStateParameters {
......@@ -64,8 +63,6 @@ export function createInitialRouterState({
createHrefFromUrl(location)
: initialCanonicalUrl
addRefreshMarkerToActiveParallelSegments(initialTree, canonicalUrl)
// When the cache hasn't been seeded yet we fill the cache with the head.
if (initialParallelRoutes === null || initialParallelRoutes.size === 0) {
fillLazyItemsTillLeafWithHead(
......
......@@ -67,7 +67,7 @@ export function generateSegmentsFromPatch(
return segments
}
function handleNavigationResult(
export function handleNavigationResult(
url: URL,
state: ReadonlyReducerState,
mutable: Mutable,
......@@ -112,7 +112,13 @@ function handleNavigationResult(
mutable.patchedTree = result.data.flightRouterState
mutable.renderedSearch = result.data.renderedSearch
mutable.canonicalUrl = result.data.canonicalUrl
mutable.scrollableSegments = result.data.scrollableSegments
// TODO: During a refresh, we don't set the `scrollableSegments`. There's
// some confusing and subtle logic in `handleMutable` that decides what
// to do when `shouldScroll` is set but `scrollableSegments` is not. I'm
// not convinced it's totally coherent but the tests assert on this
// particular behavior so I've ported the logic as-is from the previous
// router implementation, for now.
mutable.scrollableSegments = result.data.scrollableSegments ?? undefined
mutable.shouldScroll = result.data.shouldScroll
mutable.hashFragment = result.data.hash
return handleMutable(state, mutable)
......
import {
fetchServerResponse,
type FetchServerResponseResult,
} from '../fetch-server-response'
import { createHrefFromUrl } from '../create-href-from-url'
import { applyRouterStatePatchToTree } from '../apply-router-state-patch-to-tree'
import { isNavigatingToNewRootLayout } from '../is-navigating-to-new-root-layout'
import type {
Mutable,
ReadonlyReducerState,
ReducerState,
RefreshAction,
} from '../router-reducer-types'
import { handleExternalUrl } from './navigate-reducer'
import { handleMutable } from '../handle-mutable'
import type { CacheNode } from '../../../../shared/lib/app-router-types'
import { fillLazyItemsTillLeafWithHead } from '../fill-lazy-items-till-leaf-with-head'
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/cache'
import { handleNavigationResult } from './navigate-reducer'
import { refresh as refreshUsingSegmentCache } from '../../segment-cache/navigation'
export function refreshReducer(
state: ReadonlyReducerState,
action: RefreshAction
): ReducerState {
const { origin } = action
const mutable: Mutable = {}
const href = state.canonicalUrl
let currentTree = state.tree
const currentUrl = new URL(state.canonicalUrl, action.origin)
const result = refreshUsingSegmentCache(
currentUrl,
state.tree,
state.nextUrl,
state.renderedSearch,
state.canonicalUrl
)
const mutable: Mutable = {}
mutable.preserveCustomHistoryState = false
const cache: CacheNode = createEmptyCacheNode()
// If the current tree was intercepted, the nextUrl should be included in the request.
// This is to ensure that the refresh request doesn't get intercepted, accidentally triggering the interception route.
const includeNextUrl = hasInterceptionRouteInCurrentTree(state.tree)
// TODO-APP: verify that `href` is not an external url.
// Fetch data from the root of the tree.
cache.lazyData = fetchServerResponse(new URL(href, origin), {
flightRouterState: [
currentTree[0],
currentTree[1],
currentTree[2],
'refetch',
],
nextUrl: includeNextUrl ? state.nextUrl : null,
})
const navigatedAt = Date.now()
return cache.lazyData.then(
async (result: FetchServerResponseResult) => {
// Handle case when navigating to page in `pages` from `app`
if (typeof result === 'string') {
return handleExternalUrl(
state,
mutable,
result,
state.pushRef.pendingPush
)
}
const { flightData, canonicalUrl, renderedSearch } = result
// Remove cache.lazyData as it has been resolved at this point.
cache.lazyData = null
for (const normalizedFlightData of flightData) {
const {
tree: treePatch,
seedData: cacheNodeSeedData,
head,
isRootRender,
} = normalizedFlightData
if (!isRootRender) {
// TODO-APP: handle this case better
console.log('REFRESH FAILED')
return state
}
const newTree = applyRouterStatePatchToTree(
// TODO-APP: remove ''
[''],
currentTree,
treePatch,
state.canonicalUrl
)
if (newTree === null) {
return handleSegmentMismatch(state, action, treePatch)
}
if (isNavigatingToNewRootLayout(currentTree, newTree)) {
return handleExternalUrl(
state,
mutable,
href,
state.pushRef.pendingPush
)
}
mutable.canonicalUrl = createHrefFromUrl(canonicalUrl)
// Handles case where prefetch only returns the router tree patch without rendered components.
if (cacheNodeSeedData !== null) {
const rsc = cacheNodeSeedData[0]
const loading = cacheNodeSeedData[2]
cache.rsc = rsc
cache.prefetchRsc = null
cache.loading = loading
fillLazyItemsTillLeafWithHead(
navigatedAt,
cache,
// Existing cache is not passed in as `router.refresh()` has to invalidate the entire cache.
undefined,
treePatch,
cacheNodeSeedData,
head
)
revalidateEntireCache(state.nextUrl, newTree)
}
await refreshInactiveParallelSegments({
navigatedAt,
state,
updatedTree: newTree,
updatedCache: cache,
includeNextUrl,
canonicalUrl: mutable.canonicalUrl || state.canonicalUrl,
})
mutable.cache = cache
mutable.patchedTree = newTree
mutable.renderedSearch = renderedSearch
currentTree = newTree
}
return handleMutable(state, mutable)
},
() => state
)
return handleNavigationResult(currentUrl, state, mutable, false, result)
}
......@@ -12,8 +12,10 @@ import type { NormalizedFlightData } from '../../flight-data-helpers'
import { fetchServerResponse } from '../router-reducer/fetch-server-response'
import {
startPPRNavigation,
startPPRRefresh,
listenForDynamicRequest,
type Task as PPRNavigationTask,
type NavigationRequestAccumulation,
} from '../router-reducer/ppr-navigations'
import { createHrefFromUrl } from '../router-reducer/create-href-from-url'
import {
......@@ -28,6 +30,7 @@ import {
import { createCacheKey } from './cache-key'
import { addSearchParamsIfPageSegment } from '../../../shared/lib/segment'
import { NavigationResultTag } from './types'
import { hasInterceptionRouteInCurrentTree } from '../router-reducer/reducers/has-interception-route-in-current-tree'
type MPANavigationResult = {
tag: NavigationResultTag.MPA
......@@ -49,7 +52,7 @@ type SuccessfulNavigationResult = {
cacheNode: CacheNode
canonicalUrl: string
renderedSearch: string
scrollableSegments: Array<FlightSegmentPath>
scrollableSegments: Array<FlightSegmentPath> | null
shouldScroll: boolean
hash: string
}
......@@ -218,6 +221,81 @@ export function navigate(
}
}
export function refresh(
currentUrl: URL,
currentFlightRouterState: FlightRouterState,
currentNextUrl: string | null,
currentRenderedSearch: string,
currentCanonicalUrl: string
): SuccessfulNavigationResult | NoOpNavigationResult | MPANavigationResult {
// A refresh is a special case of a navigation where all the dynamic data
// on the current router is re-fetched. Most of the logic is handled within
// the ppr-navigations module. The main difference here is that we call
// startPPRRefresh instead of startPPRNavigation.
const now = Date.now()
const shouldScroll = true
const accumulation: NavigationRequestAccumulation = {
scrollableSegments: [],
separateRefreshUrls: null,
}
const task = startPPRRefresh(
now,
currentFlightRouterState,
currentNextUrl,
accumulation
)
if (task !== null) {
if (task.dynamicRequestTree !== null) {
// If the current tree was intercepted, the nextUrl should be included in
// the request. This is to ensure that the refresh request doesn't get
// intercepted, accidentally triggering the interception route.
// TODO: This logic was copied from the old implementation. It works, but
// a simpler way to model this would be to track whether any navigation
// has occurred since the initial (SSR) navigation, since that's the only
// one that should not be intercepted.
const includeNextUrl = hasInterceptionRouteInCurrentTree(
currentFlightRouterState
)
listenForDynamicRequest(
currentUrl,
includeNextUrl ? currentNextUrl : null,
task,
task.dynamicRequestTree,
null,
accumulation
)
}
const newTree = task.route
const newCacheNode = task.node
if (newTree !== null && newCacheNode !== null) {
// Re-render with the new data. All the other data remains the same.
return {
tag: NavigationResultTag.Success,
data: {
flightRouterState: newTree,
cacheNode: newCacheNode,
canonicalUrl: currentCanonicalUrl,
renderedSearch: currentRenderedSearch,
// During a refresh, we don't set the `scrollableSegments`. See
// corresponding comment in navigate-reducer.ts for context.
scrollableSegments: null,
shouldScroll,
hash: currentUrl.hash,
},
}
}
}
return {
tag: NavigationResultTag.NoOp,
data: {
canonicalUrl: currentCanonicalUrl,
shouldScroll,
},
}
}
function navigateUsingPrefetchedRouteTree(
now: number,
url: URL,
......@@ -241,7 +319,10 @@ function navigateUsingPrefetchedRouteTree(
// TODO: Eventually updateCacheNodeOnNavigation (or the equivalent) should
// read from the Segment Cache directly. It's only structured this way for now
// so we can share code with the old prefetching implementation.
const scrollableSegments: Array<FlightSegmentPath> = []
const accumulation: NavigationRequestAccumulation = {
scrollableSegments: [],
separateRefreshUrls: null,
}
const task = startPPRNavigation(
now,
currentUrl,
......@@ -252,29 +333,25 @@ function navigateUsingPrefetchedRouteTree(
prefetchHead,
isPrefetchHeadPartial,
isSamePageNavigation,
scrollableSegments
accumulation
)
if (task !== null) {
const dynamicRequestTree = task.dynamicRequestTree
if (dynamicRequestTree !== null) {
const promiseForDynamicServerResponse = fetchServerResponse(
new URL(canonicalUrl, url.origin),
{
flightRouterState: dynamicRequestTree,
nextUrl,
}
if (task.dynamicRequestTree !== null) {
listenForDynamicRequest(
url,
nextUrl,
task,
task.dynamicRequestTree,
null,
accumulation
)
listenForDynamicRequest(task, promiseForDynamicServerResponse)
} else {
// The prefetched tree does not contain dynamic holes — it's
// fully static. We can skip the dynamic request.
}
return navigationTaskToResult(
task,
currentCacheNode,
canonicalUrl,
renderedSearch,
scrollableSegments,
accumulation.scrollableSegments,
shouldScroll,
hash
)
......@@ -509,7 +586,10 @@ async function navigateDynamicallyWithNoPrefetch(
const isPrefetchHeadPartial = true
// Now we proceed exactly as we would for normal navigation.
const scrollableSegments: Array<FlightSegmentPath> = []
const accumulation: NavigationRequestAccumulation = {
scrollableSegments: [],
separateRefreshUrls: null,
}
const task = startPPRNavigation(
now,
currentUrl,
......@@ -520,7 +600,7 @@ async function navigateDynamicallyWithNoPrefetch(
prefetchHead,
isPrefetchHeadPartial,
isSamePageNavigation,
scrollableSegments
accumulation
)
if (task !== null) {
// In this case, we've already sent the dynamic request, so we don't
......@@ -531,9 +611,15 @@ async function navigateDynamicallyWithNoPrefetch(
// was present in the cache, but the route tree was not. E.g. navigating
// to a URL that was not prefetched but rewrites to a different URL
// that was.
const hasDynamicHoles = task.dynamicRequestTree !== null
if (hasDynamicHoles) {
listenForDynamicRequest(task, promiseForDynamicServerResponse)
if (task.dynamicRequestTree !== null) {
listenForDynamicRequest(
url,
nextUrl,
task,
task.dynamicRequestTree,
promiseForDynamicServerResponse,
accumulation
)
} else {
// The prefetched tree does not contain dynamic holes — it's
// fully static. We don't need to process the server response further.
......@@ -543,7 +629,7 @@ async function navigateDynamicallyWithNoPrefetch(
currentCacheNode,
createHrefFromUrl(canonicalUrl),
renderedSearch,
scrollableSegments,
accumulation.scrollableSegments,
shouldScroll,
hash
)
......
import { nextTestSetup } from 'e2e-utils'
import { createRouterAct } from 'router-act'
import { browserConfigWithFixedTime, fastForwardTo } from './test-utils'
import path from 'path'
import { Playwright } from 'next-webdriver'
import type { Page as PlaywrightPage } from 'playwright'
describe('app dir client cache with parallel routes', () => {
const { next, isNextDev } = nextTestSetup({
......@@ -29,13 +29,14 @@ describe('app dir client cache with parallel routes', () => {
describe('prefetch={true}', () => {
it('should prefetch the full page', async () => {
let act: ReturnType<typeof createRouterAct>
let page: PlaywrightPage
const browser = await next.browser('/', {
beforePageLoad(page) {
browserConfigWithFixedTime.beforePageLoad(page)
act = createRouterAct(page)
async beforePageLoad(p) {
page = p
await page.clock.install()
},
})
const act = createRouterAct(page)
// Reveal the link to trigger prefetch and wait for it to complete
const link = await act(
......@@ -53,13 +54,14 @@ describe('app dir client cache with parallel routes', () => {
})
it('should re-use the cache for the full page, only for 5 mins', async () => {
let act: ReturnType<typeof createRouterAct>
let page: PlaywrightPage
const browser = await next.browser('/', {
beforePageLoad(page) {
browserConfigWithFixedTime.beforePageLoad(page)
act = createRouterAct(page)
async beforePageLoad(p) {
page = p
await page.clock.install()
},
})
const act = createRouterAct(page)
// Toggle the link, assert on the prefetch content
const link = await act(
......@@ -109,7 +111,7 @@ describe('app dir client cache with parallel routes', () => {
}, 'no-requests')
// Fast forward 5 minutes
await browser.eval(fastForwardTo, 5 * 60 * 1000)
await page.clock.fastForward(5 * 60 * 1000)
// Toggle the link to the other page again, assert on prefetch content
const linkAfterExpiry = await act(
......
export default function AnalyticsPage() {
return <div>Analytics page</div>
}
export default function DefaultDashboardMain() {
return <div>(Default dashboard main)</div>
}
export default function DashboardMainLandingPage() {
return <div>Dashboard main landing page</div>
}
'use client'
import { useState } from 'react'
export function RenderCounterClient({ uuid }: { uuid: string }) {
const [counter, setCounter] = useState(0)
const [prevUuid, setPrevUuid] = useState(uuid)
if (prevUuid !== uuid) {
// A new dynamic value was received from the server. Increment the counter.
setPrevUuid(uuid)
setCounter(counter + 1)
}
return counter
}
export default function DefaultDashboardNavbar() {
return <div>(Default dashboard navbar)</div>
}
import { Suspense } from 'react'
import { connection } from 'next/server'
import { RenderCounterClient } from './client'
async function DynamicRenderCounter(): Promise<React.ReactNode> {
// Renders a count of the number of times the client receives new dynamic data
// from the server. The count is computed on the client and stored in React
// state, so it gets reset if the state of the tree is reset.
await connection()
const uuid = crypto.randomUUID()
return <RenderCounterClient uuid={uuid} />
}
export default function DashboardNavbarLandingPage() {
return (
<div>
<p>Navbar</p>
<Suspense fallback={<div>Loading...</div>}>
<p>
Navbar dynamic render counter:{' '}
<span id="navbar-dynamic-render-counter">
<DynamicRenderCounter />
</span>
</p>
</Suspense>
</div>
)
}
'use client'
import { useRouter } from 'next/navigation'
export function ClientRefreshButton() {
const router = useRouter()
return (
<button
id="client-refresh-button"
onClick={() => {
router.refresh()
}}
>
Refresh
</button>
)
}
import { LinkAccordion } from '../../components/link-accordion'
import { ClientRefreshButton } from './client'
export default function DashboardLayout({
navbar,
main,
}: {
navbar: React.ReactNode
main: React.ReactNode
}) {
return (
<div>
<div style={{ border: '1px solid black', padding: '1rem' }}>
{navbar}
<div>
<ClientRefreshButton />
</div>
<ul>
<li>
<LinkAccordion href="/dashboard">Dashboard Home</LinkAccordion>
</li>
<li>
<LinkAccordion href="/dashboard/analytics">Analytics</LinkAccordion>
</li>
</ul>
</div>
<div>{main}</div>
</div>
)
}
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
import { LinkAccordion } from '../components/link-accordion'
export default async function Page() {
return <LinkAccordion href="/dashboard">Dashboard</LinkAccordion>
}
'use client'
import Link, { type LinkProps } from 'next/link'
import { useState } from 'react'
export function LinkAccordion({
href,
children,
prefetch,
}: {
href: string
children: React.ReactNode
prefetch?: LinkProps['prefetch']
}) {
const [isVisible, setIsVisible] = useState(false)
return (
<>
<input
type="checkbox"
checked={isVisible}
onChange={() => setIsVisible(!isVisible)}
data-link-accordion={href}
/>
{isVisible ? (
<Link href={href} prefetch={prefetch}>
{children}
</Link>
) : (
<>{children} (link is hidden)</>
)}
</>
)
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
cacheComponents: true,
productionBrowserSourceMaps: true,
}
module.exports = nextConfig
import { nextTestSetup } from 'e2e-utils'
import type * as Playwright from 'playwright'
import { createRouterAct } from 'router-act'
describe('segment cache (refresh)', () => {
const { next, isNextDev } = nextTestSetup({
files: __dirname,
})
if (isNextDev) {
test('disabled in development', () => {})
return
}
it('refreshes data inside reused default parallel route slots', async () => {
// Load the main Dashboard page. This will render the nav bar into the
// @navbar slot.
let page: Playwright.Page
const browser = await next.browser('/dashboard', {
beforePageLoad(p: Playwright.Page) {
page = p
},
})
const act = createRouterAct(page)
// Navigate to the Analytics page. The analytics page does not match the
// @navbar slot, so the client reuses the one that was rendered by the
// previous page.
await act(async () => {
const toggleAnalyticsLink = await browser.elementByCss(
'input[data-link-accordion="/dashboard/analytics"]'
)
await toggleAnalyticsLink.click()
const link = await browser.elementByCss('a[href="/dashboard/analytics"]')
await link.click()
})
// Click the refresh button and confirm the navigation bar is re-rendered,
// even though it's not part of the Analytics page.
await act(
async () => {
const refreshButton = await browser.elementById('client-refresh-button')
await refreshButton.click()
},
{
includes: 'Navbar dynamic render counter',
}
)
const navbarDynamicRenderCounter = await browser.elementById(
'navbar-dynamic-render-counter'
)
// If this is still 0, then the nav bar was not successfully refreshed
expect(await navbarDynamicRenderCounter.textContent()).toBe('1')
})
})
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