Unverified Commit 08793dd9 authored by Wyatt Johnson's avatar Wyatt Johnson Committed by GitHub

[ppr] Handle corrupted postponed state (#67021)

Previously, when the provided postponed state was corrupted, the render
failed which resulted in a hydration error. Instead, when the postponed
state is corrupted, we send the resume stream just for the hydration
where it'll attempt to render client side with the data that's already
available.
parent a3c36fe2
...@@ -88,8 +88,10 @@ import { getAssetQueryString } from './get-asset-query-string' ...@@ -88,8 +88,10 @@ import { getAssetQueryString } from './get-asset-query-string'
import { setReferenceManifestsSingleton } from './encryption-utils' import { setReferenceManifestsSingleton } from './encryption-utils'
import { import {
createStaticRenderer, createStaticRenderer,
DYNAMIC_DATA,
getDynamicDataPostponedState, getDynamicDataPostponedState,
getDynamicHTMLPostponedState, getDynamicHTMLPostponedState,
type PostponedState,
} from './static/static-renderer' } from './static/static-renderer'
import { isDynamicServerError } from '../../client/components/hooks-server-context' import { isDynamicServerError } from '../../client/components/hooks-server-context'
import { import {
...@@ -1018,15 +1020,24 @@ async function renderToHTMLOrFlightImpl( ...@@ -1018,15 +1020,24 @@ async function renderToHTMLOrFlightImpl(
tracingMetadata: tracingMetadata, tracingMetadata: tracingMetadata,
}) })
let postponed: PostponedState | null = null
// If provided, the postpone state should be parsed as JSON so it can be
// provided to React.
if (typeof renderOpts.postponed === 'string') {
try {
postponed = JSON.parse(renderOpts.postponed)
} catch {
// If we failed to parse the postponed state, we should default to
// performing a dynamic data render.
postponed = DYNAMIC_DATA
}
}
const renderer = createStaticRenderer({ const renderer = createStaticRenderer({
isRoutePPREnabled, isRoutePPREnabled,
isStaticGeneration, isStaticGeneration,
// If provided, the postpone state should be parsed as JSON so it can be postponed,
// provided to React.
postponed:
typeof renderOpts.postponed === 'string'
? JSON.parse(renderOpts.postponed)
: null,
streamOptions: { streamOptions: {
onError: htmlRendererErrorHandler, onError: htmlRendererErrorHandler,
onHeaders, onHeaders,
...@@ -1042,7 +1053,7 @@ async function renderToHTMLOrFlightImpl( ...@@ -1042,7 +1053,7 @@ async function renderToHTMLOrFlightImpl(
}) })
try { try {
let { stream, postponed, resumed } = await renderer.render(children) const result = await renderer.render(children)
const prerenderState = staticGenerationStore.prerenderState const prerenderState = staticGenerationStore.prerenderState
if (prerenderState) { if (prerenderState) {
...@@ -1063,10 +1074,10 @@ async function renderToHTMLOrFlightImpl( ...@@ -1063,10 +1074,10 @@ async function renderToHTMLOrFlightImpl(
// First we check if we have any dynamic holes in our HTML prerender // First we check if we have any dynamic holes in our HTML prerender
if (usedDynamicAPIs(prerenderState)) { if (usedDynamicAPIs(prerenderState)) {
if (postponed != null) { if (result.postponed != null) {
// This is the Dynamic HTML case. // This is the Dynamic HTML case.
metadata.postponed = JSON.stringify( metadata.postponed = JSON.stringify(
getDynamicHTMLPostponedState(postponed) getDynamicHTMLPostponedState(result.postponed)
) )
} else { } else {
// This is the Dynamic Data case // This is the Dynamic Data case
...@@ -1079,7 +1090,7 @@ async function renderToHTMLOrFlightImpl( ...@@ -1079,7 +1090,7 @@ async function renderToHTMLOrFlightImpl(
// It is possible in the set of stream transforms for Dynamic HTML vs Dynamic Data may differ but currently both states // It is possible in the set of stream transforms for Dynamic HTML vs Dynamic Data may differ but currently both states
// require the same set so we unify the code path here // require the same set so we unify the code path here
return { return {
stream: await continueDynamicPrerender(stream, { stream: await continueDynamicPrerender(result.stream, {
getServerInsertedHTML, getServerInsertedHTML,
}), }),
} }
...@@ -1093,10 +1104,10 @@ async function renderToHTMLOrFlightImpl( ...@@ -1093,10 +1104,10 @@ async function renderToHTMLOrFlightImpl(
if (usedDynamicAPIs(prerenderState)) { if (usedDynamicAPIs(prerenderState)) {
// This is the same logic above just repeated after ensuring the RSC stream itself has completed // This is the same logic above just repeated after ensuring the RSC stream itself has completed
if (postponed != null) { if (result.postponed != null) {
// This is the Dynamic HTML case. // This is the Dynamic HTML case.
metadata.postponed = JSON.stringify( metadata.postponed = JSON.stringify(
getDynamicHTMLPostponedState(postponed) getDynamicHTMLPostponedState(result.postponed)
) )
} else { } else {
// This is the Dynamic Data case // This is the Dynamic Data case
...@@ -1109,14 +1120,14 @@ async function renderToHTMLOrFlightImpl( ...@@ -1109,14 +1120,14 @@ async function renderToHTMLOrFlightImpl(
// It is possible in the set of stream transforms for Dynamic HTML vs Dynamic Data may differ but currently both states // It is possible in the set of stream transforms for Dynamic HTML vs Dynamic Data may differ but currently both states
// require the same set so we unify the code path here // require the same set so we unify the code path here
return { return {
stream: await continueDynamicPrerender(stream, { stream: await continueDynamicPrerender(result.stream, {
getServerInsertedHTML, getServerInsertedHTML,
}), }),
} }
} else { } else {
// This is the Static case // This is the Static case
// We still have not used any dynamic APIs. At this point we can produce an entirely static prerender response // We still have not used any dynamic APIs. At this point we can produce an entirely static prerender response
let renderedHTMLStream = stream let renderedHTMLStream = result.stream
if (staticGenerationStore.forceDynamic) { if (staticGenerationStore.forceDynamic) {
throw new StaticGenBailoutError( throw new StaticGenBailoutError(
...@@ -1124,13 +1135,13 @@ async function renderToHTMLOrFlightImpl( ...@@ -1124,13 +1135,13 @@ async function renderToHTMLOrFlightImpl(
) )
} }
if (postponed != null) { if (result.postponed != null) {
// We postponed but nothing dynamic was used. We resume the render now and immediately abort it // We postponed but nothing dynamic was used. We resume the render now and immediately abort it
// so we can set all the postponed boundaries to client render mode before we store the HTML response // so we can set all the postponed boundaries to client render mode before we store the HTML response
const resumeRenderer = createStaticRenderer({ const resumeRenderer = createStaticRenderer({
isRoutePPREnabled, isRoutePPREnabled,
isStaticGeneration: false, isStaticGeneration: false,
postponed: getDynamicHTMLPostponedState(postponed), postponed: getDynamicHTMLPostponedState(result.postponed),
streamOptions: { streamOptions: {
signal: createPostponedAbortSignal( signal: createPostponedAbortSignal(
'static prerender resume' 'static prerender resume'
...@@ -1165,7 +1176,7 @@ async function renderToHTMLOrFlightImpl( ...@@ -1165,7 +1176,7 @@ async function renderToHTMLOrFlightImpl(
const { stream: resumeStream } = const { stream: resumeStream } =
await resumeRenderer.render(resumeChildren) await resumeRenderer.render(resumeChildren)
// First we write everything from the prerender, then we write everything from the aborted resume render // First we write everything from the prerender, then we write everything from the aborted resume render
renderedHTMLStream = chainStreams(stream, resumeStream) renderedHTMLStream = chainStreams(result.stream, resumeStream)
} }
return { return {
...@@ -1187,10 +1198,10 @@ async function renderToHTMLOrFlightImpl( ...@@ -1187,10 +1198,10 @@ async function renderToHTMLOrFlightImpl(
nonce, nonce,
formState formState
) )
if (resumed) { if (result.resumed) {
// We have new HTML to stream and we also need to include server inserted HTML // We have new HTML to stream and we also need to include server inserted HTML
return { return {
stream: await continueDynamicHTMLResume(stream, { stream: await continueDynamicHTMLResume(result.stream, {
inlinedDataStream, inlinedDataStream,
getServerInsertedHTML, getServerInsertedHTML,
}), }),
...@@ -1198,7 +1209,7 @@ async function renderToHTMLOrFlightImpl( ...@@ -1198,7 +1209,7 @@ async function renderToHTMLOrFlightImpl(
} else { } else {
// We are continuing a Dynamic Data Prerender and simply need to append new inlined flight data // We are continuing a Dynamic Data Prerender and simply need to append new inlined flight data
return { return {
stream: await continueDynamicDataResume(stream, { stream: await continueDynamicDataResume(result.stream, {
inlinedDataStream, inlinedDataStream,
}), }),
} }
...@@ -1208,7 +1219,7 @@ async function renderToHTMLOrFlightImpl( ...@@ -1208,7 +1219,7 @@ async function renderToHTMLOrFlightImpl(
// @TODO factor this further to make the render types more clearly defined and remove // @TODO factor this further to make the render types more clearly defined and remove
// the deluge of optional params that passed to configure the various behaviors // the deluge of optional params that passed to configure the various behaviors
return { return {
stream: await continueFizzStream(stream, { stream: await continueFizzStream(result.stream, {
inlinedDataStream: createInlinedDataReadableStream( inlinedDataStream: createInlinedDataReadableStream(
dataStream, dataStream,
nonce, nonce,
......
...@@ -228,6 +228,27 @@ describe('required server files app router', () => { ...@@ -228,6 +228,27 @@ describe('required server files app router', () => {
}) })
}) })
it('should still render when postponed is corrupted', async () => {
const random = Math.random().toString(36).substring(2)
const res = await fetchViaHTTP(appPort, '/dyn/' + random, undefined, {
method: 'POST',
headers: {
'x-matched-path': '/_next/postponed/resume/dyn/' + random,
},
// This is a corrupted postponed JSON payload.
body: '{',
})
expect(res.status).toBe(200)
const html = await res.text()
// Expect that the closing HTML tag is still present, indicating a
// successful render.
expect(html).toContain('</html>')
})
it('should send cache tags in minimal mode for ISR', async () => { it('should send cache tags in minimal mode for ISR', async () => {
for (const [path, tags] of [ for (const [path, tags] of [
[ [
......
...@@ -15997,7 +15997,8 @@ ...@@ -15997,7 +15997,8 @@
"required server files app router should properly handle fallback for bot request", "required server files app router should properly handle fallback for bot request",
"required server files app router should properly handle prerender for bot request", "required server files app router should properly handle prerender for bot request",
"required server files app router should properly stream resume", "required server files app router should properly stream resume",
"required server files app router should send cache tags in minimal mode for ISR" "required server files app router should send cache tags in minimal mode for ISR",
"required server files app router should still render when postponed is corrupted"
], ],
"pending": [], "pending": [],
"flakey": [], "flakey": [],
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