JavaScript Webpack cache busting with hashes not working webpack cache busting index.html,webpack cache busting css,webpack

I am trying do cache busting using webpack by adding a hash to the end of each javascript file. My webpack configuration file is below:

const AssetsPlugin = require('assets-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
//const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    entry: "./js/main.js",
    output: {
        path: __dirname + '/static/',
        publicPath: '',
        filename: "bundle-[hash].js",
    },
    resolveLoader: {
    moduleExtensions: ['-loader']
    },
    module: {
        loaders: [
            {
                test: /\.jsx?$/,
                exclude: /(node_modules|bower_components)/,
                loader: 'babel',
                query: {
                    presets: ['react', 'es2015', 'stage-0']
                }
            },
            {
                test: /\.css$/,
                loader: 'style-loader',
            },
            {
                test: /\.css$/,
                loader: 'css-loader',
                query: {
                    modules: true,
                    localIdentName: '[name]__[local]___[hash:base64:5]'
                }
            }
        ]
    },
    plugins: [
        new CleanWebpackPlugin(['static/bundle*.js'], {watch: true}),
        new AssetsPlugin({
                filename: 'static/webpack.assets.json',
                prettyPrint: true
        }),
    ]
};

The index.html file which serves the javascript file created by webpack is below:

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript" >
            $.getJSON('webpack.assets.json', function(data){
                <!--
                var bundleScript = "<script src=" + data['main']['js'] + "></script>";
                var div = document.getElementById('app');
                div.innerHTML = bundleScript;
                $(bundleScript).appendTo('#app');
                //!-->
            });
        </script>
    </head>

    <body>
        <div id="app"></div>
    </body>
</html>

When I make changes to the code, I have to hard refresh the browser in order to see the changes, instead of just plain refreshing, which I would expect if the cache busting were to work. Any help is appreciated; thanks!

Answer:1

Webpack cache busting still working in here. If you change the code, webpack will recreate the file with the different hash (https://webpack.js.org/guides/caching)

What you want is called hot reload. You can read more about it in https://webpack.js.org/concepts/hot-module-replacement/

To use hot reload, you should create new config:

const AssetsPlugin = require('assets-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');

module.exports = {
entry: "./js/main.js",
output: {
    path: __dirname + '/static/',
    publicPath: '',
    filename: "bundle.js", // remove hash
},
resolveLoader: {
moduleExtensions: ['-loader']
},
module: {
    loaders: [
        {
            test: /\.jsx?$/,
            exclude: /(node_modules|bower_components)/,
            loader: 'babel',
            query: {
                presets: ['react', 'es2015', 'stage-0']
            }
        },
        {
            test: /\.css$/,
            loader: 'style-loader',
        },
        {
            test: /\.css$/,
            loader: 'css-loader',
            query: {
                modules: true,
                localIdentName: '[name]__[local]___[hash:base64:5]'
            }
        }
    ]
},
plugins: [
    // new CleanWebpackPlugin(['static/bundle*.js'], {watch: true}), comment it
    // new AssetsPlugin({
    //        filename: 'static/webpack.assets.json',
    //        prettyPrint: true
    // }), and this 
],
devServer: {
  contentBase: path.join(__dirname, "static"),
  compress: true,
  port: 9000
}
};

And run with: webpack-dev-server -c 'your new config' --hot

Answer:2

here is example from my config file

install babel hmre plugin

npm install --save-dev babel-preset-react-hmre


module.exports = {
  // entry and output options

  module: {
    loaders: [{
      test: /\.js$/,
      exclude: /node_modules/,
      loader: "babel",
      include: __dirname,
      query: {
        presets: [ 'es2015', 'react', 'react-hmre' ]
      }
    }]
  }
}

and edit your start script in package.json to enable the hot option:

"start": "webpack-dev-server --progress --inline --hot",
Answer:3

I updated the ui-bootstrap version from 0.13.0 to 0.13.4, It broke the validation on datepicker field. Plunkr with a problem Previously I was using 0.13.0 and it was working fine. Basically when I ...

I updated the ui-bootstrap version from 0.13.0 to 0.13.4, It broke the validation on datepicker field. Plunkr with a problem Previously I was using 0.13.0 and it was working fine. Basically when I ...

I have some blocker. How can i select character and sort this inside a tag like <p>-tag in jQuery OR Vanilla JS? function alphabet(str){ var x = str.split(" "); return x.sort()....

I have some blocker. How can i select character and sort this inside a tag like <p>-tag in jQuery OR Vanilla JS? function alphabet(str){ var x = str.split(" "); return x.sort()....

I am trying to apply and remove a filter to my active object. My code will apply the filter but will not remove it: $scope.contrastFilter = function () { var obj = canvas.getActiveObject(); ...

I am trying to apply and remove a filter to my active object. My code will apply the filter but will not remove it: $scope.contrastFilter = function () { var obj = canvas.getActiveObject(); ...

I have 2 contenteditable div, i want it that if div (div A) height become greater than 100px move exceed text to another div (div B). I am learning (for example, here div height is greater than 100px,...

I have 2 contenteditable div, i want it that if div (div A) height become greater than 100px move exceed text to another div (div B). I am learning (for example, here div height is greater than 100px,...

  1. fundal height greater than gestational age
  2. div height greater than content
  3. css height greater than 100
  4. row height greater than 409
  5. height is greater than length
  6. fundal height greater than expected
  7. autolayout height greater than
  8. media query height greater than width
  9. css child height greater than parent
  10. excel row height greater than 409
  11. css media height greater than width
  12. if div height greater than scroll
  13. if div height greater than css
  14. if div height greater than
  15. height 100 is greater than parent
  16. height must be greater than 0px
  17. css selector height greater than
  18. css media height greater than
  19. if height is greater than jquery
  20. the height is greater than