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

[Cache Components] Reset PrerenderStore after generating RSC payload (#82286)

The RSC payload ideally would be constructed during the prerender.
However it is not currently sync and RSC doesn't provide a top level way
to produce an object child from a function at the root so we generate
the component tree before starting the render.

However we use the workUnitStore type to govern certain behaviors within
the RSC payload generation for instance preparing things like
searchParams and params props for layouts and pages.

So at the moment we scope the RSC generation inside the same
workUnitStore instance that will also scope the prerender. This is fine
except there are certain things that apply in the prerender context that
do not apply in the component tree generation. Namely we do not want to
track Sync IO as need to abort the prerender. This would lead to the
entire prerender being aborted before it even begins.

There are a few options. We could create a new workUnitStore type. This
isn't great b/c we have so many places where we need to exhaustively
check this.

We could use a new phase to similarly track whether we're before render
or not however this has the same problem of all places where phase is
considered needing to be aware of this additional possible phase.

Instead I think we ought to leave the logic of the code that determines
what to do simple and continue to track the sync IO but we just reset
the dynamic tracking and abort controllers so that the render starts
afresh with these.
parent 78d70501
/**
* This is a rather contrived test. The trick is to construct a scenario where
* Math.random is called during the create-component-tree phase but not during the render.
* To do this we patch Symbol.for. When the `react.client.reference` symbol is checked
* we know that this is happening in the create-component-tree function. Given the simple
* setup of the test page this symbol is not checked during the actual prerender itself.
*
* This test may flake in the future. It should be replaced ideally with a test that uses
* our OTel implementation to ensure that the built-in trace in create-component-tree does not
* early abort the prerender. The problem is that we are about to make these functions avoid the
* workUnitStore scope so it will not express the regression for other reasons.
*/
function patchSymbolFor() {
const isPatched = globalThis[Symbol.for('__SYMBOL_FOR_PATCHED__')]
if (!isPatched) {
console.log('patching')
globalThis[Symbol.for('__SYMBOL_FOR_PATCHED__')] = true
const originalSymbolFor = Symbol.for
Symbol.for = (...args) => {
if (args[0] === 'react.client.reference') {
Math.random()
}
return originalSymbolFor.apply(Symbol, args)
}
}
}
patchSymbolFor()
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Page() {
return (
<>
<p>
This test does some hacky stuff to cause a `Math.random()` call to
happen during the component tree creation which happens before the RSC
render. Before the fix that this test accompanied landed this kind of
sync IO was reported as a problem in Dev and in runtime during start. In
practice this would be because of an OTEL span generation during
create-component-tree. With this fix in place the create-component-tree
function will not leak any sync IO results into the final prerender.
</p>
<p>
In the long run we ought to move create-component-tree into the render
itself and ensure the tree structure comports with CacheComponent rules.
This test can be deleted at that time.
</p>
</>
)
}
import { isNextDev, nextTestSetup } from 'e2e-utils'
import { assertNoRedbox } from 'next-test-utils'
describe('hello-world', () => {
const { next } = nextTestSetup({
files: __dirname,
skipStart: !isNextDev,
skipDeployment: true,
})
if (isNextDev) {
it('should not indicate there is an error when incidental math.random calls occur during component tree generation during dev', async () => {
const browser = await next.browser('/')
await assertNoRedbox(browser)
// The redbox assertion is currently unreliable in this test and so this is an additional check to ensure the CLI didn't print anything with `Math.random()` in it.
expect(next.cliOutput).not.toContain('Math.random()')
})
} else {
it('should not indicate there is an error when incidental math.random calls occur during component tree generation during build', async () => {
try {
await next.build()
} catch (e) {
console.error('Expected build to Succeed, but it failed.')
throw e
}
expect(next.cliOutput).not.toContain('Math.random()')
})
}
})
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
cacheComponents: true,
},
}
module.exports = nextConfig
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