Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

3rd Party Script Caching in Rails 3.1

I have a script 3rd party websites are using: /assets/script.js. For obvious reasons, I can't ask them to change the link every time I deploy to point to the latest fingerprinted version of the script. I got a few caching issues where users still see old versions of /script.js. Are there any ways to make the cache go away directly for script.js instead of script-9dc5afea3571ba2a883a72b0da0bb623.js?

More Information: Rails on Passenger + Nginx. Looking for ways to serve the script.js file instead if the finger-printed file and invalidate the cache on every deployment.

I thought about adding ETags based on the deployment git revision, but have no idea how to do this. Nginx has no built in ETags support. There are unsupported old third party modules that do this. I can use add_header Etag="something" for this, but how do I add the git version there.

Any other ideas and options?

Thanks!

like image 854
CamelCamelCamel Avatar asked Dec 23 '12 22:12

CamelCamelCamel


2 Answers

If you have a script with a name that is part of your public interface then you need to start versioning this script explicitly, and keeping old versions around for older clients.

e.g. /assets/script.1.0.js, /assets/script.1.1.js etc

The key part is that you need to be keeping the old ones around, and the code doesn't change without the name changing explicitly. The Rails asset pipeline can't do this for you, since there's usually only the very latest version of the script kept current.

As with all public interfaces, you will need to spend more time on managing this process than you would for an internal-only script.

like image 151
Tom Fakes Avatar answered Oct 26 '22 16:10

Tom Fakes


I recommend using an ETag. Add an ETag header to your response http://en.wikipedia.org/wiki/HTTP_ETag

Set the ETag header to a different, unique string for each version of your script. This will make sure browsers get a new version of the script whenever you deploy a new version .

like image 32
Pasha Bitz Avatar answered Oct 26 '22 15:10

Pasha Bitz