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
Preloading modules with magic comments does not result in modulepreload injection #16838
Comments
What plugin do you use for HTML generation, I don't think webpack can solve it here |
Will the description of Preload in the document cause misunderstandings, making readers think that Webpack will do the injection job? |
Hi @alexander-akait - I am using the HTML Webpack Plugin, however, I handle the injection manually using an HTML Upon further inspection, I think I have misunderstood the documentation here. From what I now understand, the magic comments will provide Webpack context for how to inject the the So, with that in mind, I think this is more of a feature request than a bug report. This is how I would expect it to work: See the following: When a compilation is set to This:
Will yield and injection of the following into the DOM:
However, this:
Will either be ignored, or will yield the same as the above (I can't decide) - this will make this functionality backward compatible if people upgrade to ES6. |
But we don't generate HTML here, all this needs to be handled and generated in
Ref: https://medium.com/webpack/link-rel-prefetch-preload-in-webpack-51a52358f84c Example: index.js async function test() {
await import(/* webpackPrefetch: true */ './module1.js');
}
setTimeout(() => {
test();
}, 1000) module1.js console.log("1");
async function test() {
await import(/* webpackPreload: true */ './module2.js');
}
setTimeout(() => {
test();
}, 1000) module2.js console.log("2"); But I think what you are trying to preload in initial chunk, so it just doesn't make sense |
Oh, I see a bug, when you have, (my bad, don't fully undestand the issue, sorry):
We generate: <link type="module" charset="utf-8" rel="preload" as="script" href="/dist/src_module2_js.bundle.js"> But we need to generate: <link rel="modulepreload" href="http://example.com/js/123.456789.chunk.js"> |
Do you want to send a fix, it should be easy https://github.com/webpack/webpack/blob/main/lib/web/JsonpChunkLoadingRuntimeModule.js#L253 |
@snitin315 There is a good issue, we just need to fix:
to
logic is here https://github.com/webpack/webpack/blob/main/lib/web/JsonpChunkLoadingRuntimeModule.js#L253 |
@alexander-akait yes, can you assign this issue to me? Otherwise, I'll forget. |
Done 👍 |
@alexander-akait We need this only when |
@snitin315 Yeah, I think we already have test case for this, can you check? |
@alexander-akait We have for CJS webpack/test/configCases/web/prefetch-preload/index.js Lines 30 to 40 in 3c0af27
|
@alexander-akait Yeah, just copy this test and set |
Bug report
Webpack magic comments do not inject the correct HTML syntax for preloading modules.
What is the current behavior?
Preloading a module results in the following HTML injection:
If the current behavior is a bug, please provide the steps to reproduce.
I believe this is a bug, but I am aware that it could be to do with my configuration...
My configuration is large so I will only include the bit that I think is relevant:
I also enabled
experiments.outputModule
and addedmodule: true
to theoutput
, but I got errors with this, so reverted.And in one of my files, I have the following line:
What is the expected behavior?
I would expect the injected HTML to look like this instead:
I may be mistaken here as obviously
/* webpackPrefetch: true */
would not work with a module, so perhaps this is intentional, and module preloading is not meant to be supported. If this is the case, then I apologise - I did search the documentation and Google from top to bottom before posting here...Other relevant information:
webpack version: 5.76.1
Node.js version: 19.7
Operating System: WSL 2 (Linux on Windows)
Additional tools:
The text was updated successfully, but these errors were encountered: