Skip to content

[Bug]: HMR not working in lazy import components #11324

Closed

Description

@mrleidesen

Version

System:
    OS: Windows 11 10.0.22631
    CPU: (20) x64 13th Gen Intel(R) Core(TM) i5-13600KF
    Memory: 19.88 GB / 31.84 GB
  Browsers:
    Edge: Chromium (131.0.2903.112)
    Internet Explorer: 11.0.22621.3527
  npmPackages:
    @rsbuild/core: ^1.4.8 => 1.4.8
    @rsbuild/plugin-react: ^1.3.4 => 1.3.4

Details

HMR throw error and not working in lazy import components, when I import new component from antd.

英文不好,这里中文再复述清楚一些:

  1. 正常 pnpm run dev 之后, 在 lazy import 的组件中引入 antd 的 Button(或者其他的组件都行,只要是新引入的)
  2. HMR 就会失效,控制台会报错 undefined factory ./node_modules/xxxx
  3. 刷新页面后就是正常的

Reproduce link

https://codesandbox.io/p/devbox/7npnc7

Reproduce Steps

Steps:

  1. run dev, you will see 'Hello'
  2. import Button in LazyItem.tsx, HMR not working
  3. see error in console

Activity

  1. added theissue type on Aug 6, 2025
  2. added theissue type on Aug 7, 2025
  3. chenjiahan commented on Aug 7, 2025

    @chenjiahan
    Member

    I suspected this was an issue with the Rspack HMR runtime, so I transferred the issue to the Rspack repository.

  4. litenjacob commented on Aug 8, 2025

    @litenjacob

    For us this case seemed to be working in 1.4.10 but brake in 1.4.11 with an undefined factory error.

  5. patricksevat commented on Aug 11, 2025

    @patricksevat

    Hi, we're facing undefined factory as well, what we are noticing is that with

    "@rspack/cli": "1.4.11",
    "@rspack/core": "1.4.11",
    

    There is a certain dynamic import with this shape:

    const LazyComponent = lazy(() => import(/* webpackChunkName: "explicit-chunk-name" */ '../containers/Container'));

    When booting in devServer mode, in the initial compile the chunk explicit-chunk-name.js contains ../containers/Container, but after an HMR recompile, suddenly explicit-chunk-name.js reduces in size by half and no longer contains ../containers/Container.

    This is persistent after refreshes and requires us to restart the rspack process

  6. stormslowly commented on Aug 12, 2025

    @stormslowly
    Contributor

    @mrleidesen I have tried rspack and webpack, they both failed to do hot replacing with the same error.

    In your case, after adding the Button component, bundler will create a new chunk named vendors-node_modules.xxxx.js. IMO, this chunk should be expected to present in hotupdate main file (xxx.[hash].hotupdate.json); but it doesn't show up, so requiring to modules in the vendor chunk will throw a undefined factory error.

    I will make PRs to webpack and rspack, it takes time.

  7. mrleidesen commented on Aug 14, 2025

    @mrleidesen
    Author

    @mrleidesen I have tried rspack and webpack, they both failed to do hot replacing with the same error.

    In your case, after adding the Button component, bundler will create a new chunk named vendors-node_modules.xxxx.js. IMO, this chunk should be expected to present in hotupdate main file (xxx.[hash].hotupdate.json); but it doesn't show up, so requiring to modules in the vendor chunk will throw a undefined factory error.

    I will make PRs to webpack and rspack, it takes time.

    Thank you!

  8. patricksevat commented on Aug 14, 2025

    @patricksevat

    @stormslowly Thanks for picking this up!

    Since @litenjacob and myself have reported this as working in v1.4.10 and broken since v1.4.11, is there any PR in that version that might have caused it?

    I've also tested it in a few other cases:

    bundler version works
    rspack 1.4.10 yes 👍
    rspack 1.4.11 no :(
    rspack 1.4.11 no :(
    rspack 1.5.0-beta.0 no :(
    webpack 5.97.1 yes 👍

    Because it works in Webpack, I'm wondering if we are facing the same issue, since you mention that what you reproduced also fails in webpack.

    In my initial comment I missed adding some additional clues. There were certain files which caused a full page reload so I only saw the undefined factory error, but on actual hot reloading (without full page refresh), I see some additional logs which might be interesting:

    Image

    Then after that, I get:

    [HMR] Updated modules:
    [HMR]  - ../../container/components/SomeComponent.tsx
    // 25+ more files 
    [HMR] App is up to date.
    Warning: React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
    
    Check your code at SomeComponent.tsx:13.
    

    Some other things to note:

    • manifest.js is our runtime chunk (rspackConfig.optimization.runtimeChunk.name = 'manifest')
    • In Webpack, for [HMR] Updated modules: I only see 1 module vs 25+ in rspack@1.4.11
    • In rspack@1.4.10, for [HMR] Updated modules: I only see 1 module vs 25+ in rspack@1.4.11
    • In rspack@1.4.11, the explicit-chunk-name.js does get an explicit-chunk-name.hot-update.js, but contains AnotherComponent.ts, not SomeComponent.tsx
    • In rspack@1.4.10, I see manifest.hot-update.js, manifest.hot-update.json and another-chunk-name.hot-update.js
    • In rspack@1.4.11, I see manifest.hot-update.js, manifest.hot-update.json, another-chunk-name.hot-update.js, explicit-chunk-name.hot-update.js (this is the bugged one!) AND 3 additional other {chunkName}.hot-update.js,

    In short, when comparing 1.4.11 vs 1.4.10, a single file change seems to affect a lot more HMR modules and HMR chunk.

    I'm unable to share private code here, but if it helps, I'm on the Rspack Discord as well (username PostmanPat), perhaps we can jump on a screensharing call to debug further

  9. mrleidesen commented on Aug 15, 2025

    @mrleidesen
    Author

    This works for me!

    "overrides": {
      "rspack": "1.4.10"
    }

    I mean "@rspack/core": "1.4.10" , not "rspack"

  10. patricksevat commented on Aug 27, 2025

    @patricksevat

    This is still an issue on 1.5.0, unfortunately.

    Is there any information I could provide to help this issue further @stormslowly ?

  11. JMStewart commented on Aug 27, 2025

    @JMStewart

    I'm also seeing this issue on 1.4.11 and 1.5.0, and no issues on 1.4.10. The initial build works great, but any small change to a single file results in a rebuild that seems to be affecting a large number of unrelated modules, and results in very different outputs that will not run.

  12. stormslowly commented on Aug 28, 2025

    @stormslowly
    Contributor

    I'm also seeing this issue on 1.4.11 and 1.5.0, and no issues on 1.4.10. The initial build works great, but any small change to a single file results in a rebuild that seems to be affecting a large number of unrelated modules, and results in very different outputs that will not run.

    could plz share reproduce repo?

  13. stormslowly commented on Aug 28, 2025

    @stormslowly
    Contributor

    This is still an issue on 1.5.0, unfortunately.

    Is there any information I could provide to help this issue further @stormslowly ?

    Currently, I can't make a conclusion yet. According the orignal reproduce codesandbox.io/p/devbox/7npnc7, a workaround is to disable splitChunks in dev mode.

    optimization: {
      splitChunks: false
    }

    there is a example

    @patricksevat could plz help to verify this workaround is working for you?

  14. stormslowly commented on Aug 28, 2025

    @stormslowly
    Contributor

    This works for me!

    "overrides": {
    "rspack": "1.4.10"
    }

    @JMStewart i don't think it's the right way to overrides. the rspack is deprecated for years.

    I have noticed that you're chinese too. Could plz join our Feishu Group for easy discuss this problem?

  15. patricksevat commented on Aug 28, 2025

    @patricksevat

    I tried this on rspack@1.5.0:

    optimization: {
      splitChunks: false
    }
    

    I'm still seeing undefined factory errors, although I'm seeing a different one than I saw before.

  16. patricksevat commented on Aug 29, 2025

    @patricksevat

    The workaround mentioned by @JSerFeng in #11530 (comment) seems to work for me in rspack@1.5.1 🥳 :

    module.exports = {
      experiments: { incremental: { buildChunkGraph: false } }
    }
    
  17. stormslowly commented on Sep 1, 2025

    @stormslowly
    Contributor

    The workaround mentioned by @JSerFeng in #11530 (comment) seems to work for me in rspack@1.5.1 🥳 :

    module.exports = {
      experiments: { incremental: { buildChunkGraph: false } }
    }
    

    So it's not the same case with @mrleidesen , I will focus on the original case.

  18. stormslowly commented on Sep 5, 2025

    @stormslowly
    Contributor

    Root Cause Analysis

    Simple Description

    During development, splitChunks may move some modules between chunks or create a new chunk for newly introduced modules.
    If a new chunk is generated, its modules may fail to load correctly during HMR. When those modules are required, it triggers an undefined factory error.

    Detailed Explanation

    Initially (before importing Antd Button):

    • Only two chunks exist: main.js and src_lazyItem_jsx.js.

    After importing Antd Button in LazyItem.js:

    • src_lazyItem_jsx.js grows in size.
    • It gets split into:
      • src_lazyItem_jsx.js
      • A new vendors_xxxx.js chunk containing required node_modules, including Antd’s Button code.

    Since CodeSplitting is involved:

    • App.js (which dynamically loads LazyItem.jsx) must now ensure both src_lazyItem_jsx.js and vendors_xxxx.js are loaded, instead of just src_lazyItem_jsx.js.
    • Therefore, in the next HMR cycle, both App.jsx and LazyItem.jsx are re-executed via *.hotupdate.js.

    Problem:

    • App.jsx loads chunks asynchronously, but execution of new code happens synchronously.
    • This means when LazyItem.jsx executes, vendors_xxxx.js may not yet be ready.
    • As a result, we hit the error: undefined factory ....node_modules/antd/es/button/index.js

    This undefined factory is expected under such circumstances, and the only safe recovery is a full reload.
    The unexpected part is that the error fails silently.


    Why It Fails Silently

    When React Refresh is enabled, all HMR errors are routed to webpackHot.accept.

    • If refreshOverlay is disabled (default in @rspack/plugin-react-refresh), runtime errors during HMR are suppressed.
    • If refreshOverlay is enabled, things still don’t work as expected:
      • Two hot update modules (LazyItem.jsx and App.jsx) need to be applied.
      • When LazyItem.jsx executes first, its error is passed to refreshOverlay.handleRuntimeError(error).
      • But since overlay rendering is debounced, the error display is delayed.
      • Then, when App.jsx hot-update runs, it clears runtime errors, dismissing the overlay before it even shows.

    Result: No error is visible to the developer.


    Solutions

    • When an undefined factory error occurs, the only reliable solution is to reload.

    • To support this, we need to let @rspack/plugin-react-refresh recognize such errors.

    • With reloadOnRuntimeError enabled, @rspack/plugin-react-refresh will trigger a full reload on undefined factory errors.

  19. stormslowly commented on Sep 11, 2025

    @stormslowly
    Contributor
  20. zhangchao828 commented on Oct 17, 2025

    @zhangchao828

    @stormslowly 最新版本1.5.8还是会报这个错,是和模块联邦一起使用时

  21. stormslowly commented on Oct 28, 2025

    @stormslowly
    Contributor

    @zhangchao828
    可以给我一个复现的 repo 吗?

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions