-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[contenthash] doesn't match between the assets in hooks and emitted output #1638
Comments
the same error
|
[Forget my poor English!] Hi,I have tried to find out where is the problem. Then I find that the orders between html-webpack-plugin and webpack/optimize/RealContentHashPlugin may be the key. html-webpack-plugin gets contenthash-A. Then html-webpack-plugin uses it to generate html, passes it by hooks (beforeAssetTagGeneration ... beforeEmit) to us. Finnaly html-webpack-plugin sends html to webpack. After html is sent, webpack uses RealContentHashPlugin to update contenthash and gets contenthash-B. webpack updates html with new contenthash-B. So we get contenthash-A. But html uses correct contenthash-B for assets. |
it seems you know the problem, what is the solution? |
In my case, i need contenthash to generate new file for some reason, so i send my content to webpack too, like: compilation.emitAsset(`${outputFilename}`, new webpack.sources.RawSource(`${content}`, false), {}); webpack updates correct contenthash automatically. if i just want to know the correct contenthash, i mabye use This is my solution for now. I hope it can help you. |
I also have this kind of problem with just html-webpack-plugin and html-webpack-harddisk-plugin and with webpack config Temporary solution for me is to disable
See https://webpack.js.org/configuration/optimization/#optimizationrealcontenthash and https://webpack.js.org/plugins/internal-plugins/#realcontenthashplugin for more info:
@jantimon is there any chance for a fix of this behaviour please? |
Hi am using:
html file: <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>React Min</title>
</head>
<body>
<div id="root"></div>
<script src="script.file.js"></script>
</body>
</html> As one can see there is a custom script added. So it requires html-loader for parsing right! I was running in same issue : "The hash in index.html was different then that of generated file." webpack config: mode: 'production',
output: {
filename: '[name].[contenthash].js',
chunkFilename: '[id].[contenthash].js',
assetModuleFilename: `assets/[name].[contenthash].[ext]`,
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
include: path.resolve(__dirname, "src"),
use: [{
loader: 'babel-loader',
}]
},
{
test: /\.html$/i,
use: [{ loader: 'html-loader' }]
} as mentioned by @rodlukas, setting
generated index.html file <!doctype html>
<html lang="en">
<head>
...
</head>
<body>
<div id="root"></div>
<script src="/assets/script.file.a4b0e773e61e7afa29f5..js"></script>
... /* bundles */
</body>
</html> |
Previously css changes where only visible after a manual refresh of the page - quite annoying. Now css is automatically refreshed on save - similar to js. It looks like, that this is actually an issue of "html-webpack-plugin" with "contenthash". See jantimon/html-webpack-plugin#1638 . This is more a workaround than an actual fix.
Previously css changes where only visible after a manual refresh of the page - quite annoying. Now css is automatically refreshed on save - similar to js. It looks like, that this is actually an issue of "html-webpack-plugin" with "contenthash". See jantimon/html-webpack-plugin#1638 . This is more a workaround than an actual fix.
This issue had no activity for at least half a year. It's subject to automatic issue closing if there is no activity in the next 15 days. |
If I'm not mistaken, this issue is still relevant. |
yes! the issue is still relevant since there was no commit that would fix the behaviour |
I had to wrestle with the same as we needed to create double-bundle (one for ES6-and-up browsers and one for ES5-and-down browsers) and inject in a single html file, so ended up creating my own plugin on top of html-webpack-plugin. Our use-case wasn't trivial so it won't fit your use-cases most likely. TLDR; To improve things, webpack maintainers (@alexander-akait) suggest to move html-webpack-plugin over to webpack-contrib, which (from what I can see) @jantimon doesn't want to. Issues backlog in this repo hints me that html-webpack-plugin is not being actively maintained so I'm not sure about its future and better alternatives. We're using NX on some of our projects and next.js on others, they provide their own custom way of injecting assets into html; there's also other (than webpack) "bleeding edge (as always)" bundlers, nothing is more stable in JS world than community shift towards yet another hyped library, so it's hard to recommend anything. My personal take - I'd rather see webpack and html-webpack-plugin evolving and community focusing on them, rather than jumping on another hype train, as they are fundamental web tools for me. |
Is there any progress on this issue? |
Current behaviour 💣
When using the
assetTags
from the callback ofalterAssetTags
oralterAssetTagGroups
hooks (didn't test other hooks), thecontenthash
portion of the asset filename differs from thecontenthash
of the same asset in the emitted output.For example:
given
filename: "bundle.[contenthash].js"
In
alterAssetTags
hook,assetTags.scripts[0].attributes.src
evaluates to:bundle.1817f49c37ed2ca083b2.js
whereas in the final output and also the emitted
index.html
it is:bundle.2e55dea7e3e197f9902d.js
Expected behaviour ☀️
The content hashes in the hooks filename and in the emitted output should be the same.
Reproduction Example 👾
https://github.com/psimk/html-plugin-contenthash-reproducible
Environment 🖥
The text was updated successfully, but these errors were encountered: