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

[dynamicIO] reimplement dynamicIO validation on prerender (#79414)

This updates the dynamic validation implementation to more closely
follow the pattern used in prerenders for dynamicIO + PPR. Now that PPR
is the only way to use dynamicIO we only have one implementation and the
validation should match it as much as possible.
parent 90b6f19b
import { InvariantError } from '../../shared/lib/invariant-error' import { InvariantError } from '../../shared/lib/invariant-error'
import { isPrerenderInterruptedError } from './dynamic-rendering'
/** /**
* This is a utility function to make scheduling sequential tasks that run back to back easier. * This is a utility function to make scheduling sequential tasks that run back to back easier.
...@@ -32,95 +31,6 @@ export function prerenderAndAbortInSequentialTasks<R>( ...@@ -32,95 +31,6 @@ export function prerenderAndAbortInSequentialTasks<R>(
} }
} }
export function prerenderServerWithPhases(
signal: AbortSignal,
render: () => ReadableStream<Uint8Array>,
finalPhase: () => void
): Promise<ServerPrerenderStreamResult>
export function prerenderServerWithPhases(
signal: AbortSignal,
render: () => ReadableStream<Uint8Array>,
secondPhase: () => void,
finalPhase: () => void
): Promise<ServerPrerenderStreamResult>
export function prerenderServerWithPhases(
signal: AbortSignal,
render: () => ReadableStream<Uint8Array>,
secondPhase: () => void,
thirdPhase: () => void,
...remainingPhases: Array<() => void>
): Promise<ServerPrerenderStreamResult>
export function prerenderServerWithPhases(
signal: AbortSignal,
render: () => ReadableStream<Uint8Array>,
...remainingPhases: Array<() => void>
): Promise<ServerPrerenderStreamResult> {
if (process.env.NEXT_RUNTIME === 'edge') {
throw new InvariantError(
'`prerenderAndAbortInSequentialTasks` should not be called in edge runtime.'
)
} else {
return new Promise((resolve, reject) => {
let result: ServerPrerenderStreamResult
signal.addEventListener(
'abort',
() => {
if (isPrerenderInterruptedError(signal.reason)) {
result.markInterrupted()
} else {
result.markComplete()
}
},
{
once: true,
}
)
setImmediate(() => {
try {
result = new ServerPrerenderStreamResult(render())
} catch (err) {
reject(err)
}
})
function runFinalTask(this: () => void) {
try {
if (result) {
result.markComplete()
this()
}
resolve(result)
} catch (err) {
reject(err)
}
}
function runNextTask(this: () => void) {
try {
if (result) {
result.markPhase()
this()
}
} catch (err) {
reject(err)
}
}
let i = 0
for (; i < remainingPhases.length - 1; i++) {
const phase = remainingPhases[i]
setImmediate(runNextTask.bind(phase))
}
if (remainingPhases[i]) {
const finalPhase = remainingPhases[i]
setImmediate(runFinalTask.bind(finalPhase))
}
})
}
}
const PENDING = 0 const PENDING = 0
const COMPLETE = 1 const COMPLETE = 1
const INTERRUPTED = 2 const INTERRUPTED = 2
...@@ -283,71 +193,6 @@ class PhasedStream<T> extends ReadableStream<T> { ...@@ -283,71 +193,6 @@ class PhasedStream<T> extends ReadableStream<T> {
} }
} }
export function prerenderClientWithPhases<T>(
render: () => Promise<T>,
finalPhase: () => void
): Promise<T>
export function prerenderClientWithPhases<T>(
render: () => Promise<T>,
secondPhase: () => void,
finalPhase: () => void
): Promise<T>
export function prerenderClientWithPhases<T>(
render: () => Promise<T>,
secondPhase: () => void,
thirdPhase: () => void,
...remainingPhases: Array<() => void>
): Promise<T>
export function prerenderClientWithPhases<T>(
render: () => Promise<T>,
...remainingPhases: Array<() => void>
): Promise<T> {
if (process.env.NEXT_RUNTIME === 'edge') {
throw new InvariantError(
'`prerenderAndAbortInSequentialTasks` should not be called in edge runtime.'
)
} else {
return new Promise((resolve, reject) => {
let pendingResult: Promise<T>
setImmediate(() => {
try {
pendingResult = render()
pendingResult.catch((err) => reject(err))
} catch (err) {
reject(err)
}
})
function runFinalTask(this: () => void) {
try {
this()
resolve(pendingResult)
} catch (err) {
reject(err)
}
}
function runNextTask(this: () => void) {
try {
this()
} catch (err) {
reject(err)
}
}
let i = 0
for (; i < remainingPhases.length - 1; i++) {
const phase = remainingPhases[i]
setImmediate(runNextTask.bind(phase))
}
if (remainingPhases[i]) {
const finalPhase = remainingPhases[i]
setImmediate(runFinalTask.bind(finalPhase))
}
})
}
}
// React's RSC prerender function will emit an incomplete flight stream when using `prerender`. If the connection // React's RSC prerender function will emit an incomplete flight stream when using `prerender`. If the connection
// closes then whatever hanging chunks exist will be errored. This is because prerender (an experimental feature) // closes then whatever hanging chunks exist will be errored. This is because prerender (an experimental feature)
// has not yet implemented a concept of resume. For now we will simulate a paused connection by wrapping the stream // has not yet implemented a concept of resume. For now we will simulate a paused connection by wrapping the stream
......
...@@ -23,7 +23,7 @@ describe('Dynamic IO Dev Errors', () => { ...@@ -23,7 +23,7 @@ describe('Dynamic IO Dev Errors', () => {
"stack": [ "stack": [
"Page app/error/page.tsx (2:23)", "Page app/error/page.tsx (2:23)",
"JSON.parse <anonymous> (0:0)", "JSON.parse <anonymous> (0:0)",
"<unknown> <anonymous> (0:0)", "LogSafely <anonymous> (0:0)",
], ],
} }
`) `)
...@@ -49,7 +49,7 @@ describe('Dynamic IO Dev Errors', () => { ...@@ -49,7 +49,7 @@ describe('Dynamic IO Dev Errors', () => {
"stack": [ "stack": [
"Page app/error/page.tsx (2:23)", "Page app/error/page.tsx (2:23)",
"JSON.parse <anonymous> (0:0)", "JSON.parse <anonymous> (0:0)",
"<unknown> <anonymous> (0:0)", "LogSafely <anonymous> (0:0)",
], ],
} }
`) `)
...@@ -111,7 +111,7 @@ describe('Dynamic IO Dev Errors', () => { ...@@ -111,7 +111,7 @@ describe('Dynamic IO Dev Errors', () => {
"html <anonymous> (2:1)", "html <anonymous> (2:1)",
"Root [Server] <anonymous> (2:1)", "Root [Server] <anonymous> (2:1)",
"JSON.parse <anonymous> (0:0)", "JSON.parse <anonymous> (0:0)",
"<unknown> <anonymous> (0:0)", "LogSafely <anonymous> (0:0)",
], ],
} }
`) `)
......
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