Skip to content

Enable lazy dynamic imports by default in development - #101873

Merged
sokra merged 2 commits into
canaryfrom
jimmym/lazy-dynamic-imports-defaults
Nov 24, 2026
Merged

sokra merged 2 commits into
canaryfrom
jimmym/lazy-dynamic-imports-defaults

Conversation

@jimmyhmiller

@jimmyhmiller jimmyhmiller commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Enable experimental.turbopackLazyDynamicImports and experimental.turbopackLazyDynamicImportsSSR by default in development so dynamic import targets are compiled only when used.

Stacked on #101937, which contains the prerequisite bugfixes and should merge first. This layer changes only the defaults and their test expectations: the HMR client adds its own manifest chunk list, and the SSR next/dynamic entry uses a manifest loader.

Explicit false still disables either flag. Production behavior is unchanged.

Verification

  • The combined implementation previously passed CI in run 43967218926, attempt 2.
  • The combined branch tree is unchanged by this split. No local tests were rerun for the branch-only restructuring.

@vercel

vercel Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Vercel Agent finished reviewing your changes.
2f54d81 • Tue, 17 Nov 2026 18:38:00 GMT

Vercel Agent's latest review does not approve this pull request under the repository's review policy.

Explanation

Please have @mischnic or @sokra review: the merge-base config-and-flags rubric strongly escalates the default changes at packages/next/src/server/config-shared.ts:2570-2571. The updated policy recognizes jimmyhmiller as a team member, but the default-change escalation still requires human review; I checked all four changed files, schema wiring, development guards, explicit-false handling, and activation paths without confirming a blocking defect. Native builds and browser tests remain unverified locally because dependencies and build outputs are unavailable.

Review steps (43.8s)

Preparing the workspace 16.2s
Analyzing the changes 23.7s
Finalizing the review 3.9s

@github-actions github-actions Bot added Turbopack Related to Turbopack with Next.js. type: next labels Nov 16, 2026
@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Tests Passed

Commit: 0e45853
View workflow run

@github-actions github-actions Bot added the tests label Nov 16, 2026
@jimmyhmiller
jimmyhmiller force-pushed the jimmym/lazy-dynamic-imports-defaults branch 3 times, most recently from 9d966af to 40a9d98 Compare November 17, 2026 15:30
@jimmyhmiller
jimmyhmiller marked this pull request as ready for review November 17, 2026 15:40
@mischnic

Copy link
Copy Markdown
Member

can you pull the bugfixes into a separate PR?

@jimmyhmiller
jimmyhmiller changed the base branch from canary to jimmym/lazy-dynamic-imports-fixes November 17, 2026 15:52
@jimmyhmiller
jimmyhmiller force-pushed the jimmym/lazy-dynamic-imports-defaults branch from 40a9d98 to acf4cea Compare November 17, 2026 16:06
@jimmyhmiller
jimmyhmiller force-pushed the jimmym/lazy-dynamic-imports-defaults branch from acf4cea to 2f54d81 Compare November 17, 2026 18:37
Base automatically changed from jimmym/lazy-dynamic-imports-fixes to canary November 24, 2026 16:14
jimmyhmiller added a commit that referenced this pull request Nov 24, 2026
### What?

Extract the bugfixes from #101873 so they can merge before lazy dynamic
imports are enabled by default. Both defaults remain unchanged.

### Why?

Lazy imports can cause false reloads, return the wrong module namespace,
or fail during loader evaluation when explicitly enabled.

### How?

- Avoid false full-page reloads when lazy compilation advances the HMR
version without a file edit. Each connection checks the version only on
its first versioned `turbopack-subscribe`, which still catches updates
missed through stale HTML or a dropped connection (a reconnect is a new
connection and is checked again). Later mismatches on the same
connection are ignored, since compiling a lazy chunk list advances the
version without the client missing an update.
- Return an ESM namespace when a manifest loader target is already
available instead of returning raw CommonJS exports.
- Include lazily compiled import targets in `this.importModule()`
graphs, because loader evaluation runs in a separate process that cannot
activate lazy imports. The graph is built with a new
`ModuleGraphOptions::assume_activated` that follows the targets of
lazy-import proxies, and those proxies generate their loading code in
such graphs. Modules stay shared with the app instead of being processed
a second time in a copy of the asset context. Explicitly enable both
lazy-import flags in the existing loader fixture so its nested
dynamic-import assertions cover this independently of the defaults, and
check that a module imported by both the app and the loader is processed
only as often as one that only the app imports.
- Fix the runtime navigation HMR test to require the timing-dependent
Fast Refresh log only for webpack.

### Verification

- An earlier version of the combined implementation, with the previous
forked-context importModule fix, passed CI in run 43967218926, attempt
2.
- The standalone bugfix branch passes the importModule suite in
Turbopack development, webpack development, and Turbopack production
modes with the explicit flags.
- Mutation checks: with no eager handling, the importModule suite fails
because loader evaluation returns unsupported values. With the earlier
forked asset context, the new processing-count assertion fails (2 runs
against 1 for the control module).
- `pnpm test-dev-turbo` on the importModule suite and the
`lazy-dynamic-imports`, `lazy-dynamic-nested-imports` and
`lazy-dynamic-chunk-list-collision` suites; `pnpm test-start-turbo` and
`pnpm test-dev-webpack` on the importModule suite.
- Manually: activating a lazy import in the browser doesn't re-run the
loader, editing a lazy target re-runs only the importModule loader, and
a restart with the persistent dev cache re-runs no loaders (checked on
an earlier build of this fix, before the final refactors).

<!-- NEXT_JS_LLM -->
Enable lazy compilation for client and SSR dynamic imports by default.
Update chunk-list expectations for the additional HMR client manifest and
the SSR next/dynamic manifest loader.
@jimmyhmiller
jimmyhmiller force-pushed the jimmym/lazy-dynamic-imports-defaults branch from 2f54d81 to 0ba0aab Compare November 24, 2026 16:15
@vercel

vercel Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Vercel Agent Verified Review finished reviewing your changes and found no issues.
0e45853 • Tue, 24 Nov 2026 16:27:06 GMT • 1m 7s • Report • Details

Important

Needs a person: this repository’s review rules call for a person’s review.

Why?

The author is a team member, the change has 10 implementation lines, and I found no correctness findings. The f1 stale-docs finding from the earlier review is fixed. But the change flips the defaults of two config options in `config-shared.ts` and in the Rust config. REVIEW.md lists config defaults as a public behavior surface, and the config-and-flags rubric calls any default change in `config-shared.ts` a strong escalation. The author doesn't own config-and-flags, so that signal isn't downgraded to a comment. The `next_config.rs` change also hits turbopack-next-integration's medium signal.

  • Trigger: Changes or additions to public APIs or behaviors (config-shared.ts:2653). Two config options flip from off to on by default in `next dev`, and REVIEW.md's definitions list 'config options and their defaults' as a public surface. The change only flips defaults of existing options; it isn't new feature surface behind a new flag.
  • Rule: Config and Flags: Escalate when (config-shared.ts:2653). The rubric says 'Any default change in `config-shared.ts` (strong, public)', and `authorIsOwner` is false for this area.
  • Rule: Turbopack Next Integration: Escalate when (next_config.rs:2807). Changes to the `next_config.rs` translation are a medium signal there, and the author doesn't own this area.

Owners to review: mischnic, sokra.

Facts from the base’s scripts/review-context.mjs: areas: config-and-flags, turbopack-next-integration, docs, dev-server-and-hmr · authorIsTeamMember: true · implementationLines: 10 · totalLines: 43

Summary

Flips the defaults of experimental.turbopackLazyDynamicImports and experimental.turbopackLazyDynamicImportsSSR to on for Turbopack next dev, in both the Rust config (unwrap_or(true)) and the JS defaultConfig. It also updates the API docs and two test expectations: the HMR client's own manifest chunk list is excluded, and the SSR next/dynamic entry is now a loader instead of an async loader. Production builds are still gated off by next_mode.is_development().

Questions for the author (2)

  • The chunk-list test now drops any subscription whose path contains hmr-client. Is that substring stable across Turbopack's dev chunk naming, so the filter can't also hide a real extra page chunk list in the future?
  • Has the eager→lazy switch been checked against apps that depend on dev compile errors in dynamic-import targets showing up at page load? With this change those errors appear only when the target is first requested.
Checked (4)

     Check Location
Holds Lazy dynamic imports stay off in production builds whatever the new default is.
Both getters AND next_mode.is_development() with unwrap_or(true), so NextMode::Build always yields false.
next_config.rs:2800
Holds An explicit false still disables either option.
The schema still accepts z.boolean().optional(). The Rust Option<bool> uses a user's Some(false) before falling back to unwrap_or(true), and JS reads the merged config value.
config-schema.ts:471
Holds The JS-side default matches the Rust-side default, so the hot reloader and Turbopack agree.
hot-reloader-turbopack.ts:400-402 reads nextConfig.experimental.turbopackLazyDynamicImports(SSR), which defaultConfig now sets to true, matching unwrap_or(true) in Rust.
config-shared.ts:2653
Holds The earlier stale-docs finding (f1) is fixed: the docs now say both options default to true and show false as the way to opt out.
Read both API pages, the turbopack.mdx table and the config-shared.ts JSDoc in the head. All now say true, and grep finds no other docs mentioning these options.
turbopackLazyDynamicImports.mdx:87
Review steps

     Step Time
Sandbox set up 15.7s
Repository rules read: REVIEW.md and AGENTS.md files (5)
Agent review: 3 of 7 changed files opened, 1 command run 49.4s
Repros run on the head and the base (0), findings graded (0) 1.8s

Read the full report ↗ (Download report.json • report.md)

@vercel vercel Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vercel Agent Verified Review reviewed 0ba0aab 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

@vercel
vercel Bot requested review from mischnic and sokra November 24, 2026 16:20
Both options now default to true in development, so the API reference
shows how to disable them and records the default change in the
version history.
@github-actions github-actions Bot added the Documentation Related to Next.js' official documentation. label Nov 24, 2026
@sokra
sokra enabled stacked auto-merge (squash) November 24, 2026 23:57
@sokra
sokra merged commit 59490df into canary Nov 24, 2026
234 of 236 checks passed
@sokra
sokra deleted the jimmym/lazy-dynamic-imports-defaults branch November 24, 2026 23:59
jimmyhmiller added a commit that referenced this pull request Nov 25, 2026
…" (#102379)

## Summary

Reverts #101873, which was merged before it was ready.
`experimental.turbopackLazyDynamicImports` and
`experimental.turbopackLazyDynamicImportsSSR` go back to being off by
default in development, and the related docs and test expectations are
restored.

The bugfixes from #101937 are kept. This only reverts the default
change.

## Verification

- Clean `git revert` of 59490df
- Not run: local tests (relying on CI)

<!-- NEXT_JS_LLM -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Documentation Related to Next.js' official documentation. tests Turbopack Related to Turbopack with Next.js. type: next

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants