0

I have a React nodejs app running on EC2.

I have set up 3 instances of it beyond Nginx for the load balancing.

I have also enabled cache in the Nginx configuration.

Basically everything should be cached beside different versions of app.js which holds the bundled React code and style.css which is also bundled.

I would like to add a version number in the js and css src link (e.g http://mywebsite.com/app.js?1.0)

My question is, can I automate this operation with webpack? Is this the way to go?

1 Answer 1

3

html-webpack-plugin is your friend here.

Instead of creating your index.html file, allow webpack to do it for you.

var HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {

    entry: "./index.js",
    output: {
        filename: "./dist/app.bundle.[hash].js"
    },
    plugins: [
        new HtmlWebpackPlugin({
            hash: true,
            filename: './dist/index.html'
        })
   ]

}

This will add the output script into index.html automatically and will generate a hash for the file.

Sign up to request clarification or add additional context in comments.

1 Comment

Great did the trick, both for js and css. However I have noticed that if you set hash to true, it adds ?[hash] to the src link which is not recommended by google speed test and other benchmarking tools. I have removed it and just added it to the file name as you recommended. I have also used the template option because I have some initial things there I would like to keep. Thanks again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.