If you would like to achieve cache busting in "webpack
way":
1. Hash name of output files
Change output filenames to hash generated names (on build phase)
output: {
path: '/',
filename: '[hash].js',
chunkFilename: '[chunkhash].js',
},
From that point your foo.js
and chunk.1.js
will be called like e883ce503b831d4dde09.js
and f900ab84da3ad9bd39cc.js
. Worth mention that generation of this files are often related to making production and time too update cacheBust
value.
2. How to include not known names of files?
Since now your foo.js
- main file is named in not known way. To extract this name of file you can use AssetsPlugin
const AssetsPlugin = require('assets-webpack-plugin');
const assetsPluginInstance = new AssetsPlugin();
and add this plugin to webpack.config.js
plugins: [
assetsPluginInstance
]
In webpack-assets.json
file you should see something like
{
"main": {
"js": "/e883ce503b831d4dde09.js"
}
}
You can use this file to point to main .js
file. For more details read this answer
3. Benefit time
I guess that if you make app production because of modification of chunk.2.js
file, you change files paths from
- build.js?cacheBust=12345
- chunk.1.js?cacheBust=12345
- chunk.2.js?cacheBust=12345
- chunk.2.js?cacheBust=12345
to new ones
- build.js?cacheBust=12346 // modified referation to chunk.2.js file
- chunk.1.js?cacheBust=12346
- chunk.2.js?cacheBust=12346 // modified
- chunk.2.js?cacheBust=12346
If you would use above solution you will get free cache determination. Now filles will be called like
(previous production)
- e883ce503b831d4dde09.js
- f900ab84da3ad9bd39cc.js
- 5015cc82c7831915903f.js
- 8b6de52a46dd942a63a7.js
(new production)
- c56322911935a8c9af13.js // modified referation to chunk.2.js file
- f900ab84da3ad9bd39cc.js
- cd2229826373edd7f3bc.js // modified
- 8b6de52a46dd942a63a7.js
Now only main file
and chunk.2.js
names are changed and you will get this for free by using webpack way.
You can read more about long term caching here https://medium.com/webpack/predictable-long-term-caching-with-webpack-d3eee1d3fa31