1

Is there a way to force browsers to reload all javascript files ( *.js )from apache ??

I've got a beta site up, and every time I make a change to a javascript file, I've gotta tell my users to clear their cache. Can I force apache to not return 300/301/302 status for .js files ? If I could, would that even solve anything ?

1
  • How's Apache configured to cache? Unless you've set mod_cache or some other mechanism to do so, Apache will check the file every time; it will only return a 304 response if it thinks the file has not been modified (say, if the If-Modified-Since header from the client is older than the modify timestamp on the file). Feb 7, 2012 at 18:12

1 Answer 1

3

Try using versioned files, like Rails and some other frameworks do. So instead of

/js/myscript.js

use a URL like

/assets/js/myscript-[MD5 Of File].js

Create a simple shell script to copy files from /js/ to /assets/js/ then insert the data into your application. This way even if only a single character is changed in the source file, the URL is different so all caches will be invalid. You can't /force/ a browser to un-cache a file if it isn't even requesting it. If you have your caching set up correctly the browsers won't be checking for If-modified-since versions

3
  • 1
    +1 - Using a different filename is fine. I'd use a monotonically increasing sequence rather than an MD5 sum if only to keep the filename length smaller. Feb 7, 2012 at 18:14
  • @EvanAnderson Is filename length that important? When you get to 10, 50, 100 app servers, all needing to know the correct sequence number, whereas any server could generate the MD5 with no extra metadata. That's my personal preference/opinion, I have in the past used file names based on Jenkins build numbers and it works just the same.
    – user80776
    Feb 7, 2012 at 21:54
  • You have a valid point w/ respect to to the MD5 sum. I was assuming some kind of replication system being used to distribute the files to the web servers that deliver the JavaScript files, but there could be times when you need the web servers to be able to generate the filenames deterministically. re: the file name length - Call me old school, but I think about every byte hitting the wire as a "tax" that I'm paying, and the fewer bytes I send the better. Feb 7, 2012 at 22:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .