Unverified Commit 272ff8f6 authored by Josh Story's avatar Josh Story Committed by GitHub

[dynamicIO] prospectively render SSR phase if needed (#71506)

Currently dynamicIO is implemented without a prospective render of the
SSR portion. This is fine in most cases because we don't expect there to
be dynamic IO in SSR and you can't use cache APIs in this layer. However
the module scope of a lazily loaded module is semantically a static
scope but as implemented it woudl execute like it was dynamic. This
means things like triggering dynamic on reading the current time can
happen even when we would expect it not to.

The solution implemented here is to look at whether the SSR render has
synchronously dynamic results. If it does we perform the render a second
time. We only log during the second render. this way you see the
warnings once and only if they persist across render attempts. If you
read something like the current time in module scope during the first
render it won't log but on the second render the module is already
loaded so it also won't log and it won't abort the render early.
parent 0a916775
...@@ -23,8 +23,14 @@ describe('Lazy Module Init', () => { ...@@ -23,8 +23,14 @@ describe('Lazy Module Init', () => {
throw new Error('expected build not to fail for fully static project') throw new Error('expected build not to fail for fully static project')
} }
expect(next.cliOutput).toContain('○ / ') expect(next.cliOutput).toContain('○ /server ')
const $ = await next.render$('/') expect(next.cliOutput).toContain('○ /client ')
let $
$ = await next.render$('/server')
expect($('#id').text().length).toBeGreaterThan(0)
$ = await next.render$('/client')
expect($('#id').text().length).toBeGreaterThan(0) expect($('#id').text().length).toBeGreaterThan(0)
}) })
}) })
'use client'
export function BuildID() {
const buildID = require('./lazy-id').buildID
return (
<dl>
<dt>Build ID</dt>
<dd id="id">{buildID}</dd>
</dl>
)
}
export async function BuildID() { export async function BuildID() {
console.log('=== RENDER BUILD ID')
const buildID = require('./lazy-id').buildID const buildID = require('./lazy-id').buildID
await 1 await 1
return ( return (
......
import { BuildID } from './build-id'
export default async function Page() {
return (
<>
<p>
This page requires a module lazily during rendering. The Module computes
a "static" id using the current time and a random number. If these APIs
are used while prerendering they would normally trigger a synchronous
dynamic bailout. However since these APIs are used in module scope they
are not semantically part of the render and should be usable like other
"static" values. We demonstrate this with this fixture by asserting that
this page still produces a static result and it does not warn for
reading current time and random in a prerender.
</p>
<BuildID />
</>
)
}
...@@ -5,7 +5,7 @@ const nextConfig = { ...@@ -5,7 +5,7 @@ const nextConfig = {
experimental: { experimental: {
ppr: process.env.__NEXT_EXPERIMENTAL_PPR === 'true', ppr: process.env.__NEXT_EXPERIMENTAL_PPR === 'true',
dynamicIO: true, dynamicIO: true,
serverMinification: true, serverMinification: false,
}, },
} }
......
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