Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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 ?

share|improve this question
    
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). –  Shane Madden Feb 7 '12 at 18:12

1 Answer 1

up vote 3 down vote accepted

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

share|improve this answer
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. –  Evan Anderson Feb 7 '12 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. –  sam Feb 7 '12 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. –  Evan Anderson Feb 7 '12 at 22:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.