Repository navigation
Docs: clarify prerender errors for time, randomness, and crypto - #102245
Conversation
|
Checked (4)
|
Tests PassedCommit: fb4e795 |
| #### Pass a server-generated value to the client | ||
|
|
||
| If the random value needs to be part of the server-rendered HTML (not deferred to after hydration), the component can call [`Math.random()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random) during render as long as a [`<Suspense>`](https://react.dev/reference/react/Suspense) boundary wraps it from the parent. Next.js prerenders the fallback and fills in the real component at request time. See [`Math.random()` in a Client Component](/docs/messages/blocking-prerender-random-client) for the full recipe. | ||
| If the random value needs to appear in the server-rendered HTML, [generate it on the server](#generate-on-every-request) and [pass it as a prop](/docs/app/getting-started/server-and-client-components#passing-data-from-server-to-client-components) so the first browser render uses the same value. |
There was a problem hiding this comment.
Note
Unverified · Minor — Linked -client pages still recommend the Suspense recipe this PR drops
The random page replaces the 'Inline render with a parent <Suspense> boundary' pattern with 'pass a server-generated value as a prop'. The PR description gives the reason: recomputing the value in a Client Component caused React #418. The random page still links to blocking-prerender-random-client from Learn more and from its unchanged Gotchas line, and that page still recommends wrapping in Suspense (line 56). Its line 189 says to use Suspense when the value must be 'hydration-stable'. blocking-prerender-current-time-client line 68 gives the same advice. Readers now get conflicting fixes depending on which page they land on. This is a judgment call: those pages aren't in the diff, and the base already had the same advice there.
Not checked by a repro: Its trace points at lines the head has, but a trace isn’t run: follow it to judge.
Unverified: its repro didn’t settle it, so a person should judge it.
|
Summary
Docs-only change. It rewrites the six sync-IO prerender error pages, the Cache Components migration step and the Questions for the author (2)
Issues (1)
Checked (6)
Review steps
Confirmed: its repro failed on this commit and not the same way on the base. Unverified: a person should judge it. |
| ### Gotchas | ||
|
|
||
| - A Client Component that produces a random value inline during render still trips this error during SSR. See the dedicated [`Math.random()` in a Client Component](/docs/messages/blocking-prerender-random-client) page for the [`<Suspense>`](https://react.dev/reference/react/Suspense) and effect-based recipes. | ||
| - A parent `<Suspense>` boundary can show a fallback during prerendering. If the Client Component renders at request time, the boundary does not preserve its random value for hydration. [Pass the server value as a prop](#pass-a-server-generated-value-to-the-client) instead of calling `Math.random()` again in the browser. |
There was a problem hiding this comment.
Note
Unverified · Major — New Math.random gotcha contradicts the unchanged *-client pages' Suspense fix
The PR removes the 'Inline render with a parent <Suspense> boundary' pattern from blocking-prerender-random.mdx. In its place, a gotcha says a parent Suspense boundary 'does not preserve its random value for hydration', and the description reports React #418 for that setup. But blocking-prerender-random-client.mdx (and current-time-client) still recommends exactly that fix as its primary one: 'Wrap in or move into Suspense… Next.js fills in the real component when the browser hydrates'. Its gotcha also says 'If the value needs to be hydration-stable… use Wrap in or move into Suspense instead'. The current-time and crypto pages still point readers to those 'Suspense and effect-based recipes'. A reader who follows those links implements the pattern this PR calls broken. REVIEW.md's no-stale-docs rule (review/rules/no-stale-docs.md) treats this as medium: 'Prose, headings, snippets, expected output, or related pages giving incompatible instructions'. The docs rubric (review/areas/docs.…
Not checked by a repro: Its trace points at lines the head has, but a trace isn’t run: follow it to judge.
Unverified: its repro didn’t settle it, so a person should judge it.
When a Server Component calls a synchronous Node crypto API such as `randomBytes(16)` during prerendering, the terminal names the right function but links to the `Math.random()` error page. The dev overlay already links the same call to the crypto page. Classify the seven instrumented `node:crypto` calls as crypto so the terminal prints the same link: ```diff -Learn more: https://nextjs.org/docs/messages/blocking-prerender-random +Learn more: https://nextjs.org/docs/messages/blocking-prerender-crypto ``` ## Verification - Exercised all seven Node API wrappers against the diagnostic factories before and after the change. Checked the server, runtime-prerender, and client error URLs, with `Math.random()` and callback-based `randomBytes` and `randomInt` as controls. - Updated the 32 affected production build snapshots across eight routes in the existing end-to-end suite. The four `getRandomValues` snapshots already pointed to the crypto page. Found while reviewing #102245. This fix can merge independently. <!-- NEXT_JS_LLM -->
| ## Wrap in or move into Suspense | ||
|
|
||
| Choose this fix when the random value is part of the rendered output and a brief fallback during SSR is acceptable. Wrap the consuming Client Component in [`<Suspense>`](https://react.dev/reference/react/Suspense) from its parent. The fallback ships in the prerendered HTML, and Next.js fills in the real component when the browser hydrates. | ||
| Choose this fix when the random value is part of the rendered output and a brief fallback during SSR is acceptable. Wrap the consuming Client Component in [`<Suspense>`](https://react.dev/reference/react/Suspense) from its parent. Next.js can prerender the fallback. If the Client Component renders on the server at request time, the browser can compute a different random value during hydration. |
There was a problem hiding this comment.
Note
Unverified · Minor — Client error pages still recommend the Suspense recipe that now admits mismatch
The change corrects the Server Component page's gotcha to say a parent <Suspense> boundary does not preserve a random value for hydration, so use a server prop instead (random.mdx:226). The intro of the matching client section now also says the browser can compute a different value. But random-client.mdx and crypto-client.mdx still present 'Wrap in or move into Suspense' as a fix for a value that is 'part of the rendered output', and their examples call Math.random()/crypto.randomUUID() inline with no suppressHydrationWarning (the time page has one). The PR description says this pattern produced React #418. Readers who follow the client page's first fix card get the hydration error that the sibling page warns about.
Not checked by a repro: Its trace points at lines the head has, but a trace isn’t run: follow it to judge.
Unverified: its repro didn’t settle it, so a person should judge it.
Vercel Agent Verified Review reviewed a newer commit and didn’t approve it: dismissing its approval of an earlier commit.
<!-- NEXT_JS_LLM -->
<!-- NEXT_JS_LLM -->
<!-- NEXT_JS_LLM -->
<!-- NEXT_JS_LLM -->
<!-- NEXT_JS_LLM -->
<!-- NEXT_JS_LLM -->
<!-- NEXT_JS_LLM -->
<!-- NEXT_JS_LLM -->
<!-- NEXT_JS_LLM -->
<!-- NEXT_JS_LLM -->
<!-- NEXT_JS_LLM -->
<!-- NEXT_JS_LLM -->
<!-- NEXT_JS_LLM -->
<!-- NEXT_JS_LLM -->
<!-- NEXT_JS_LLM -->
2ea8df8 to
c4ff224
Compare
<!-- NEXT_JS_LLM -->
<!-- NEXT_JS_LLM -->
<!-- NEXT_JS_LLM -->
<!-- NEXT_JS_LLM -->
<!-- NEXT_JS_LLM -->
<!-- NEXT_JS_LLM -->
<!-- NEXT_JS_LLM -->
<!-- NEXT_JS_LLM -->
<!-- NEXT_JS_LLM -->
<!-- NEXT_JS_LLM -->
<!-- NEXT_JS_LLM -->
<!-- NEXT_JS_LLM -->
<!-- NEXT_JS_LLM -->
Summary
Correct the sync IO guidance across the Cache Components migration guide, the six error pages, and the
connection()reference. Build on the browser-only rendering guidance merged in #102401: lead withuse(browser())for browser-only library rendering, distinguish module imports and lazy loading, and link to the SPA guide for those details.Server examples suspend before a fresh read with
await io()under a parent Suspense boundary, retainconnection()as an alternative, and show caching for reusable values. The three Client Component pages first show where to wrap the component in<Suspense>. For values that client code also needs, pass the server value as a prop so hydration reuses it. The migration guide makes the same server/client distinction.When a package is named, #102471 links to the external-library section on the matching error page. Each section distinguishes the library calls or inputs that can be cached on the server from reads that must be fresh or happen during client rendering. The two clock errors also link directly to the existing TanStack Query guide: cache the hydration timestamp on the server or wrap the query component in Suspense on the client. The request ID label stays in the shell while only its value loads. Clarify that a driver can read time or randomness before its async query suspends, and that request APIs may resolve during runtime prerendering with Partial Prefetching. Separate data generation and timing instrumentation from component rendering so the examples pass React's purity rule.
Verification