Per File Cache Bust with requirejs
I have been working on a web application where I am using requirejs. I have implemented very efficient cache mechanism.
I have taken help from previous work — http://richardtier.com/2014/10/19/tell-browser-when-files-are-updated/
In the above blogpost, author has mentioned how to use “per file cache bust” using overwriting requirejs.s.contexts._.nameToUrl function.
But blogpost do not mention much on hashMapping. I am using git and whenever you make a commit in git repo a new sha1sum id is generated corresponding to those files. So I wrote some grunt task to generate a file called gitLog.json and gitHash.json. In my project the first file is init.js which is never cached so I have added above json objects to this file.
I am using grunt and grunt-shell to generate json file which basically contains mapping between url and its current Sha1sum id.
I am using git. and this is final screenshot
Now you can see that appLoader.js has different hash and application.js has different hash code. These resource (“.js”) has Expiry date of 5–6 months. So before 5–6 month If I update one file then browser will load only that file. This is superb mechanism which I always wanted to have.
Not just my browser is downloading only “new updated file” it is also NOT downloading files which are not needed to render current view.