Unverified Commit 3b658c96 authored by digasic's avatar digasic Committed by GitHub

End the cache-signal read when `encryptActionBoundArgs` hits the RDC (#100622)

### What?

End the `CacheSignal` read in `encryptActionBoundArgs` on the Resume
Data Cache hit path, so a request's `cacheReady()` can resolve.

### Why?

`encryptActionBoundArgs` calls `startReadOnce()` before looking the
serialized bound args up in the RDC. On a hit it returns the cached
ciphertext immediately and never calls `endReadIfStarted()`, leaving the
request's `CacheSignal` count at 1 forever.

With `partialPrefetching` the dynamic render embeds a runtime-prefetch
stream in the initial RSC payload and only writes/closes it after
`cacheSignal.cacheReady()` (`startRuntimePrefetchRender`). With the read
never ended, that stream is never closed, the Flight stream never ends,
the HTML `</body></html>` suffix is never flushed, and the HTTP response
of every partially prerendered page that renders an inline Server
Function with bound args never terminates in production (`next start`).
curl waits for its timeout; browsers stay in `document.readyState ===
"loading"`.

The hit is the normal production case: the build-time prerender stores
the encrypted bound args in the RDC, which is persisted in the postponed
state whenever the page has at least one `"use cache"` entry.

### How?

Call `endReadIfStarted()` before returning `cachedEncrypted`, mirroring
the miss path (which ends the read after `encodeActionBoundArg`) and the
error path.

Adds an e2e test
(`test/e2e/app-dir/partial-prefetching-inline-server-function`) with
`cacheComponents` + `partialPrefetching`, a page that has a `"use
cache"` entry, an inline Server Function closing over a prop and a
`cookies()` hole; it asserts that a production fetch of the page
completes and the body ends with `</html>`. Without the fix, reading the
body never resolves.

Fixes #100621

🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-authored-by: 's avatarClaude Fable 5.1 <noreply@anthropic.com>
parent 3a6dc245
...@@ -225,6 +225,7 @@ export const encryptActionBoundArgs = React.cache( ...@@ -225,6 +225,7 @@ export const encryptActionBoundArgs = React.cache(
const cachedEncrypted = resumeDataCache?.encryptedBoundArgs.get(cacheKey) const cachedEncrypted = resumeDataCache?.encryptedBoundArgs.get(cacheKey)
if (cachedEncrypted) { if (cachedEncrypted) {
endReadIfStarted()
return cachedEncrypted return cachedEncrypted
} }
......
import type { ReactNode } from 'react'
export default function RootLayout({ children }: { children: ReactNode }) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
import { Suspense } from 'react'
import { cookies } from 'next/headers'
// A "use cache" entry makes the Resume Data Cache get persisted into the
// postponed state, together with the encrypted bound args of the inline Server
// Functions that were rendered during the prerender.
async function getGreeting() {
'use cache'
return 'Hello'
}
async function Greeting() {
return <h1>{await getGreeting()}</h1>
}
// An inline Server Function that closes over a prop is compiled to
// `action.bind(null, encryptActionBoundArgs(id, locale))`. At request time the
// encrypted bound args are found in the Resume Data Cache.
function SearchForm({ locale }: { locale: string }) {
async function onSubmit(formData: FormData) {
'use server'
console.log(locale, formData.get('q'))
}
return (
<form action={onSubmit}>
<input name="q" />
<button type="submit">Go</button>
</form>
)
}
// Request-time data inside Suspense: the page gets a static shell and is
// resumed per request.
async function DynamicContent() {
await cookies()
return <p id="dynamic-content">Dynamic content</p>
}
export default function Page() {
return (
<main>
<Greeting />
<SearchForm locale="en" />
<Suspense fallback={<p>Loading...</p>}>
<DynamicContent />
</Suspense>
</main>
)
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
cacheComponents: true,
partialPrefetching: true,
}
module.exports = nextConfig
import { nextTestSetup } from 'e2e-utils'
describe('partial prefetching with an inline server function', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('terminates the HTML response of a partially prerendered page', async () => {
// The page renders an inline Server Function with bound args whose
// encrypted value is served from the Resume Data Cache. Before the fix,
// `encryptActionBoundArgs` left the request's CacheSignal read open on that
// cache hit, so the runtime-prefetch stream embedded in the RSC payload was
// never closed and reading the body never resolved.
const res = await next.fetch('/')
expect(res.status).toBe(200)
const html = await res.text()
expect(html).toContain('Dynamic content')
expect(html.trimEnd().endsWith('</html>')).toBe(true)
})
})
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