4

The problem I am trying to solve when I deploy a new build, the users need to get the latest bundle.js and bundle.css, not the browser cached one. The solution the problem is appending a query string to the filename like /client.bundle.js?v=1.01. The 1.01 comes from the package.json's version property.

The content of the index.html, which is contained in the public folder is below. How do I configure the webpack to modify the index.html automatically when building?

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Loading...</title>
  <meta name="viewport"
    content="width=device-width,user-scalable=no,initial-scale=1">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta charset="utf-8">
  <link href="https://fonts.googleapis.com/css?family=Lato:300,400,600,700" rel="stylesheet">
  <link rel="stylesheet" type="text/css" href="/client.bundle.css">
</head>
<body>
  <div id="app"></div>
  <script src="/bundle.js"></script>
  </script>
</body>
</html>

1 Answer 1

3

In Webpack build system you can use something like this:

const VERSION = JSON.stringify(require('./package.json').version)

And make this available on window object via Webpack's DefinePlugin

new webpack.DefinePlugin({__VERSION : VERSION})

Which you can use later in index.html file or if you use any templating language.

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

Comments

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.