Unverified Commit f2610392 authored by Josh Story's avatar Josh Story Committed by GitHub

[dynamicIO] Don't abort prospective render on sync access during SSR (#81258)

When filling caches we never want to let sync IO interrupt the cache
filling render. We already handled this for the react-server render
where most caches live. But modules are a form of caches and they
similarly rely on the prospective render to "fill" their entry. Prior to
this change during SSR prerenders a sync IO could cause the prospective
render to abort early. This led to the case where the final render would
also potentially abort early for something like a serial sync IO in
module scope.
parent 2d19dc62
...@@ -2433,14 +2433,15 @@ async function spawnDynamicValidationInDev( ...@@ -2433,14 +2433,15 @@ async function spawnDynamicValidationInDev(
} }
if (initialServerResult) { if (initialServerResult) {
const initialClientController = new AbortController() const initialClientRenderController = new AbortController()
const initialClientPrerenderController = new AbortController()
const initialClientPrerenderStore: PrerenderStore = { const initialClientPrerenderStore: PrerenderStore = {
type: 'prerender-client', type: 'prerender-client',
phase: 'render', phase: 'render',
rootParams, rootParams,
implicitTags, implicitTags,
renderSignal: initialClientController.signal, renderSignal: initialClientRenderController.signal,
controller: initialClientController, controller: initialClientPrerenderController,
// For HTML Generation the only cache tracked activity // For HTML Generation the only cache tracked activity
// is module loading, which has it's own cache signal // is module loading, which has it's own cache signal
cacheSignal: null, cacheSignal: null,
...@@ -2471,7 +2472,7 @@ async function spawnDynamicValidationInDev( ...@@ -2471,7 +2472,7 @@ async function spawnDynamicValidationInDev(
nonce={nonce} nonce={nonce}
/>, />,
{ {
signal: initialClientController.signal, signal: initialClientRenderController.signal,
onError: (err) => { onError: (err) => {
const digest = getDigestForWellKnownError(err) const digest = getDigestForWellKnownError(err)
...@@ -2485,7 +2486,7 @@ async function spawnDynamicValidationInDev( ...@@ -2485,7 +2486,7 @@ async function spawnDynamicValidationInDev(
return undefined return undefined
} }
if (initialClientController.signal.aborted) { if (initialClientRenderController.signal.aborted) {
// These are expected errors that might error the prerender. we ignore them. // These are expected errors that might error the prerender. we ignore them.
} else if ( } else if (
process.env.NEXT_DEBUG_BUILD || process.env.NEXT_DEBUG_BUILD ||
...@@ -2521,7 +2522,7 @@ async function spawnDynamicValidationInDev( ...@@ -2521,7 +2522,7 @@ async function spawnDynamicValidationInDev(
// Promises passed to client were already awaited above (assuming that they came from cached functions) // Promises passed to client were already awaited above (assuming that they came from cached functions)
trackPendingModules(cacheSignal) trackPendingModules(cacheSignal)
await cacheSignal.cacheReady() await cacheSignal.cacheReady()
initialClientController.abort() initialClientRenderController.abort()
} }
const finalServerController = new AbortController() const finalServerController = new AbortController()
...@@ -3053,14 +3054,15 @@ async function prerenderToStream( ...@@ -3053,14 +3054,15 @@ async function prerenderToStream(
} }
if (initialServerResult) { if (initialServerResult) {
const initialClientController = new AbortController() const initialClientRenderController = new AbortController()
const initialClientPrerenderController = new AbortController()
const initialClientPrerenderStore: PrerenderStore = { const initialClientPrerenderStore: PrerenderStore = {
type: 'prerender-client', type: 'prerender-client',
phase: 'render', phase: 'render',
rootParams, rootParams,
implicitTags, implicitTags,
renderSignal: initialClientController.signal, renderSignal: initialClientRenderController.signal,
controller: initialClientController, controller: initialClientPrerenderController,
// For HTML Generation the only cache tracked activity // For HTML Generation the only cache tracked activity
// is module loading, which has it's own cache signal // is module loading, which has it's own cache signal
cacheSignal: null, cacheSignal: null,
...@@ -3091,7 +3093,7 @@ async function prerenderToStream( ...@@ -3091,7 +3093,7 @@ async function prerenderToStream(
nonce={nonce} nonce={nonce}
/>, />,
{ {
signal: initialClientController.signal, signal: initialClientRenderController.signal,
onError: (err) => { onError: (err) => {
const digest = getDigestForWellKnownError(err) const digest = getDigestForWellKnownError(err)
...@@ -3105,7 +3107,7 @@ async function prerenderToStream( ...@@ -3105,7 +3107,7 @@ async function prerenderToStream(
return undefined return undefined
} }
if (initialClientController.signal.aborted) { if (initialClientRenderController.signal.aborted) {
// These are expected errors that might error the prerender. we ignore them. // These are expected errors that might error the prerender. we ignore them.
} else if ( } else if (
process.env.NEXT_DEBUG_BUILD || process.env.NEXT_DEBUG_BUILD ||
...@@ -3140,7 +3142,7 @@ async function prerenderToStream( ...@@ -3140,7 +3142,7 @@ async function prerenderToStream(
// Promises passed to client were already awaited above (assuming that they came from cached functions) // Promises passed to client were already awaited above (assuming that they came from cached functions)
trackPendingModules(cacheSignal) trackPendingModules(cacheSignal)
await cacheSignal.cacheReady() await cacheSignal.cacheReady()
initialClientController.abort() initialClientRenderController.abort()
} }
let serverIsDynamic = false let serverIsDynamic = false
......
...@@ -25,6 +25,8 @@ describe('Lazy Module Init', () => { ...@@ -25,6 +25,8 @@ describe('Lazy Module Init', () => {
expect(next.cliOutput).toContain('○ /server ') expect(next.cliOutput).toContain('○ /server ')
expect(next.cliOutput).toContain('○ /client ') expect(next.cliOutput).toContain('○ /client ')
expect(next.cliOutput).toContain('◐ /client-page ')
expect(next.cliOutput).toContain('◐ /[dyn] ')
let $ let $
$ = await next.render$('/server') $ = await next.render$('/server')
...@@ -32,5 +34,14 @@ describe('Lazy Module Init', () => { ...@@ -32,5 +34,14 @@ describe('Lazy Module Init', () => {
$ = await next.render$('/client') $ = await next.render$('/client')
expect($('#id').text().length).toBeGreaterThan(0) expect($('#id').text().length).toBeGreaterThan(0)
$ = await next.render$('/client-page')
expect($('#id').text().length).toBeGreaterThan(0)
$ = await next.render$('/foo')
expect($('#id').text().length).toBeGreaterThan(0)
$ = await next.render$('/serial-client-sync-io')
expect($('#id').text().length).toBeGreaterThan(0)
}) })
}) })
export async function BuildID() {
const buildID = require('./lazy-id').buildID
await 1
return (
<dl>
<dt>Build ID</dt>
<dd id="id">{buildID}</dd>
</dl>
)
}
export default async function Indirection({
children,
}: {
children: React.ReactNode
}) {
return children
}
import { BuildID } from './build-id'
import Indirection from './indirection'
import { date as date1 } from './client-lazy-now-1'
import { date as date2 } from './client-lazy-now-2'
import { date as date3 } from './client-lazy-now-3'
export default async function Page() {
return (
<>
<p>
This page requires a module lazily during rendering. The Module computes
a "static" id using the current time and a random number. If these APIs
are used while prerendering they would normally trigger a synchronous
dynamic bailout. However since these APIs are used in module scope they
are not semantically part of the render and should be usable like other
"static" values. We demonstrate this with this fixture by asserting that
this page still produces a static result and it does not warn for
reading current time and random in a prerender.
</p>
<BuildID />
<Indirection>
<p>Date 1: {date1}</p>
</Indirection>
<Indirection>
<p>Date 2: {date2}</p>
</Indirection>
<Indirection>
<p>Date 3: {date3}</p>
</Indirection>
</>
)
}
export default async function Indirection({
children,
}: {
children: React.ReactNode
}) {
return children
}
...@@ -4,14 +4,13 @@ export default async function Page() { ...@@ -4,14 +4,13 @@ export default async function Page() {
return ( return (
<> <>
<p> <p>
This page requires a module lazily during rendering. The Module computes This page has several client modules that have sync IO in the module
a "static" id using the current time and a random number. If these APIs scope that will run in serial when prerendering to HTML. The point of
are used while prerendering they would normally trigger a synchronous this test is to assert that the serial nature does not lead to
dynamic bailout. However since these APIs are used in module scope they unexpected sync IO errors. In the past this particular setup would show
are not semantically part of the render and should be usable like other up as a sync IO error because the later modules did not initialize
"static" values. We demonstrate this with this fixture by asserting that during the prospective render and thus appeared to use sync IO when
this page still produces a static result and it does not warn for being initialized during the final render.
reading current time and random in a prerender.
</p> </p>
<BuildID /> <BuildID />
</> </>
......
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