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

[PPR Navs] Bugfix: Dynamic data never streams in if prefetch entry is stale (#59833)

Adds a regression test and a fix for a bug that sometimes happens when a
prefetched route on the client becomes stale — the app would get stuck
in a loading state.

The problem was the condition I used to fallback to the non-PPR
implementation, inside navigateReducer. It was too narrow, causing
prefetched segments that contained dynamic holes to sometimes be treated
as if they were complete. The net effect was that the dynamic data would
never stream in, and the page would get stuck in a fallback state until
the stale prefetch was eventually purged from the cache, or the user
refreshed the page.

The reason the mistake happened was, as an incremental step, I decided
to fallback to the non-PPR implementation for any case where I hadn't
yet implemented the equivalent functionality. I think still think this
is a good strategy, despite the mistake, but I'm eager to get everything
migrated to the new model as soon as possible.


Closes NEXT-1920
parent 14052c05
......@@ -64,7 +64,8 @@ export function updateCacheNodeOnNavigation(
oldRouterState: FlightRouterState,
newRouterState: FlightRouterState,
prefetchData: CacheNodeSeedData,
prefetchHead: React.ReactNode
prefetchHead: React.ReactNode,
isPrefetchStale: boolean
): Task | null {
// Diff the old and new trees to reuse the shared layouts.
const oldRouterStateChildren = oldRouterState[1]
......@@ -126,7 +127,8 @@ export function updateCacheNodeOnNavigation(
oldRouterStateChild,
newRouterStateChild,
prefetchDataChild,
prefetchHead
prefetchHead,
isPrefetchStale
)
} else {
// The server didn't send any prefetch data for this segment. This
......@@ -143,7 +145,8 @@ export function updateCacheNodeOnNavigation(
taskChild = spawnPendingTask(
newRouterStateChild,
prefetchDataChild !== undefined ? prefetchDataChild : null,
prefetchHead
prefetchHead,
isPrefetchStale
)
}
} else {
......@@ -164,7 +167,8 @@ export function updateCacheNodeOnNavigation(
taskChild = spawnPendingTask(
newRouterStateChild,
prefetchDataChild !== undefined ? prefetchDataChild : null,
prefetchHead
prefetchHead,
isPrefetchStale
)
}
}
......@@ -248,13 +252,15 @@ function patchRouterStateWithNewChildren(
function spawnPendingTask(
routerState: FlightRouterState,
prefetchData: CacheNodeSeedData | null,
prefetchHead: React.ReactNode
prefetchHead: React.ReactNode,
isPrefetchStale: boolean
): Task {
// Create a task that will later be fulfilled by data from the server.
const pendingCacheNode = createPendingCacheNode(
routerState,
prefetchData,
prefetchHead
prefetchHead,
isPrefetchStale
)
return {
route: routerState,
......@@ -277,7 +283,12 @@ function spawnTaskForMissingData(routerState: FlightRouterState): Task {
// Create a task for a new subtree that wasn't prefetched by the server.
// This shouldn't really ever happen but it's here just in case the Seed Data
// Tree and the Router State Tree disagree unexpectedly.
const pendingCacheNode = createPendingCacheNode(routerState, null, null)
const pendingCacheNode = createPendingCacheNode(
routerState,
null,
null,
false
)
return {
route: routerState,
node: pendingCacheNode,
......@@ -454,7 +465,8 @@ function finishTaskUsingDynamicDataPayload(
function createPendingCacheNode(
routerState: FlightRouterState,
prefetchData: CacheNodeSeedData | null,
prefetchHead: React.ReactNode
prefetchHead: React.ReactNode,
isPrefetchStale: boolean
): ReadyCacheNode {
const routerStateChildren = routerState[1]
const prefetchDataChildren = prefetchData !== null ? prefetchData[1] : null
......@@ -474,7 +486,8 @@ function createPendingCacheNode(
const newCacheNodeChild = createPendingCacheNode(
routerStateChild,
prefetchDataChild === undefined ? null : prefetchDataChild,
prefetchHead
prefetchHead,
isPrefetchStale
)
const newSegmentMapChild: ChildSegmentMap = new Map()
......@@ -492,8 +505,15 @@ function createPendingCacheNode(
lazyData: null,
parallelRoutes: parallelRoutes,
prefetchRsc: maybePrefetchRsc === undefined ? null : maybePrefetchRsc,
prefetchHead: isLeafSegment ? prefetchHead : null,
prefetchRsc:
// If the prefetched cache entry is stale, we don't show it. We wait for the
// dynamic data to stream in.
// TODO: This check is aruably too deep in the stack. Might be better to
// pass an empty prefetchData Cache Seed object instead.
!isPrefetchStale && maybePrefetchRsc !== undefined
? maybePrefetchRsc
: null,
prefetchHead: !isPrefetchStale && isLeafSegment ? prefetchHead : null,
// Create a deferred promise. This will be fulfilled once the dynamic
// response is received from the server.
......
......@@ -413,22 +413,32 @@ function navigateReducer_PPR(
}
if (
prefetchEntryCacheStatus !== PrefetchCacheEntryStatus.stale &&
// This is just a paranoid check. When PPR is enabled, the server
// will always send back a static response that's rendered from
// the root. If for some reason it doesn't, we fall back to the
// non-PPR implementation.
// TODO: We should get rid of the else branch and do all navigations
// via updateCacheNodeOnNavigation. The current structure is just
// an incremental step.
flightDataPath.length === 3
) {
const prefetchedTree: FlightRouterState = flightDataPath[0]
const seedData = flightDataPath[1]
const head = flightDataPath[2]
// Check whether the prefetched data is stale. If so, we'll
// ignore it and wait for the dynamic data to stream in before
// showing new segments.
const isPrefetchStale =
prefetchEntryCacheStatus === PrefetchCacheEntryStatus.stale
const task = updateCacheNodeOnNavigation(
currentCache,
currentTree,
prefetchedTree,
seedData,
head
head,
isPrefetchStale
)
if (task !== null && task.node !== null) {
// We've created a new Cache Node tree that contains a prefetched
......
import React, { Suspense } from 'react'
import { TriggerBadSuspenseFallback } from './client'
import { getDynamicTestData, getStaticTestData } from './test-data-service'
import { getDynamicTestData, getStaticTestData } from '../test-data-service'
async function Dynamic({ dataKey }) {
return (
......
......@@ -28,27 +28,23 @@ describe('ppr-navigations', () => {
const port = await findPort()
server.listen(port)
next = await createNext({
files: __dirname,
files: __dirname + '/loading-tsx-no-partial-rendering',
env: { TEST_DATA_SERVICE_URL: `http://localhost:${port}` },
})
// There should have been no data requests during build
TestLog.assert([])
const browser = await next.browser(
'/loading-tsx-no-partial-rendering/start'
)
const browser = await next.browser('/start')
// Use a text input to set the target URL.
const input = await browser.elementByCss('input')
await input.fill('/loading-tsx-no-partial-rendering/yay')
await input.fill('/yay')
// This causes a <Link> to appear. (We create the Link after initial render
// so we can control when the prefetch happens.)
const link = await browser.elementByCss('a')
expect(await link.getAttribute('href')).toBe(
'/loading-tsx-no-partial-rendering/yay'
)
expect(await link.getAttribute('href')).toBe('/yay')
// The <Link> triggers a prefetch. Even though this route has a loading.tsx
// boundary, we're still able to prefetch the static data in the page.
......@@ -94,6 +90,92 @@ describe('ppr-navigations', () => {
const loading = await browser.elementById('loading-tsx')
expect(await loading.innerHTML()).toEqual('Loading [inner loading.tsx]...')
})
// This is a regression test where a condition that checked whether a cached
// route entry was stale accidentally caused the router to treat prefetched
// data as if it were complete data that didn't contain dynamic holes. This
// prevented the dynamic data from ever streaming in.
test(
'works if a prefetched route entry has become stale (too much ' +
'time has elapsed since it was prefetched)',
async () => {
const TestLog = createTestLog()
let autoresolveRequests = true
let pendingRequests = new Map()
server = createTestDataServer(async (key, res) => {
TestLog.log('REQUEST: ' + key)
if (autoresolveRequests) {
res.resolve()
return
}
if (pendingRequests.has(key)) {
throw new Error('Request already pending for ' + key)
}
pendingRequests.set(key, res)
})
const port = await findPort()
server.listen(port)
next = await createNext({
files: __dirname + '/stale-prefetch-entry',
env: { TEST_DATA_SERVICE_URL: `http://localhost:${port}` },
})
TestLog.assert(['REQUEST: Some data [static]'])
autoresolveRequests = false
const browser = await next.browser('/', {
// Override Date.now so we can simulate time passing to expire a
// prefetch entry.
async beforePageLoad(page) {
await page.addInitScript(`
__FAKE_NOW__ = 0
Date = class extends Date {
constructor(...args) {
if (args.length === 0) {
super(__FAKE_NOW__)
} else {
super(...args)
}
}
static now() {
return __FAKE_NOW__
}
}
`)
},
})
const startTime = await browser.eval(() => Date.now())
// Explicitly hover over the link to trigger a prefetch.
const link = await browser.elementByCss('a[href="/some-page"]')
await link.hover()
// Increment time by 3 minutes. This is longer than the default expiration
// interval for prefetch entries.
await browser.eval(`__FAKE_NOW__ = 60 * 1000 * 3`)
const endTime = await browser.eval(() => Date.now())
// Sanity check.
expect(endTime - startTime).toBe(60 * 1000 * 3)
// Navigate. The prefetch entry will be stale.
await link.click()
// The static UI appears immediately because it was prerendered at
// build time.
const staticContainer = await browser.elementById('static')
expect(await staticContainer.innerText()).toBe('Some data [static]')
// The dynamic data is fetched on navigation. (In the regression case that
// this test simulates, the dynamic data was never requested, and the page
// got stuck in a loading state.)
await TestLog.waitFor(['REQUEST: Some data [dynamic]'])
pendingRequests.get('Some data [dynamic]').resolve()
// Now the dynamic data appears.
const dynamicContainer = await browser.elementById('dynamic')
expect(await dynamicContainer.innerText()).toBe('Some data [dynamic]')
}
)
})
// NOTE: I've intentionally not yet moved these helpers into a separate
......@@ -103,7 +185,7 @@ describe('ppr-navigations', () => {
type TestDataResponse = {
_res: http.ServerResponse
resolve: (value: string) => any
resolve: (value?: string) => any
reject: (value: any) => any
}
......@@ -147,7 +229,7 @@ function createTestDataServer(
const response: TestDataResponse = {
_res: res,
resolve(value: string | void) {
resolve(value?: string) {
res.end(value === undefined ? key : value)
},
reject(error: Error, status?: number) {
......@@ -208,14 +290,15 @@ function createTestLog() {
}
function assert(expectedEvents: any[]) {
const actualEvents = events
if (pendingExpectation !== null) {
const error = new Error('Cannot assert while a waitFor() is pending.')
Error.captureStackTrace(error, assert)
throw error
}
const actualEvents = events
events = []
if (!areLogsEqual(expectedEvents, actualEvents)) {
// Capture the stack trace of `assert` so that Jest will report the
// error as originating from the `assert` call instead of here.
......@@ -265,6 +348,8 @@ function createTestLog() {
}
}, timeout)
pingExpectation()
return promise
}
......
import React from 'react'
export default function Root({ children }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import Link from 'next/link'
export default function Start() {
return (
<form>
<Link href="/some-page">Navigate</Link>
</form>
)
}
import React, { Suspense } from 'react'
import { getDynamicTestData, getStaticTestData } from '../test-data-service'
import Link from 'next/link'
async function Dynamic({ dataKey }) {
return (
<div id="dynamic">{await getDynamicTestData(`${dataKey} [dynamic]`)}</div>
)
}
async function Static({ dataKey }) {
return <div id="static">{await getStaticTestData(`${dataKey} [static]`)}</div>
}
export default async function Page() {
return (
<>
<Link href="/">Home</Link>
<Suspense fallback="Loading dynamic...">
<Dynamic dataKey="Some data" />
</Suspense>
<Suspense fallback="Loading static...">
<Static dataKey="Some data" />
</Suspense>
</>
)
}
import 'server-only'
import { unstable_noStore } from 'next/cache'
// NOTE: I've intentionally not yet moved these helpers into a shared module, to
// avoid early abstraction. I will if/when we start using them for other tests.
// They are based on the testing patterns we use all over the React codebase, so
// I'm reasonably confident in them.
const TEST_DATA_SERVICE_URL = process.env.TEST_DATA_SERVICE_URL
const ARTIFICIAL_DELAY = 3000
async function getTestData(key: string, isStatic: boolean): Promise<string> {
const searchParams = new URLSearchParams({
key,
})
if (!TEST_DATA_SERVICE_URL) {
// If environment variable is not set, resolve automatically after a delay.
// This is so you can run the test app locally without spinning up a
// data server.
await new Promise<void>((resolve) =>
setTimeout(() => resolve(), ARTIFICIAL_DELAY)
)
if (!isStatic) {
unstable_noStore()
}
return key
}
const response = await fetch(
TEST_DATA_SERVICE_URL + '?' + searchParams.toString(),
{
cache: isStatic ? 'force-cache' : 'no-store',
}
)
const text = await response.text()
if (response.status !== 200) {
throw new Error(text)
}
return text
}
export async function getStaticTestData(key: string): Promise<string> {
return getTestData(key, true)
}
export async function getDynamicTestData(key: string): Promise<string> {
return getTestData(key, false)
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
ppr: true,
},
}
module.exports = nextConfig
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