I have a React application that every time I redeploy the users tell me they cannot see the changes. I ask them to do a hard reset and clear cache. I want to bust browser cache when I push a new version, so the user sees the changes.
I used react-create-app to originally create the app.
I read here that you should use hash: true in your webpack plugin. I did this and now I am seeing that the bundled react app now has a query string, but now I am getting the error:
Refused to execute script from 'https://example.com/static/js/main.9b80cc8a.js?76de7bb1d01e56c5fcb0' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled
That error is covered here with Node. I am using express.static
I changed the web pack from this:
new HtmlWebpackPlugin({
inject: true,
template: paths.appHtml,
minify: {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true,
},
}),
to this:
new HtmlWebpackPlugin({
hash: true,
inject: true,
template: paths.appHtml,
minify: {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true,
},
}),
My node code looks like this, which I think is right:
app.use(express.static(path.join(__dirname, 'public')));
The public directory contains the production built app.
How can I prevent this error and clear browser cache when the app is updated?