You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
docs: add guidance on choosing what to lazy load - #102472
Follow-up to #102401. Adds a way to choose between prerendering, use(browser()), and next/dynamic when you land on either guide.
Lazy Loading guide:
New "Choosing what to lazy load" section with a table that maps common situations to an approach.
"Skipping SSR" now says what ssr: false is for: lazy load a component and keep its module out of the server render.
New "Loading on interaction" section: what the loading placeholder does on a click, and how to show the component as soon as its code loads by updating state in a transition.
New "Preloading before interaction" section: a hidden <Activity>, or preloading on hover and focus through a shared loader.
Single-Page Applications guide:
A short table at the top of "Rendering components only in the browser" for choosing between use(browser()) and next/dynamic with ssr: false.
Why?
The two APIs were described, but the decision between them was spread across paragraphs, and nothing covered components that load after an interaction. That case has behavior worth knowing: with a loading placeholder the component appears with React's next batch of suspended content, even when its code is already loaded.
Verification
Measured in a repro app on a production build (Next.js 16.4.6, cacheComponents: true, Turbopack, headless Chrome). Time from click to content, with a 150ms delay on scripts requested after the click:
Setup
Content on page
Static import
1ms
next/dynamic with loading
303ms
next/dynamic without loading, in a transition
155ms
next/dynamic without loading, no transition, inside a page <Suspense>
308ms, section replaced by the boundary's fallback
Hidden <Activity> around next/dynamic
1ms
Preload on hover through a shared loader with React.lazy, click 300ms later
29ms
Preload on focus, Enter 300ms later
4ms
Also checked:
A route that doesn't render a component doesn't load its code.
A module that reads window at import fails next build behind use(browser()) and builds with next/dynamic and ssr: false.
Not run: Pages Router. The new sections are inside the App Router block of the shared guide.
Docs-only change. It adds a 'Choosing what to lazy load' table and two new sections, 'Loading on interaction' and 'Preloading before interaction', to the App Router part of the lazy-loading guide, and a short table to the SPA guide for choosing between use(browser()) and next/dynamic with ssr: false. I checked the claims against loadable.tsx, app-dynamic.tsx and the SWC next/dynamic transform, and ran two webpack next builds of the new examples with the head's build. I couldn't run a Turbopack build of a scratch app here because the workspace root couldn't be resolved outside the repository.
Questions for the author (1)
The hover/focus example uses dynamic(loadReplyBox), so the import() sits outside the dynamic() call. The SWC transform then emits no loadableGenerated, so SSR chunk and CSS preloading is skipped (dynamic.rs only records the specifier when the import is inline). The example builds and is gated behind client state, so it isn't SSR'd. But the Pages-only note in the same file (line 362) says the import() 'has to be inside the dynamic() call'. Should the new section say that this pattern…
Checked (4)
Check
Location
Passed
With neither loading nor ssr: false, next/dynamic adds no Suspense boundary, so the transition example keeps the current UI. I read hasSuspenseBoundary = !opts.ssr || !!opts.loading, with defaults loading: null and ssr: true, so the wrapper is a Fragment.
A gated next/dynamic component whose module reads window at import builds without ssr: false, as the 'Loading on interaction' section claims. I ran next build --webpack with the head's build on the section's ChatBox example, where chat-box imports a module doing window.innerWidth. It prerendered / without errors.
The shared-loader preload example, dynamic(loadReplyBox) with no inline import(), compiles and builds. I ran next build --webpack on the snippet copied verbatim and it built successfully. In dynamic.rs I read that a non-inline loader only skips loadableGenerated, with no error.
All new lazy-loading sections are inside the <AppOnly> block, so the Pages Router guide is unchanged. Grep shows <AppOnly> at lines 11–340, which contain the new headings at lines 26, 179 and 217.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What?
Follow-up to #102401. Adds a way to choose between prerendering,
use(browser()), andnext/dynamicwhen you land on either guide.Lazy Loading guide:
ssr: falseis for: lazy load a component and keep its module out of the server render.loadingplaceholder does on a click, and how to show the component as soon as its code loads by updating state in a transition.<Activity>, or preloading on hover and focus through a shared loader.Single-Page Applications guide:
use(browser())andnext/dynamicwithssr: false.Why?
The two APIs were described, but the decision between them was spread across paragraphs, and nothing covered components that load after an interaction. That case has behavior worth knowing: with a
loadingplaceholder the component appears with React's next batch of suspended content, even when its code is already loaded.Verification
Measured in a repro app on a production build (Next.js 16.4.6,
cacheComponents: true, Turbopack, headless Chrome). Time from click to content, with a 150ms delay on scripts requested after the click:next/dynamicwithloadingnext/dynamicwithoutloading, in a transitionnext/dynamicwithoutloading, no transition, inside a page<Suspense><Activity>aroundnext/dynamicReact.lazy, click 300ms laterAlso checked:
windowat import failsnext buildbehinduse(browser())and builds withnext/dynamicandssr: false.