Skip to content

hmr might fail if there are new initial chunks #18548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
JSerFeng opened this issue Jul 1, 2024 · 6 comments
Open

hmr might fail if there are new initial chunks #18548

JSerFeng opened this issue Jul 1, 2024 · 6 comments
Assignees

Comments

@JSerFeng
Copy link
Contributor

JSerFeng commented Jul 1, 2024

Bug report

What is the current behavior?

HMR might fail if one initial chunk occur at second compilation.

The reason:

If one chunk is not initial chunk in the first compilation, but is initial chunk in the second compilation, when apply hot updates, there will be some imports to the initial chunk, but at this moment, the initial chunks are not loaded, so webpack will go fetching that chunk, and treats it as async chunk, so if async chunk path is different from initial chunk path, it cannot find that chunk

If the current behavior is a bug, please provide the steps to reproduce.

repro repo:

https://github.com/JSerFeng/webpack-lazy-compilation-issue/tree/hmr

run

pnpm i
pnpm run dev

open the browser

modify src/initial.js

try {
-  require("core-js/package.json");
+  require("core-js");
} catch (_err) {}

module.hot.accept();

module.exports = 1;

What is the expected behavior?

Other relevant information:
webpack version: 5.92.1

@JSerFeng JSerFeng changed the title when enable entry's lazy compilation, hmr might fail at first load hmr might fail if there are new initial chunks Jul 1, 2024
@alexander-akait alexander-akait moved this to Priority - High in webpack 5/6 Jul 10, 2024
@alexander-akait
Copy link
Member

Feel free to send a PR

@webpack-bot
Copy link
Contributor

This issue had no activity for at least three months.

It's subject to automatic issue closing if there is no activity in the next 15 days.

@JSerFeng
Copy link
Contributor Author

bump

@webpack-bot
Copy link
Contributor

Issue was closed because of inactivity.

If you think this is still a valid issue, please file a new issue with additional information.

@JSerFeng
Copy link
Contributor Author

Could we reopen this ? I forgot to commit a bump comment

@pranjalisr
Copy link

pranjalisr commented Mar 21, 2025

You can add a patch to handle this specific case:

Inside hmrpack.js
if (module.hot) {
const originalHotCheck = module.hot.check

module.hot.check = function patchedHotCheck(applyOnUpdate) {
return originalHotCheck.call(this, applyOnUpdate).catch((error) => {
// If error is related to missing chunks, force a full reload
if (error && (error.message.includes("Cannot find module") || error.message.includes("chunk not found"))) {
console.warn("[HMR] Chunk loading failed, performing full reload")
window.location.reload()
return null
}
throw error
})
}
}

The issue occurs because webpack's HMR system doesn't properly handle the transition of chunks from non-initial to initial during hot updates.

You can Use webpack configuration or Use the runtime patch to address this issue.

@snitin315 snitin315 self-assigned this Apr 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Priority - High
Development

Successfully merging a pull request may close this issue.

5 participants