Skip to content

docs: add guidance on choosing what to lazy load - #102472

Open
icyJoseph wants to merge 12 commits into
canaryfrom
docs/lazy-loading-use-cases
Open

icyJoseph wants to merge 12 commits into
canaryfrom
docs/lazy-loading-use-cases

Conversation

@icyJoseph

Copy link
Copy Markdown
Member

What?

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.

@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Tests Passed

Commit: dbfcc4b
View workflow run

@github-actions github-actions Bot added created-by: Next.js team PRs by the Next.js team. Documentation Related to Next.js' official documentation. labels Nov 26, 2026
Comment thread docs/01-app/02-guides/lazy-loading.mdx Resolved
Comment thread docs/01-app/02-guides/lazy-loading.mdx Resolved
Comment thread docs/01-app/02-guides/lazy-loading.mdx Resolved
Comment thread docs/01-app/02-guides/lazy-loading.mdx Resolved
Comment thread docs/01-app/02-guides/lazy-loading.mdx Resolved
Comment thread docs/01-app/02-guides/lazy-loading.mdx Resolved
Comment thread docs/01-app/02-guides/lazy-loading.mdx Resolved
Comment thread docs/01-app/02-guides/lazy-loading.mdx Resolved
Comment thread docs/01-app/02-guides/lazy-loading.mdx Resolved
Comment thread docs/01-app/02-guides/lazy-loading.mdx Resolved
Comment thread docs/01-app/02-guides/lazy-loading.mdx Resolved
@icyJoseph
icyJoseph marked this pull request as ready for review November 27, 2026 13:20
@vercel

vercel Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Vercel Agent Verified Review ? approved this pull request.
dbfcc4b • Ran Fri, 27 Nov 2026 14:48:10 GMT • 2m 21s • Read the code review report ↗

Summary

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.
loadable.tsx:52
Passed 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.
lazy-loading.mdx:214
Passed 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.
lazy-loading.mdx:250
Passed 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.
lazy-loading.mdx:11
Review steps

     Step Time
Sandbox set up 13.5s
Agent review: 2 commands run 2m 7s
Repros run on the head and the base (0), findings graded (0) 1.3s

Comment thread docs/01-app/02-guides/lazy-loading.mdx Resolved

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

created-by: Next.js team PRs by the Next.js team. Documentation Related to Next.js' official documentation.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants