Unverified Commit 3c6591c1 authored by Josh Story's avatar Josh Story Committed by GitHub

[dynamicIO] use RSC dynamicness to control partial vs complete PPR result (#81627)

Historically anything dynamic in a prerender would result in a partially
static shell that resumes at runtime. With this change, now partial
shells are only possible when RSC is dynamic. If your client prerender
has any IO but your RSC tree was entirely prerenderable then Next.js
will produce a complete static result and not perform a resume at
runtime. This simplifies how you reason about what will and wil not make
a static page. It also means that anything dynamic in the client layer
will only render in browser.

---------
Co-authored-by: 's avatarHendrik Liebau <mail@hendrik-liebau.de>
parent 17675a2f
...@@ -3264,7 +3264,6 @@ async function prerenderToStream( ...@@ -3264,7 +3264,6 @@ async function prerenderToStream(
captureOwnerStack: undefined, // Not available in production. captureOwnerStack: undefined, // Not available in production.
} }
let clientIsDynamic = false
let dynamicValidation = createDynamicValidationState() let dynamicValidation = createDynamicValidationState()
const prerender = ( const prerender = (
...@@ -3291,8 +3290,6 @@ async function prerenderToStream( ...@@ -3291,8 +3290,6 @@ async function prerenderToStream(
isPrerenderInterruptedError(err) || isPrerenderInterruptedError(err) ||
finalClientController.signal.aborted finalClientController.signal.aborted
) { ) {
clientIsDynamic = true
const componentStack: string | undefined = ( const componentStack: string | undefined = (
errorInfo as any errorInfo as any
).componentStack ).componentStack
...@@ -3356,7 +3353,12 @@ async function prerenderToStream( ...@@ -3356,7 +3353,12 @@ async function prerenderToStream(
fallbackRouteParams fallbackRouteParams
) )
if (serverIsDynamic || clientIsDynamic) { if (serverIsDynamic) {
// Dynamic case
// We will always need to perform a "resume" render of some kind when this route is accessed
// because the RSC data itself is dynamic. We determine if there are any HTML holes or not
// but generally this is a "partial" prerender in that there will be a per-request compute
// concatenated to the static shell.
if (postponed != null) { if (postponed != null) {
// Dynamic HTML case // Dynamic HTML case
metadata.postponed = await getDynamicHTMLPostponedState( metadata.postponed = await getDynamicHTMLPostponedState(
...@@ -3390,6 +3392,8 @@ async function prerenderToStream( ...@@ -3390,6 +3392,8 @@ async function prerenderToStream(
} }
} else { } else {
// Static case // Static case
// We will not perform resumption per request. The result can be served statically to the requestor
// and if there was anything dynamic it will only be rendered in the browser.
if (workStore.forceDynamic) { if (workStore.forceDynamic) {
throw new StaticGenBailoutError( throw new StaticGenBailoutError(
'Invariant: a Page with `dynamic = "force-dynamic"` did not trigger the dynamic pathway. This is a bug in Next.js' 'Invariant: a Page with `dynamic = "force-dynamic"` did not trigger the dynamic pathway. This is a bug in Next.js'
......
'use client'
export function Time() {
return (
<p>
The time is{' '}
<span id="time" suppressHydrationWarning>
{new Date().toISOString()}
</span>
</p>
)
}
import { Suspense } from 'react'
import { getSentinelValue } from '../../getSentinelValue'
import { Time } from './client'
export default function Page() {
return (
<>
<p>
This page is static in RSC but dynamic in the client. It should serve a
static fallback that updates in the browser.
</p>
<Suspense fallback={<span>Loading...</span>}>
<Time />
</Suspense>
<div id="page">{getSentinelValue()}</div>
</>
)
}
/* eslint-disable jest/no-standalone-expect */ /* eslint-disable jest/no-standalone-expect */
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
import cheerio from 'cheerio'
describe('dynamic-io', () => { describe('dynamic-io', () => {
const { next, isNextDev, skipped } = nextTestSetup({ const { next, isNextDev, skipped } = nextTestSetup({
...@@ -330,4 +331,46 @@ describe('dynamic-io', () => { ...@@ -330,4 +331,46 @@ describe('dynamic-io', () => {
expect($('#page-children').text()).toBe('at buildtime') expect($('#page-children').text()).toBe('at buildtime')
} }
}) })
it('should not resume when client components are dynamic but the RSC render was static', async () => {
let html = await next.render('/cases/static-rsc-dynamic-client', {})
const $ = cheerio.load(html)
// Confirm the HTML document was sent completely
expect(html).toContain('</body></html>')
if (isNextDev) {
expect($('#layout').text()).toBe('at runtime')
expect($('#page').text()).toBe('at runtime')
// In dev we SSR the time
expect($('#time').length).toBe(1)
} else {
expect($('#layout').text()).toBe('at buildtime')
expect($('#page').text()).toBe('at buildtime')
// Confirm the time span is not part of the completed HTML document
expect($('#time').length).toBe(0)
}
const browser = await next.browser('/cases/static-rsc-dynamic-client')
const now = new Date()
if (isNextDev) {
expect(await browser.elementById('layout').text()).toBe('at runtime')
expect(await browser.elementById('page').text()).toBe('at runtime')
// Assert that we rendered a time within the last couple seconds.
const inPageDate = new Date(
await browser.waitForElementByCss('#time').text()
)
expect(inPageDate.getTime() - now.getTime()).toBeLessThan(2000)
} else {
expect(await browser.elementById('layout').text()).toBe('at buildtime')
expect(await browser.elementById('page').text()).toBe('at buildtime')
// Assert that we rendered a time within the last 2 seconds.
const inPageDate = new Date(
await browser.waitForElementByCss('#time').text()
)
expect(inPageDate.getTime() - now.getTime()).toBeLessThan(2000)
}
})
}) })
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