Skip to content

Commit b343b78

Browse files
committed
[dynamicIO] use new heuristic to track whether server render is dynamic
Instead of relying on onError just use the timing of the prerender resolution to determine whether the server render completed before aborting.
1 parent ef41607 commit b343b78

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

packages/next/src/server/app-render/app-render.tsx

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2716,11 +2716,12 @@ async function prerenderToStream(
27162716
ctx,
27172717
res.statusCode === 404
27182718
)
2719+
let prerenderIsPending = true
27192720
const reactServerResult = (reactServerPrerenderResult =
27202721
await createReactServerPrerenderResult(
27212722
prerenderAndAbortInSequentialTasks(
2722-
() =>
2723-
workUnitAsyncStorage.run(
2723+
async () => {
2724+
const prerenderResult = await workUnitAsyncStorage.run(
27242725
// The store to scope
27252726
finalRenderPrerenderStore,
27262727
// The function to run
@@ -2730,17 +2731,36 @@ async function prerenderToStream(
27302731
clientReferenceManifest.clientModules,
27312732
{
27322733
onError: (err: unknown) => {
2734+
// TODO we can remove this once https://github.com/facebook/react/pull/31715 lands
2735+
// because we won't have onError calls when halting the prerender
27332736
if (finalServerController.signal.aborted) {
2734-
serverIsDynamic = true
27352737
return
27362738
}
27372739

27382740
return serverComponentsErrorHandler(err)
27392741
},
27402742
signal: finalServerController.signal,
27412743
}
2742-
),
2744+
)
2745+
prerenderIsPending = false
2746+
return prerenderResult
2747+
},
27432748
() => {
2749+
if (
2750+
finalServerController.signal.aborted ||
2751+
prerenderIsPending
2752+
) {
2753+
// If the server controller is already aborted we must have called something
2754+
// that required aborting the prerender synchronously such as with new Date()
2755+
serverIsDynamic = true
2756+
return
2757+
}
2758+
2759+
if (prerenderIsPending) {
2760+
// If prerenderIsPending then we have blocked for longer than a Task and we assume
2761+
// there is something unfinished.
2762+
serverIsDynamic = true
2763+
}
27442764
finalServerController.abort()
27452765
}
27462766
)

0 commit comments

Comments
 (0)