html-webpack-plugin icon indicating copy to clipboard operation
html-webpack-plugin copied to clipboard

`[hash]` in filename uses template file hash, not hash of built html

Open jenan-stripe opened this issue 9 years ago • 14 comments

When using new HtmlWebpackPlugin({ filename: 'index-[hash].html', ...}), the hash value seems to be based on the text content of the template file, not on what it produces. This means that changing dependent chunks will not update the output filename, even though the contents have changed. This appears to be the case with both inject: true and explicitly using ejs templating to include <script src="..."></script> tags.

E.g., if I have a template index.html:

<html>
<body>
</body>
</html>

and use a webpack config with:

entry: {index: 'index.js'},
output: {path: 'build/', filename: '[name]-[hash].js'},
plugins: [
  new HtmlWebpackPlugin({
    filename: 'index-[hash].html',
    template: 'index.html',
    inject: true,
    chunks: ['index'],
  }),
]

and then get a build/index-aaaaaaaa.html of:

<html>
<body>
<script type="text/javascript" src="index-bbbbbbb.js"></script>
</body>
</html>

and then change the javascript for index.js, I'll get a build/index-aaaaaaaa.html (same file name as before) of:

<html>
<body>
<script type="text/javascript" src="index-cccccccc.js"></script>
</body>
</html>

jenan-stripe avatar Jan 22 '17 23:01 jenan-stripe

Here is a minimal reproduction.

jenan-stripe avatar Jan 23 '17 00:01 jenan-stripe

+1

JuoCode avatar Feb 11 '17 09:02 JuoCode

@jenan-stripe the reason is that it uses the hashing algorithm by webpack - otherwise we would have issues with caching while using the webpack middleware or the webpack dev server

jantimon avatar Feb 13 '17 08:02 jantimon

@jantimon Hm does anything else in webpack exhibit this behavior, where the contents may change but the fingerprint does not?

jenan-stripe avatar Feb 13 '17 17:02 jenan-stripe

@jenan-stripe I'm running into the same problem with a similar configuration. were you able to get it to work?

lbalceda avatar May 23 '17 20:05 lbalceda

@lbalceda Nope. I'm manually fingerprinting in a post-build step.

jenan-stripe avatar May 23 '17 20:05 jenan-stripe

It makes it very hard to cache the html in a service worker for offline use. This is definitely something that should be fixed.

moimael avatar Jun 13 '17 15:06 moimael

@jenan-stripe Did u still manually fingerprinting in a post-build step or find a new way to get it work?

Measy avatar Oct 31 '17 07:10 Measy

@Measy Still manually fingerprinting.

jenan-stripe avatar Oct 31 '17 16:10 jenan-stripe

I guess we could add [contenthash] like extract-text-webpack-plugin is doing: https://github.com/webpack-contrib/extract-text-webpack-plugin/blob/8de6558e33487e7606e7cd7cb2adc2cccafef272/src/index.js#L212-L214

mastilver avatar Oct 31 '17 16:10 mastilver

Any progress on this ?

moimael avatar Feb 08 '18 08:02 moimael

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.

stale[bot] avatar Aug 07 '18 09:08 stale[bot]

We should add the [contenthash] solution proposed by matsilver

jantimon avatar Aug 07 '18 11:08 jantimon

Fixed with #1021

jantimon avatar Jan 08 '19 08:01 jantimon