2

I've seen the many solutions for using urlArgs to force the file to change but is there an efficient way to have it automatically change only single files if the file has been updated?

Is it possible to bust the cache of only files that have been modified?

The biggest example being this topic.

function bust(path) {
    return path + '?bust=' + (new Date()).getTime();
}

require.config({
    baseUrl: '/base/path',
    paths: {
        'fileAlias': bust('fileLikelyToChange'),
        'anotherFileAlias': bust('anotherFileLikelyToChange'),
        'jQuery': 'jQuery'
    },
});

The problem is this solution busts the cache every time instead of only when the file has been modified.

Community
  • 1
  • 1
nicholinde
  • 21
  • 1
  • How could RequireJS know if the files had been modified? – idbehold Sep 05 '14 at 19:24
  • Your question is my answer. There seems to be no feasible way to do this at all with RequireJS. Just trying to look for solutions where there probably are none. :/ – nicholinde Sep 05 '14 at 19:33

1 Answers1

0

My solution may be a bit heavy handed and is a hybrid of this and other solutions posted but it works for me.

Part 1: In the html, append all dependent scripts with cache busting query string-->

<script type="text/javascript">
    var require = {
        urlArgs : "bust="+ Math.random()
}
</script>

<script data-main="js/app" src="js/require.js"></script>

Part 2: Then in app.js add a custom bust method to apply to only the files that may have changed:

function bust(path) {
    return path + '.js?bust=' + Math.random();
}

Part 3: update the urlArs with an empty string: urlArgs: "" to overide cache busting query string ubiquitously applied in Part 1 and add the cache busting method only to files of your choice:

requirejs.config({
    urlArgs: "",
    paths: {
        'utilities': 'utilities',
        'controller': bust('controller'),
        'main': 'main'
    }
});
dborde
  • 1