Repository navigation
docs: lead with use(browser()) for browser-only rendering - #102401
Conversation
Tests PassedCommit: 0eef717 |
19c436b to
fdb98ac
Compare
Code review ?
Summary
Docs-only change. The 'Rendering components only in the browser' section of the SPA guide now leads with use(browser()) inside a Suspense boundary. It keeps next/dynamic with ssr: false for keeping a module out of prerendering or lazy loading it, adds LCP and fallback guidance, and the Lazy Loading guide gets a cross-link. Since the earlier reviewed commit, one sentence changed: it now says ssr: false skips both rendering and importing the module on the server. Nothing was run; this is prose, so the review rests on reading and on comparing the claims with the SWC dynamic transform and loadable code. Checked (3)
|
There was a problem hiding this comment.
Vercel Agent Verified Review reviewed cfce5ab and isn’t approving it: this repository’s review rules call for a person’s review. 1 open finding is listed in its review summary and inline. View the full report
There was a problem hiding this comment.
Vercel Agent Verified Review approves 6d6d195: no issues found, and the change meets this repository’s review rules. View the full report
|
|
||
| Calling `use(browser())` guards the component's render. The component's module is still imported during prerendering, so code that reads `window` or `document` at the top level of a module, as some third-party libraries do, throws before the component renders. | ||
|
|
||
| To keep a module out of prerendering, or to lazy load the component's JavaScript, use [`next/dynamic`](/docs/app/guides/lazy-loading#nextdynamic) with `ssr: false`. It uses `React.lazy` with the dynamic `import()` you pass, so the browser downloads the JavaScript only when the component renders, and `ssr: false` skips the import on the server: |
There was a problem hiding this comment.
...and ssr: false skips the import on the server:
Right, we don't import/evaluate the module, but it also it is the lever to skip rendering — as phrased it is technically correct, you can't render if you don't even load the module, but do humans understand that layering?
) ## 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 with `use(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, retain `connection()` 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](https://github.com/vercel/next.js/blob/canary/docs/01-app/02-guides/client-side-data-fetching/tanstack-query.mdx#build-a-prerenderable-hydration-state) or [wrap the query component in Suspense on the client](https://github.com/vercel/next.js/blob/canary/docs/01-app/02-guides/client-side-data-fetching/tanstack-query.mdx#use-suspense-for-client-data). 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 - Parsed all 105 JS/TS snippets across the eight changed pages. All 92 JSX/TSX snippets pass React's purity, set-state-in-effect, and refs rules. - Prettier and MDX ESLint pass. The CI link checker's read-only validation passes for 317 internal links across the eight pages. All 17 overlay fix cards match their error pages in order, wording, snippets, and links. - [Runnable reproductions](aurorascharff/reproductions#3) and [hosted production build and browser checks](https://github.com/aurorascharff/reproductions/actions/runs/45258382066) on Next.js 16.4.6 previously verified server fixes, session prefetch after request data, and hydration. Recomputing a timestamp in a request-time Client Component produced React #418; serialized server props and post-mount reads hydrated cleanly. The latest documentation edits have not had a new browser run. <!-- NEXT_JS_LLM -->
What
Updates "Rendering components only in the browser" in the Single-Page Applications guide, plus a pointer from the Lazy Loading guide.
use(browser())inside a Suspense boundary as the way to render a component only in the browser.next/dynamicwithssr: falsefor large components that are not needed when the page loads, where the code splitting is the point.useEffectsuggestion.Why
The section presented
next/dynamicwithssr: falseas the default. That option also code splits, so the component's bundle is requested after hydration, andReact.lazysuspends on its first client render. The content then arrives as a retried Suspense boundary, which React reveals at most once every 300ms.With
use(browser())and a static import nothing suspends in the browser, so the component renders in the same pass that hydrates the page.Measured on a production build of a small repro (Next.js 16.4.6, React 19.3.0, headless Chrome, medians):
next/dynamicwithssr: falseuse(browser())in<Suspense>On a soft navigation, the
next/dynamicversion showed its fallback for about 300ms with the chunk already cached. Theuse(browser())version rendered the content directly.🤖 Generated with Claude Code