Unverified Commit 502b6b7d authored by Arnaud's avatar Arnaud Committed by GitHub

[PPF] Fix hanging HTML response after a `'use cache'` error (#100673)

When a failing `'use cache'` function is also read by
`generateMetadata`, error recovery could leave a tracked cache read
pending. With Partial Prefetching enabled, this prevented the embedded
runtime prefetch stream and HTML response from finishing.

The tracked stream now ends its cache read exactly once on completion,
error, or cancellation.

Fixes #100666
parent a5829309
......@@ -1851,16 +1851,35 @@ function createTrackedReadableStream(
cacheSignal: CacheSignal
) {
const reader = stream.getReader()
// End the read exactly once, whether the stream finishes, errors, or is
// canceled: a read that never ends keeps `cacheReady()` from resolving, and a
// second `endRead()` would unbalance the signal.
let readEnded = false
const endRead = () => {
if (!readEnded) {
readEnded = true
cacheSignal.endRead()
}
}
return new ReadableStream({
async pull(controller) {
const { done, value } = await reader.read()
if (done) {
controller.close()
cacheSignal.endRead()
} else {
controller.enqueue(value)
try {
const { done, value } = await reader.read()
if (done) {
controller.close()
endRead()
} else {
controller.enqueue(value)
}
} catch (error) {
endRead()
throw error
}
},
cancel(reason) {
endRead()
return reader.cancel(reason)
},
})
}
......
import type { Metadata } from 'next'
import { cacheLife } from 'next/cache'
async function getItem(id: string): Promise<{ name: string }> {
'use cache'
cacheLife('hours')
throw new Error(`Failed to load item ${id}`)
}
// The item is read outside of a Suspense boundary, so the route needs to allow
// a blocking render.
export const instant = false
export async function generateMetadata({
params,
}: {
params: Promise<{ id: string }>
}): Promise<Metadata> {
const { id } = await params
const item = await getItem(id)
return { title: item.name }
}
export default async function Page({
params,
}: {
params: Promise<{ id: string }>
}) {
const { id } = await params
const item = await getItem(id)
return <h1>{item.name}</h1>
}
import type { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
cacheComponents: true,
partialPrefetching: true,
}
export default nextConfig
import { nextTestSetup } from 'e2e-utils'
describe('use-cache-error-metadata', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('should finish the error document when a failing "use cache" function is also read by generateMetadata', async () => {
const res = await next.fetch('/items/1', {
// Without a bound, a response that never finishes would only fail on the
// test timeout.
signal: AbortSignal.timeout(10_000),
})
const html = await res.text()
expect(html).toContain('id="__next_error__"')
expect(html).toEndWith('</html>')
})
})
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