Skip to content

Fix lazy dynamic import reloads and module evaluation - #101937

Merged
jimmyhmiller merged 7 commits into
canaryfrom
jimmym/lazy-dynamic-imports-fixes
Nov 24, 2026
Merged

jimmyhmiller merged 7 commits into
canaryfrom
jimmym/lazy-dynamic-imports-fixes

Conversation

@jimmyhmiller

@jimmyhmiller jimmyhmiller commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

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).

@vercel

vercel Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Vercel Agent finished reviewing your changes.
db3e169 • Thu, 19 Nov 2026 21:52:24 GMT • 50.3s • Details

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

Explanation

@mischnic and @wbinnssmith: the merge-base policy still calls for owner review because this PR combines the medium core-algorithm trigger (turbopack/crates/turbopack-core/src/module_graph/mod.rs:311-324,2453-2468) with the medium HMR-message-handling signal (packages/next/src/server/dev/hot-reloader-turbopack.ts:1846-1868). No new concrete defects found: the latest traversal change retains the activatable-module check inside primary_chunkable_referenced_modules, and the settled review threads remain settled. I reviewed the full diff and the updated traversal path; full Rust/e2e execution remains unverified because dependencies are absent.

Resolved issues (1)

Severity Issue Location
Medium The importModule fix has no regression coverage with lazy compilation enabled, so restoring the broken loader-evaluation behavior would still pass the existing tests. webpack.rs:986
Review steps

     Step Time
Preparing the workspace 14.6s
Analyzing the changes 26.1s
Finalizing the review 9.6s

@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Tests Passed

Commit: 887059b
View workflow run

@github-actions github-actions Bot added tests Turbopack Related to Turbopack with Next.js. type: next labels Nov 17, 2026
@jimmyhmiller
jimmyhmiller force-pushed the jimmym/lazy-dynamic-imports-fixes branch from 24e03cc to 38662a8 Compare November 17, 2026 16:06
@jimmyhmiller
jimmyhmiller marked this pull request as ready for review November 17, 2026 16:31
@jimmyhmiller
jimmyhmiller requested a review from a team November 17, 2026 16:32
@jimmyhmiller
jimmyhmiller force-pushed the jimmym/lazy-dynamic-imports-fixes branch from 38662a8 to 14db82c Compare November 17, 2026 18:37
vercel[bot]
vercel Bot previously approved these changes Nov 17, 2026
@vercel
vercel Bot dismissed their stale review November 18, 2026 21:15

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

@jimmyhmiller
jimmyhmiller force-pushed the jimmym/lazy-dynamic-imports-fixes branch 2 times, most recently from 5ba70f6 to aedbb63 Compare November 19, 2026 20:34
Activating a lazy dynamic import changes the client output, which
advances the HMR version without the client missing an update. The
chunk list it then loads subscribes with the client's older version,
and the version check reloaded the page.

Check the version once per connection, on its first versioned
subscription. That catches updates missed through stale HTML or a
dropped connection. Later mismatches are skipped, since lazy
compilation can advance the version without the client missing an
update.

<!-- NEXT_JS_LLM -->
When a dynamic import target is already available, use the parent import
to return an ESM namespace instead of raw CommonJS exports.
Webpack loaders evaluate importModule graphs in a separate process that
cannot activate lazily compiled dynamic imports. Resolve those graphs in
an asset context without lazy compilation.
Turbopack sends no client update for this navigation and only logs a no-op
rebuild when compiling the page exceeds its suppression delay. Assert the
Fast Refresh log only for webpack.
Forking the asset context without lazy compilation processed every
module reachable from an importModule target a second time, and the
copies had the same idents (and so module ids) as the originals.

Keep the shared asset context and build the importModule graph with
the new `ModuleGraphOptions::force_activated`, which treats activatable
modules as activated in that graph only: it walks the
`ActivatableModule::activated_references` of lazy compilation proxies,
and the proxies generate their active code without depending on the
activation state.

The importModule fixture now checks that a module imported by both the
app and the loader is processed as often as one only the app imports.
primary_chunkable_referenced_modules already falls back to
Module::references for modules that aren't activatable, so pass
force_activated through unchanged.
@jimmyhmiller
jimmyhmiller force-pushed the jimmym/lazy-dynamic-imports-fixes branch from 98cdc92 to 887059b Compare November 24, 2026 14:46
@vercel

vercel Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Vercel Agent Verified Review finished reviewing your changes and confirmed no issues. 2 findings need your judgment.
887059b • Tue, 24 Nov 2026 14:51:09 GMT • 2m 52s • Report • Details

Important

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

Why?

The author is a team member but owns none of the touched areas. Two medium triggers apply: a change to the module graph traversal and the ActivatableModule trait (a core data structure and algorithm), and a change to HMR message handling in the Turbopack hot reloader (the dev-server-and-hmr rubric's medium signal). Two medium triggers escalate on their own. On top of that, the HMR fix in runtime code has no regression test that fails without it, and its described mechanism doesn't match the diff. The repository context script ran without errors (162 implementation lines, under the 600-line threshold).

  • Trigger: Changes to core data structures, algorithms, or architectures (mod.rs:325). Adds a ModuleGraphOptions field that changes which references graph construction follows, plus a new required method on the ActivatableModule trait.
  • Trigger: Changes to HMR message handling or invalidation ordering in either hot reloader (hot-reloader-turbopack.ts:1815). Changes when turbopack-subscribe triggers RELOAD_PAGE.
  • Rule: bugfix-regression-test (hot-reloader-turbopack.ts:1818). No new or changed test reaches the hmrVersionChecked logic and fails without it.
  • Rule: bugfix-root-cause (hot-reloader-turbopack.ts:1810). The description's 'track the version clients must have separately' mechanism and its server-component-refresh fix aren't in the diff.

Review requested from: wbinnssmith, devjiwonchoi, sokra.

Facts from the base’s scripts/review-context.mjs: areas: turbopack-next-integration, dev-server-and-hmr, webpack-config-and-loaders, turbopack-core-bundler · authorIsTeamMember: true · implementationLines: 162 · totalLines: 231

Summary

Fixes for lazy dynamic imports in Turbopack dev. The hot reloader now checks the client's HMR version only on the first versioned turbopack-subscribe of each connection. Manifest loaders for targets that are already available now return the module through parentImport instead of __turbopack_require__. this.importModule() graphs are built with a new ModuleGraphOptions::force_activated, so lazy-import proxies follow their activated_references and emit active loading code. The PR also loosens a Turbopack log assertion in app-hmr and adds a test that counts how often each module is processed in the importModule fixture.

Questions for the author (2)

  • The description refers to ModuleGraphOptions::assume_activated, but the code names it force_activated. Which description is current, and was the 'track required version separately' HMR approach dropped in favor of the once-per-connection check?
  • With SSR lazy imports, can an activation during the page's own render bump clientHmrVersion after the HTML's initialHmrVersion was read? The first subscription would then still trigger a false reload.

Issues (2)

Severity Issue Location
High Unverified: HMR fix doesn't match its description and has no regression test hot-reloader-turbopack.ts:1815
Low Unverified: Turbopack Fast Refresh log assertion dropped instead of made deterministic hmr.test.ts:260
Checked (4)

     Check Location
Holds Only the loader's importModule graph forces activation; every app, NFT, service-worker and client-reference graph passes false.
Grepped every force_activated and primary_chunkable_referenced_modules call site: project.rs:1297, next_server_nft.rs:202, service_worker.rs:128 and visit_client_reference.rs pass false.
webpack.rs:1015
Holds Mixing forced and unforced graphs errors instead of silently generating inconsistent proxy code.
Read ModuleGraph::force_activated: it bails when only some graphs in the snapshot are forced.
mod.rs:1014
Holds Proxy chunk content, content ident and manifest output assets all use the same graph-aware activeness check.
Traced is_active_in to chunk_item_content, chunk_item_content_ident (proxy.rs) and ManifestAsyncModule::chunk_item_output_assets (chunk_asset.rs:277).
proxy.rs:251
Holds The empty-chunks manifest loader resolves through parentImport, the same way the chunk-loading path does, so it yields an ESM namespace.
Compared it with the non-empty branch, which already calls parentImport({dynamic_id}).
loader_module.rs:159
Review steps

     Step Time
Sandbox set up 18.5s
Repository rules read: REVIEW.md and AGENTS.md files (6)
Agent review: 5 of 21 changed files opened, 1 command run 2m 31s
Repros run on the head and the base (0), findings graded (2) 2.6s

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

Confirmed: its repro failed on this commit and not the same way on the base. Unverified: a person should judge it.

@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 887059b and isn’t approving it: this repository’s review rules call for a person’s review. 2 open findings are listed in its review summary and inline. View the full report

// are skipped, since lazily compiling a chunk list advances the version without the
// client missing an update.
const state = clientStates.get(client)
if (

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.

Note

Unverified · Major — HMR fix doesn't match its description and has no regression test

The description says the false-reload fix "advance[s] the output version immediately while tracking the version clients must have separately" and also avoids server component refreshes. The diff does neither: it adds a per-connection hmrVersionChecked flag and skips the version check on every later subscription. No added or changed test reaches this path: the only HMR test change (test/development/app-hmr/hmr.test.ts) removes an assertion for Turbopack. REVIEW.md's bugfix-regression-test rule (strong for packages/next/src/server/**) asks for a test that fails without the fix, and bugfix-root-cause asks that the described cause match the diff.

Needs your judgment: the review traced this through the code but couldn’t run anything to prove it.

)
// Turbopack sends no client update for this navigation, and only logs a no-op rebuild when
// compiling the page takes longer than its suppression delay.
if (!isTurbopack) {

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.

Note

Unverified · Minor — Turbopack Fast Refresh log assertion dropped instead of made deterministic

Under Turbopack the test no longer requires the '[Fast Refresh] done in' log, so for Turbopack it now checks only the negative assertion. This is the bundler-keyed assertion skip that REVIEW.md's no-fake-greens rule names. It is justified by a comment, so it counts as a justified medium, but the navigation's Turbopack behavior is no longer asserted at all.

Needs your judgment: the review traced this through the code but couldn’t run anything to prove it.

@jimmyhmiller
jimmyhmiller merged commit b5903f2 into canary Nov 24, 2026
127 checks passed
@jimmyhmiller
jimmyhmiller deleted the jimmym/lazy-dynamic-imports-fixes branch November 24, 2026 16:14
sokra pushed a commit that referenced this pull request Nov 24, 2026
## 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.

<!-- NEXT_JS_LLM -->
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

tests Turbopack Related to Turbopack with Next.js. type: next

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants