elanh
elanh

Reputation: 1441

How can I insert a version number to index.html when building with Grunt?

I am building my app using Grunt, and I want to generate a build number on each commit. I want this number to be inserted into my index.html file in order to allow cache busting.

My index.html:

<html>
<head>
    <link type="text/css" rel="stylesheet" href="assets/css/main.css?v=<%VERSION%>" />
</head>
<body>
<script>
    //require global configuration
    var require = {
        "urlArgs": "v=<%VERSION%>"
    };
</script>
<!--entry point to application is main.js-->
<script data-main="main.js" src="assets/js/lib/require/require.js"></script>
</body>
</html>

So I want to replace the <%VERSION%> in the CSS link and the require configuration.

Are there any Grunt tasks that can do that? I am using the grunt-contrib-requirejs for optimization. Can that help?

Upvotes: 1

Views: 905

Answers (1)

elanh
elanh

Reputation: 1441

I ended up keeping it simple and used the grunt-text-replace plugin to replace the placeholder (@@BUST@@) with a timestamp.

replace: {
            bust: {
                src: ['./target/*.html'],
                overwrite: true,                 // overwrite matched source files
                replacements: [
                    {
                        from: '@@BUST@@',
                        to: "<%= new Date().getTime() %>"
                    }
                ]
            }
        }

Upvotes: 2

Related Questions