Unverified Commit 730f7bb1 authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

[test] Reproduce two cache-sharing bugs (#100510)

A cache handler lookup for a `'use cache'` function started for one
route can be shared with another route that has different implicit tags.
The reproductions show that the route passed to `revalidatePath()` can
still receive the old value instead of regenerating it. Another case
shows that a route can bypass its cache handler while a fill is pending.

Shared invocation results also preserve the first caller's rendering
decision. A direct prerender caller can leave a nested cache waiting on
a hanging promise, and one prerender can pass its hanging promise to
another. Direct and nested callers can also inherit the wrong
revalidation decision for a stale entry.

These tests deliberately assert the current incorrect behavior. #100511
corrects the assertions alongside the wrapper rewrite.
parent 42ea2b32
import { connection } from 'next/server'
import { getData } from '../data'
import cacheSharingState from '../../../cache-sharing-state'
export default async function Page({
searchParams,
}: {
searchParams: Promise<{ id?: string }>
}) {
await connection()
const { id } = await searchParams
const pendingData = id === undefined ? getData() : getData(id)
cacheSharingState.startedRoutes.push(id === undefined ? 'a' : `a:${id}`)
return <p id="data">{await pendingData}</p>
}
import { connection } from 'next/server'
import { getData } from '../data'
import cacheSharingState from '../../../cache-sharing-state'
export default async function Page({
searchParams,
}: {
searchParams: Promise<{ id?: string }>
}) {
await connection()
const { id } = await searchParams
const pendingData = id === undefined ? getData() : getData(id)
cacheSharingState.startedRoutes.push(id === undefined ? 'b' : `b:${id}`)
return <p id="data">{await pendingData}</p>
}
import { cacheLife, cacheTag } from 'next/cache'
import cacheSharingState from '../../cache-sharing-state'
export async function getData(id?: string) {
'use cache: route-aware'
cacheLife('hours')
cacheTag('path-revalidation')
if (id !== undefined) {
cacheTag(`observed-fill:${id}`)
cacheSharingState.fills.push(id)
await cacheSharingState.pendingFill
}
return Math.random()
}
import { revalidatePath, revalidateTag } from 'next/cache'
import { connection } from 'next/server'
import cacheSharingState from '../../cache-sharing-state'
export async function GET(request: Request) {
await connection()
const id = new URL(request.url).searchParams.get('id')
if (id !== null) {
return Response.json({
startedRoutes: cacheSharingState.startedRoutes,
fills: cacheSharingState.fills.filter((key) => key === id),
writes: cacheSharingState.writes.filter(
(tag) => tag === `observed-fill:${id}`
),
pausedLookupKeys: cacheSharingState.pausedLookupKeys.filter((key) =>
key.includes(JSON.stringify(id))
),
})
}
return Response.json({
softTags: cacheSharingState.softTags,
startedRoutes: cacheSharingState.startedRoutes,
})
}
export async function POST(request: Request) {
switch (await request.text()) {
case 'revalidate-route-b':
revalidatePath('/path-revalidation/b')
break
case 'invalidate-entry': {
const id = new URL(request.url).searchParams.get('id')
if (id === null) {
return new Response(null, { status: 400 })
}
revalidateTag(`observed-fill:${id}`, { expire: 0 })
break
}
case 'hold-fill':
cacheSharingState.pendingFill = new Promise((resolve) => {
cacheSharingState.releaseFill = resolve
})
break
case 'release-fill':
cacheSharingState.releaseFill?.()
cacheSharingState.pendingFill = null
cacheSharingState.releaseFill = null
break
case 'hold-route-lookup-result':
cacheSharingState.pendingRouteLookupResult = new Promise((resolve) => {
cacheSharingState.releaseRouteLookupResult = resolve
})
break
case 'release-route-lookup-result':
cacheSharingState.releaseRouteLookupResult?.()
cacheSharingState.pendingRouteLookupResult = null
cacheSharingState.releaseRouteLookupResult = null
break
case 'hold-entry-lookup':
cacheSharingState.softTags.length = 0
cacheSharingState.startedRoutes.length = 0
cacheSharingState.pendingEntryLookup = new Promise((resolve) => {
cacheSharingState.releaseEntryLookup = resolve
})
break
case 'release-entry-lookup':
cacheSharingState.releaseEntryLookup?.()
cacheSharingState.releaseEntryLookup = null
cacheSharingState.pendingEntryLookup = null
break
default:
return new Response(null, { status: 400 })
}
return new Response(null, { status: 204 })
}
// @ts-check
/**
* The fixture routes and cache handler share these lookup and generation
* controls.
*
* @typedef {object} CacheSharingState
* @property {string[][]} softTags
* Implicit tags from `get()` calls for `path-revalidation` entries.
* @property {string[]} startedRoutes
* Routes that have started a cached call.
* @property {Promise<void> | null} pendingEntryLookup
* Pauses `get()` for entries tagged `path-revalidation`.
* @property {(() => void) | null} releaseEntryLookup
* @property {string[]} fills
* IDs whose cached functions have started execution.
* @property {string[]} writes
* Tags from completed handler writes.
* @property {string[]} pausedLookupKeys
* Keys whose `get()` calls wait on `pendingRouteLookupResult`.
* @property {Promise<void> | null} pendingFill
* Pauses execution of the cached function.
* @property {(() => void) | null} releaseFill
* @property {Promise<void> | null} pendingRouteLookupResult
* Pauses route B's `get()` before it returns.
* @property {(() => void) | null} releaseRouteLookupResult
*/
/**
* @type {typeof globalThis & { cacheSharingState?: CacheSharingState }}
*/
const reference = globalThis
module.exports = reference.cacheSharingState ??= {
softTags: [],
startedRoutes: [],
pendingEntryLookup: null,
releaseEntryLookup: null,
fills: [],
writes: [],
pausedLookupKeys: [],
pendingFill: null,
releaseFill: null,
pendingRouteLookupResult: null,
releaseRouteLookupResult: null,
}
......@@ -5,6 +5,7 @@ const nextConfig = {
cacheComponents: true,
cacheHandlers: {
default: require.resolve('./handler-loader.mjs'),
'route-aware': require.resolve('./route-aware-handler.js'),
},
}
......
// @ts-check
const cacheHandler = require('./handler')
const cacheSharingState = require('./cache-sharing-state')
const defaultCacheHandler =
require('next/dist/server/lib/cache-handlers/default.external').default
/**
* This handler checks implicit-tag expiration in `get()`. Its `getExpiration()`
* returns `Infinity`, so the wrapper does not repeat that check.
*
* @type {import('next/cache').CacheHandler}
*/
const routeAwareCacheHandler = {
...cacheHandler,
async get(cacheKey, softTags) {
const entry = await cacheHandler.get(cacheKey, softTags)
if (
cacheKey.includes('observed-miss-') &&
softTags.some((tag) => tag.includes('/path-revalidation/b')) &&
cacheSharingState.pendingRouteLookupResult !== null
) {
// Record this `get()` call before it waits on `pendingRouteLookupResult`.
// The handler has not checked the route's implicit-tag expiration yet.
cacheSharingState.pausedLookupKeys.push(cacheKey)
await cacheSharingState.pendingRouteLookupResult
}
if (entry === undefined) {
return undefined
}
if (entry.tags.includes('path-revalidation')) {
cacheSharingState.softTags.push([...softTags])
await cacheSharingState.pendingEntryLookup
}
const expiration = await cacheHandler.getExpiration(softTags)
if (entry.timestamp <= expiration) {
await entry.value.cancel()
return undefined
}
return entry
},
async set(cacheKey, pendingEntry) {
const { tags } = await pendingEntry
await cacheHandler.set(cacheKey, pendingEntry)
for (const tag of tags) {
if (tag.startsWith('observed-fill:')) {
cacheSharingState.writes.push(tag)
}
}
},
async getExpiration() {
// This handler checks implicit tags in get(), so Next.js must use that
// result for each route.
return Infinity
},
async refreshTags() {
return defaultCacheHandler.refreshTags()
},
async updateTags(tags) {
return defaultCacheHandler.updateTags(tags)
},
}
module.exports = routeAwareCacheHandler
import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
import type { CacheWrite } from './cache-writes'
import { randomUUID } from 'crypto'
const isoDateRegExp = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/
......@@ -208,9 +209,8 @@ describe('use-cache-custom-handler', () => {
await retry(async () => {
const cliOutput = next.cliOutput.slice(outputIndex)
// The first call misses and fills. The second is served from the entry
// retained for this request, so it never reaches the handler, which for a
// registered handler can be a remote round trip.
// The second call reuses the entry generated by the first call. Only one
// handler `get()` is needed, even though the calls are sequential.
expect(cliOutput).toIncludeRepeated(`ModernCustomCacheHandler::get`, 1)
// Matched up to the escaped opening bracket of the logged cache key,
......@@ -223,6 +223,138 @@ describe('use-cache-custom-handler', () => {
})
})
// Development serves front-cache hits before reconciling the backing cache.
// Its serialized backing reads cannot reach both held lookups concurrently.
// @force-gate start
it('currently shares a handler lookup across routes and serves old data after revalidatePath()', async () => {
const control = (action: string) =>
next.fetch('/path-revalidation', { method: 'POST', body: action })
const readLookupState = () =>
next.fetch('/path-revalidation').then(
(response) =>
response.json() as Promise<{
softTags: string[][]
startedRoutes: string[]
}>
)
const initial = (await next.render$('/path-revalidation/a'))('#data').text()
await retry(async () => {
const writes: CacheWrite[] = await next
.fetch('/cache-writes')
.then((response) => response.json())
expect(writes).toContainEqual(
expect.objectContaining({ tags: ['path-revalidation'] })
)
})
// Revalidate only route B, so route A can still use the existing entry.
// Pause A's `get()` until B calls the same cached function.
expect((await control('revalidate-route-b')).status).toBe(204)
expect((await control('hold-entry-lookup')).status).toBe(204)
const routeARendering = next.render$('/path-revalidation/a')
let routeBRendering: ReturnType<typeof next.render$> | undefined
try {
await retry(async () => {
expect((await readLookupState()).softTags).toEqual([
expect.arrayContaining(['_N_T_/path-revalidation/a/page']),
])
})
routeBRendering = next.render$('/path-revalidation/b')
await retry(async () => {
expect((await readLookupState()).startedRoutes).toEqual(['a', 'b'])
})
expect((await control('release-entry-lookup')).status).toBe(204)
const [routeAPage, routeBPage] = await Promise.all([
routeARendering,
routeBRendering,
])
// B currently reuses A's handler result despite `revalidatePath()`. Only
// A's implicit tags reach `get()`.
expect(routeAPage('#data').text()).toBe(initial)
expect(routeBPage('#data').text()).toBe(initial)
expect(routeBPage('#data').text()).not.toBe('')
expect((await readLookupState()).softTags).toEqual([
expect.arrayContaining(['_N_T_/path-revalidation/a/page']),
])
} finally {
await control('release-entry-lookup')
await Promise.allSettled([routeARendering, routeBRendering])
}
})
// This test controls when the custom handler's `get()` returns. Development
// can serve an entry from its in-memory handler before that call completes.
// @force-gate start
it('currently shares a pending fill without checking route-specific cache invalidation', async () => {
const id = `observed-miss-${randomUUID()}`
const pathname = `/path-revalidation?id=${id}`
const control = (action: string) =>
next.fetch(pathname, { method: 'POST', body: action })
const readState = () =>
next.fetch(pathname).then(
(response) =>
response.json() as Promise<{
fills: string[]
writes: string[]
pausedLookupKeys: string[]
startedRoutes: string[]
}>
)
const initial = (await next.render$(`/path-revalidation/a?id=${id}`))(
'#data'
).text()
await retry(async () => {
expect((await readState()).writes).toHaveLength(1)
})
// Start generation for A, then revalidate B before that generation
// completes. Configure B's `get()` to wait until A's handler write
// completes.
await control('hold-fill')
await control('invalidate-entry')
const routeARendering = next.render$(`/path-revalidation/a?id=${id}`)
let routeBRendering: ReturnType<typeof next.render$> | undefined
try {
await retry(async () => {
expect((await readState()).fills).toHaveLength(2)
})
await control('revalidate-route-b')
await control('hold-route-lookup-result')
routeBRendering = next.render$(`/path-revalidation/b?id=${id}`)
await retry(async () => {
expect((await readState()).startedRoutes).toContain(`b:${id}`)
})
await control('release-fill')
const routeAPage = await routeARendering
const refreshed = routeAPage('#data').text()
expect(refreshed).not.toBe(initial)
expect(refreshed).not.toBe('')
await retry(async () => {
expect((await readState()).writes).toHaveLength(2)
})
// B currently joins A's generated result without calling `get()`. The
// empty `pausedLookupKeys` array confirms that B skipped its own lookup.
await control('release-route-lookup-result')
const routeBPage = await routeBRendering
expect(routeBPage('#data').text()).toBe(refreshed)
expect(routeBPage('#data').text()).not.toBe('')
await retry(async () => {
const state = await readState()
expect(state.pausedLookupKeys).toHaveLength(0)
expect(state.fills).toHaveLength(2)
expect(state.writes).toHaveLength(2)
})
} finally {
await control('release-fill')
await control('release-route-lookup-result')
await Promise.allSettled([routeARendering, routeBRendering])
}
})
if (isNextStart) {
it('handles a failed handler preload without an unhandled rejection', async () => {
await next.stop()
......
import { cacheLife, cacheTag } from 'next/cache'
import { notify } from '../../lib/notify'
export async function readData(id: string) {
'use cache'
cacheLife({ stale: 300, revalidate: 1, expire: 3600 })
cacheTag(`caller-context:${id}`)
await notify(`fill:${id}`)
return { value: `${id}:${crypto.randomUUID()}`, createdAt: Date.now() }
}
export async function readOuter(id: string, name: string) {
'use cache'
cacheLife('hours')
return { name, data: await readData(id) }
}
import { connection } from 'next/server'
import { readData, readOuter } from '../data'
import { notify } from '../../../lib/notify'
export default async function Page({
searchParams,
}: {
searchParams: Promise<{ id?: string | string[] }>
}) {
await connection()
const { id } = await searchParams
if (id === undefined) {
return null
}
if (typeof id !== 'string') {
throw new Error('Expected a single id search parameter')
}
const pendingOuter = readOuter(id, 'outer')
await notify(`direct-ready:${id}`)
const direct = await readData(id)
// This request records when the direct `readData()` call resolves.
await notify(`direct-served:${id}`)
const outer = await pendingOuter
return (
<main>
<p id="direct">{direct.value}</p>
<p id="outer">{outer.data.value}</p>
</main>
)
}
import { connection } from 'next/server'
import { readData, readOuter } from './data'
import { notify } from '../../lib/notify'
export default async function Page({
searchParams,
}: {
searchParams: Promise<{ id?: string | string[] }>
}) {
await connection()
const { id } = await searchParams
if (id === undefined) {
return null
}
if (typeof id !== 'string') {
throw new Error('Expected a single id search parameter')
}
const direct = await readData(id)
await notify(`outer:${id}`)
const [first, second] = await Promise.all([
readOuter(id, 'first'),
readOuter(id, 'second'),
])
return (
<main>
<p id="direct">{direct.value}</p>
<p id={first.name}>{first.data.value}</p>
<p id={second.name}>{second.data.value}</p>
</main>
)
}
import { connection } from 'next/server'
import { readData } from '../data'
export async function GET(request: Request) {
await connection()
const id = new URL(request.url).searchParams.get('id')!
return Response.json(await readData(id))
}
import { get } from 'node:http'
export function notify(key: string): Promise<void> {
const url = process.env.TEST_DATA_SERVICE_URL
if (url === undefined) {
return Promise.resolve()
}
// Test coordination bypasses patched fetch so events are not cached or
// deferred by the render stage that the test controls.
return new Promise((resolve, reject) => {
get(`${url}?key=${encodeURIComponent(key)}`, (response) => {
response.resume()
response.on('end', resolve)
response.on('error', reject)
}).on('error', reject)
})
}
......@@ -17,7 +17,7 @@ function defer(callback: () => Promise<void>) {
// It likely asserts local CLI or runtime output that deploy tests do not expose.
// @force-gate !deploy
describe('use-cache-swr', () => {
const { next } = nextTestSetup({
const { next, isNextDev } = nextTestSetup({
files: __dirname,
skipStart: true,
env: { NEXT_PRIVATE_DEBUG_CACHE: '1' },
......@@ -56,10 +56,38 @@ describe('use-cache-swr', () => {
dataServer.listen(port)
next.env.TEST_DATA_SERVICE_URL = `http://localhost:${port}`
await next.start()
if (isNextDev) {
// Compile both pages before `warmData()` creates the entries under test.
// Neither request has an `id`, so both pages skip their cached functions.
await next.render('/caller-context')
await next.render('/caller-context/foreground-first')
}
})
afterAll(() => dataServer?.close())
async function warmData(id: string) {
const initial: { value: string; createdAt: number } = await next
.fetch(`/caller-context/warm?id=${id}`)
.then((response) => response.json())
await retry(
() => {
expect(Date.now() - initial.createdAt).toBeGreaterThan(1100)
},
5000,
100
)
outputIndex = next.cliOutput.length
return initial
}
function expectOneWrite(id: string) {
expect(getOutput()).toIncludeRepeated(
`PersistentCacheHandler::set [^\\r\\n]*${escapeStringRegexp(JSON.stringify([id]))}`,
1
)
}
let outputIndex: number
beforeEach(() => {
......@@ -326,6 +354,67 @@ describe('use-cache-swr', () => {
}
})
it('currently gives nested callers stale data after background revalidation completes', async () => {
const id = `caller-context-${randomUUID()}`
const initial = await warmData(id)
// Pause the page after its direct `readData()` call returns stale data. The
// nested `readOuter()` calls start after the `outer:<id>` response
// completes.
heldResponseKeys.add(`outer:${id}`)
const rendering = next.render$(`/caller-context?id=${id}`)
try {
await retry(() => {
expect(dataRequestCountsByKey.get(`outer:${id}`)).toBeGreaterThan(0)
expectOneWrite(id)
})
// Both nested calls currently reuse the direct call's stale value,
// although the handler already contains the revalidated entry.
releaseDataResponses(`outer:${id}`)
const $ = await rendering
expect($('#direct').text()).toBe(initial.value)
expect($('#first').text()).toBe(initial.value)
expect($('#first').text()).not.toBe('')
expect($('#second').text()).toBe($('#first').text())
expect(dataRequestCountsByKey.get(`fill:${id}`)).toBe(2)
expectOneWrite(id)
} finally {
releaseDataResponses(`outer:${id}`)
await rendering
}
})
it('currently delays a direct stale read behind a nested foreground fill', async () => {
const id = `caller-context-${randomUUID()}`
const initial = await warmData(id)
// Pause the nested `readData()` call at `fill:<id>`, then start the direct
// call. The nested call requires fresh data. The direct call can use the
// stale entry.
heldResponseKeys.add(`fill:${id}`)
heldResponseKeys.add(`direct-ready:${id}`)
const rendering = next.render$(`/caller-context/foreground-first?id=${id}`)
try {
await retry(() => {
expect(dataRequestCountsByKey.get(`fill:${id}`)).toBe(2)
expect(
dataRequestCountsByKey.get(`direct-ready:${id}`)
).toBeGreaterThan(0)
})
releaseDataResponses(`direct-ready:${id}`)
releaseDataResponses(`fill:${id}`)
const $ = await rendering
expect($('#direct').text()).not.toBe(initial.value)
expect($('#direct').text()).toBe($('#outer').text())
expect($('#outer').text()).not.toBe(initial.value)
expect($('#outer').text()).not.toBe('')
expect(dataRequestCountsByKey.get(`fill:${id}`)).toBe(2)
expectOneWrite(id)
} finally {
releaseDataResponses(`direct-ready:${id}`)
releaseDataResponses(`fill:${id}`)
await rendering
}
})
it('should serve stale data and then refreshed data on a subsequent request', async () => {
const browser = await next.browser('/')
const initialOuter = await browser.elementById('outer-data').text()
......
import type { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import { Suspense } from 'react'
import { cacheLife } from 'next/cache'
import { readShortLived, waitForWarmup } from '../cache'
import { data } from '../data'
async function ShortLived() {
await waitForWarmup('shared')
return <p id="short-lived">{await readShortLived('shared')}</p>
}
async function Outer() {
'use cache'
cacheLife({ stale: 300, revalidate: 600, expire: 3600 })
await data('wait:lookup:short-lived:shared')
const result = readShortLived('shared')
// The test releases the direct call's `get()` after this nested call starts.
await data('join-ready')
return <p id="outer">{await result}</p>
}
export default function Page() {
return (
<main>
<Suspense fallback={<p id="short-pending">Loading short-lived</p>}>
<ShortLived />
</Suspense>
<Outer />
</main>
)
}
import { cacheLife } from 'next/cache'
import { readShortLived } from '../../cache'
export default async function Page() {
'use cache'
cacheLife('default')
await readShortLived('shared')
return <p>Warmup complete</p>
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
cacheComponents: true,
cacheHandlers: {
default: require.resolve('./handler.js'),
},
experimental: {
useCacheTimeout: 5,
},
}
module.exports = nextConfig
import { Suspense } from 'react'
import { readShortLived } from '../../cache'
import { data } from '../../data'
async function waitForLookup() {
'use cache'
await data('wait:lookup:short-lived:shared')
}
// The test waits for this notification before it releases the producer's
// handler lookup.
async function notifyConsumerStarted() {
'use cache'
await data('join-ready')
}
async function readIndependent() {
'use cache'
return data('independent')
}
async function readCachedMessage(value: string) {
'use cache'
await data('cached-message')
return `message: ${value}`
}
async function Consumer() {
await waitForLookup()
const shortLived = readShortLived('shared')
const independent = readIndependent()
const [value] = await Promise.all([
Promise.race([shortLived, independent]),
notifyConsumerStarted(),
])
return <p id="cached-message">{await readCachedMessage(value)}</p>
}
export default function Page() {
return (
<Suspense fallback={<p id="consumer-pending">Loading consumer</p>}>
<Consumer />
</Suspense>
)
}
import type { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import { Suspense } from 'react'
import { after } from 'next/server'
import { readShortLived, waitForWarmup } from '../../cache'
import { notify } from '../../notify'
async function ShortLived() {
await waitForWarmup('shared')
return <p id="short-lived">{await readShortLived('shared')}</p>
}
export default function Page() {
after(() => notify('producer-finished'))
return (
<Suspense fallback={<p id="short-pending">Loading short-lived</p>}>
<ShortLived />
</Suspense>
)
}
import { cacheLife } from 'next/cache'
import { readShortLived } from '../../cache'
export default async function Page() {
'use cache'
cacheLife('default')
await readShortLived('shared')
return <p>Warmup complete</p>
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
cacheComponents: true,
cacheHandlers: {
default: require.resolve('./handler.js'),
},
experimental: {
useCacheTimeout: 5,
},
}
module.exports = nextConfig
import { cacheLife, cacheTag } from 'next/cache'
import { data } from './data'
export async function readShortLived(key: string) {
'use cache'
cacheTag(`short-lived:${key}`)
cacheLife({ stale: 30, revalidate: 120, expire: 180 })
return `short-lived ${key}`
}
export async function waitForWarmup(key: string) {
'use cache'
await data(`wait:stored:short-lived:${key}`)
}
export async function data(key: string) {
const response = await fetch(
`${process.env.TEST_DATA_SERVICE_URL}?key=${key}`
)
return response.text()
}
const { notify } = require('./notify')
// This handler stores fully collected cache entries in memory. It sends
// `lookup:<tag>` requests from `get()` and `stored:<tag>` notifications after
// successful `set()` calls.
const entries = new Map()
module.exports = {
async get(key) {
const entry = entries.get(key)
if (!entry) {
return undefined
}
const tag = entry.tags.find((tag) => tag.startsWith('short-lived:'))
if (tag) {
await notify(`lookup:${tag}`)
}
const [value, retained] = entry.value.tee()
entry.value = retained
return { ...entry, value }
},
async set(key, pendingEntry) {
const entry = await pendingEntry
const [value, consumed] = entry.value.tee()
const reader = consumed.getReader()
while (!(await reader.read()).done) {}
entries.set(key, { ...entry, value })
const tag = entry.tags.find((tag) => tag.startsWith('short-lived:'))
if (tag) {
// Notify after the write promise settles so prerenders cannot join the
// warmup.
setImmediate(() => {
void notify(`stored:${tag}`)
})
}
},
async refreshTags() {},
async getExpiration() {
return 0
},
async updateTags() {},
}
const { get } = require('node:http')
// Handler barriers and after() notifications bypass Next's patched fetch.
// Patched fetch can suspend during prerendering instead of sending the request.
function notify(key) {
return new Promise((resolve, reject) => {
get(`${process.env.TEST_DATA_SERVICE_URL}?key=${key}`, (response) => {
response.resume()
response.on('end', resolve)
response.on('error', reject)
}).on('error', reject)
})
}
module.exports = { notify }
import { retry } from 'next-test-utils'
import { createTestDataServer } from 'test-data-service/writer'
/**
* This service records fixture requests and controls when their responses
* complete. `hold(key)` pauses responses for that key. A `wait:<key>` request
* waits until the service receives a request for `key`. It does not wait for
* that request's response to complete.
*/
export function createDataService() {
const requestedKeys: string[] = []
const heldResponseKeys = new Set<string>()
const pendingResponsesByKey = new Map<
string,
Array<(value?: string) => void>
>()
const requestWaitersByKey = new Map<string, Array<() => void>>()
const server = createTestDataServer((key, response) => {
requestedKeys.push(key)
if (key.startsWith('wait:')) {
const event = key.slice('wait:'.length)
if (requestedKeys.includes(event)) {
response.resolve()
} else {
const callbacks = requestWaitersByKey.get(event) ?? []
callbacks.push(() => response.resolve())
requestWaitersByKey.set(event, callbacks)
}
return
}
requestWaitersByKey.get(key)?.forEach((resolve) => resolve())
requestWaitersByKey.delete(key)
if (heldResponseKeys.has(key)) {
const callbacks = pendingResponsesByKey.get(key) ?? []
callbacks.push((value) => response.resolve(value))
pendingResponsesByKey.set(key, callbacks)
} else {
response.resolve()
}
})
function releaseAll() {
heldResponseKeys.clear()
for (const callbacks of pendingResponsesByKey.values()) {
callbacks.forEach((resolve) => resolve())
}
for (const callbacks of requestWaitersByKey.values()) {
callbacks.forEach((resolve) => resolve())
}
pendingResponsesByKey.clear()
requestWaitersByKey.clear()
}
return {
server,
requestedKeys,
hold(key: string) {
heldResponseKeys.add(key)
},
release(key: string, value?: string) {
heldResponseKeys.delete(key)
pendingResponsesByKey.get(key)?.forEach((resolve) => resolve(value))
pendingResponsesByKey.delete(key)
},
async waitForRequest(key: string) {
await retry(
() => {
expect(requestedKeys).toContain(key)
},
30_000,
100
)
},
releaseAll,
close() {
releaseAll()
server.close()
},
}
}
import { FileRef, nextTestSetup } from 'e2e-utils'
import { findPort } from 'next-test-utils'
import { load } from 'cheerio'
import path from 'path'
import { createDataService } from './test-data'
function setupFixture(appDirectory: string) {
const { next } = nextTestSetup({
files: path.join(__dirname, 'fixtures', appDirectory),
overrideFiles: {
'cache.ts': new FileRef(path.join(__dirname, 'shared/cache.ts')),
'data.ts': new FileRef(path.join(__dirname, 'shared/data.ts')),
'notify.js': new FileRef(path.join(__dirname, 'shared/notify.js')),
'handler.js': new FileRef(path.join(__dirname, 'shared/handler.js')),
},
skipStart: true,
})
let dataService: ReturnType<typeof createDataService>
beforeAll(async () => {
dataService = createDataService()
const port = await findPort()
dataService.server.listen(port)
next.env.TEST_DATA_SERVICE_URL = `http://localhost:${port}`
})
afterAll(() => {
dataService?.close()
})
return {
next,
get dataService() {
return dataService
},
}
}
describe('use-cache-dedupe-prerender', () => {
describe('direct and nested calls during static prerendering', () => {
const fixture = setupFixture('context')
it('currently times out a nested cache when a direct short-lived warm-cache call starts the lookup first', async () => {
const { next, dataService } = fixture
// Pause the direct call's handler `get()` until `Outer()` calls the same
// short-lived cached function.
dataService.hold('lookup:short-lived:shared')
const building = next.build()
try {
await dataService.waitForRequest('join-ready')
dataService.release('lookup:short-lived:shared')
const result = await building
// The nested call shares the direct call's hanging promise. Its cache
// entry cannot complete, so the build reports a prerender timeout.
expect(result.exitCode).toBe(1)
expect(next.cliOutput).toContain(
'function took too long during prerendering'
)
expect(next.cliOutput).toContain('Error occurred prerendering page "/"')
} finally {
dataService.releaseAll()
await building
}
})
})
describe('concurrent prerenders sharing the same short-lived cache', () => {
const fixture = setupFixture('lifetime')
it('currently omits cached work in a second prerender after the first prerender finishes', async () => {
const { next, dataService } = fixture
// Keep the producer's `get()` pending until the consumer starts its
// calls. Keep `readIndependent()` pending until the producer's `after()`
// callback.
dataService.hold('lookup:short-lived:shared')
dataService.hold('independent')
const building = next.build()
try {
await dataService.waitForRequest('join-ready')
dataService.release('lookup:short-lived:shared')
await dataService.waitForRequest('producer-finished')
// Resolve the independent read after the producer's `after()` callback.
// The consumer currently skips `readCachedMessage()`.
dataService.release('independent', 'independent value')
const result = await building
expect(result.exitCode).toBe(0)
// Copy the requests before `next.render()` can add runtime requests.
// The `cached-message` assertion must check work done during the build.
const buildRequests = [...dataService.requestedKeys]
await next.start({ skipBuild: true })
const html = await next.render('/consumer')
const [shell] = html.split('<!-- PPR_BOUNDARY_SENTINEL -->')
const $ = load(shell)
expect($('#cached-message').text()).toBe('')
expect($('#consumer-pending').length).toBe(1)
expect(buildRequests).not.toContain('cached-message')
} finally {
dataService.releaseAll()
await building
}
})
})
})
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