Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
167
rated 0 times [  172] [ 5]  / answers: 1 / hits: 112537  / 13 Years ago, tue, november 29, 2011, 12:00:00

RequireJS seems to do something internally that caches required javascript files. If I make a change to one of the required files, I have to rename the file in order for the changes to be applied.



The common trick of appending a version number as a querystring param to the end of the filename does not work with requirejs <script src=jsfile.js?v2></script>



What I am looking for is a way to prevent this internal cacheing of RequireJS required scripts without having to rename my script files every time they are updated.



Cross-Platform Solution:



I am now using urlArgs: bust= + (new Date()).getTime() for automatic cache-busting during development and urlArgs: bust=v2 for production where I increment the hard-coded version num after rolling out an updated required script.



Note:



@Dustin Getz mentioned in a recent answer that Chrome Developer Tools will drop breakpoints during debugging when Javascript files are continuously refreshed like this. One workaround is to write debugger; in code to trigger a breakpoint in most Javascript debuggers.



Server-Specific Solutions:



For specific solutions that may work better for your server environment such as Node or Apache, see some of the answers below.


More From » jquery

 Answers
4

RequireJS can be configured to append a value to each of the script urls for cache busting.



From the RequireJS documentation (http://requirejs.org/docs/api.html#config):




urlArgs: Extra query string arguments appended to URLs that RequireJS
uses to fetch resources. Most useful to cache bust when the browser or
server is not configured correctly.




Example, appending v2 to all scripts:



require.config({
urlArgs: bust=v2
});


For development purposes, you can force RequireJS to bypass the cache by appending a timestamp:



require.config({
urlArgs: bust= + (new Date()).getTime()
});

[#88845] Monday, November 28, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
loric

Total Points: 110
Total Questions: 96
Total Answers: 91

Location: Estonia
Member since Wed, Jun 8, 2022
2 Years ago
;