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

[dynamicIO] use new heuristic to track whether server render is dynamic (#73751)

Instead of relying on onError just use the timing of the prerender
resolution to determine whether the server render completed before
aborting.

React is going to remove onError and onPostpone calls from `prerender`
in flight [31715](https://github.com/facebook/react/pull/31715). This is
because semantically the error is going to be hidden from the prerender
and on resume it might not error. So rather than relying on whether the
error callback is called we simply infer whether we are done by checking
if the `prerender` resolved prior to our abort deadline. In our case it
is possible we also aborted earlier than the deadline because you can
call something like `Math.random()` which requires the `prerender` to
halt synchronously so we also check the signal status in our deadline
task.

We don't expect this to change any semantics so there are no altered
tests.

This is only currently implemented in the dynamicIO + PPR case because
that is the only case using experimental React with `prerender`. When
dynamicIO is on by itself we still use `renderToReadableStream` which
does not have halt semantics. We will switch to `prerender` when it is
available in React's stable channel
parent e0987559
...@@ -2716,11 +2716,12 @@ async function prerenderToStream( ...@@ -2716,11 +2716,12 @@ async function prerenderToStream(
ctx, ctx,
res.statusCode === 404 res.statusCode === 404
) )
let prerenderIsPending = true
const reactServerResult = (reactServerPrerenderResult = const reactServerResult = (reactServerPrerenderResult =
await createReactServerPrerenderResult( await createReactServerPrerenderResult(
prerenderAndAbortInSequentialTasks( prerenderAndAbortInSequentialTasks(
() => async () => {
workUnitAsyncStorage.run( const prerenderResult = await workUnitAsyncStorage.run(
// The store to scope // The store to scope
finalRenderPrerenderStore, finalRenderPrerenderStore,
// The function to run // The function to run
...@@ -2730,8 +2731,9 @@ async function prerenderToStream( ...@@ -2730,8 +2731,9 @@ async function prerenderToStream(
clientReferenceManifest.clientModules, clientReferenceManifest.clientModules,
{ {
onError: (err: unknown) => { onError: (err: unknown) => {
// TODO we can remove this once https://github.com/facebook/react/pull/31715 lands
// because we won't have onError calls when halting the prerender
if (finalServerController.signal.aborted) { if (finalServerController.signal.aborted) {
serverIsDynamic = true
return return
} }
...@@ -2739,8 +2741,23 @@ async function prerenderToStream( ...@@ -2739,8 +2741,23 @@ async function prerenderToStream(
}, },
signal: finalServerController.signal, signal: finalServerController.signal,
} }
), )
prerenderIsPending = false
return prerenderResult
},
() => { () => {
if (finalServerController.signal.aborted) {
// If the server controller is already aborted we must have called something
// that required aborting the prerender synchronously such as with new Date()
serverIsDynamic = true
return
}
if (prerenderIsPending) {
// If prerenderIsPending then we have blocked for longer than a Task and we assume
// there is something unfinished.
serverIsDynamic = true
}
finalServerController.abort() finalServerController.abort()
} }
) )
......
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