Search code examples
gruntjssails.jsgrunt-contrib-concat

How to add a build timestamp at the end of the minified files


I have a real issue now, When I'm on Production and when ever I update my CSS or JS the same file keeps on serving from the server.. I i need to get the new updates then I have to hard refresh the browser,

I'm thinking of having a way to add a build version at the end of my minified file name, So when ever my files are updated and Grunt minifies my files it will output a perfect link which will be newly downloaded by the web browser

Right now the link is like this

<script src="/min/production.min.js"></script>

I need this to be,

(<script src="/min/production[timestamp].min.js"></script>)

Solution

  • What I've done to do this are as following

    tasks/uglify.js

    Replace following line

    dest:'.tmp/public/min/production.min.js' 
    

    with

    dest: global.productionJSName 
    

    tasks/sails-linker.js

    Add following variables to your task

    global.timestamp = global.timestamp || new Date().getTime();
    global.productionJSName = '.tmp/public/min/production-' + global.timestamp + '.min.js';
    
    module.exports = function(grunt) {
        var productionJSName = global.productionJSName // Add this
        ...
    

    Replace all the old path to productionJSName

    ...
    prodJs : {
        ...
        files: {
                '.tmp/public/**/*.html': [productionJSName],
                'views/**/*.html': [productionJSName],
                'views/**/*.ejs': [productionJSName]
            }
    }
    ...
    prodJsRelative : {
        ...
        files: {
                '.tmp/public/**/*.html': [productionJSName],
                'views/**/*.html': [productionJSName],
                'views/**/*.ejs': [productionJSName]
            }
    }
    ...
    prodJsJade : {
        ...
        files: {
                'views/**/*.jade': [productionJSName]
            }
    }
    ...
    prodJsRelativeJade : {
        ...
        files: {
                'views/**/*.jade': [productionJSName]
            }
    }
    

    I'm not sure this is the best way, let me know if you find a better solution.