Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I control caching for releases with PHP, headers, etc for a backbone project?

So we are doing weekly releases of our project and we are running into problems with clients having old stale versions of certain files.

The stack is backbone with requirejs with backbone boilerplate and an Apache2 server with PHP backend.

We have the index html file that gets loaded, template HTML files that get loaded using AJAX, and then all the js files.

This question seems related to https://stackoverflow.com/questions/12103341/backbone-boilerplate-disable-caching but I didn't see a good answer there.

I've heard that cache-control headers and mod expires and mod headers for PHP might be helpful, but I don't know how to put it all together.

Essentially, what we want to do is make sure on a release of new code to the prod server that everything is not cached at least once. THen after that, normal caching to increase load speed would be ideal.

At the very least, I would liek to understand how to completely prevent these things from being cached.

Any ideas?

like image 994
Evan Avatar asked Oct 19 '12 19:10

Evan


2 Answers

I've found it difficult to control the user client's browser as far as caching is concerned. One trick I've used in the past is to append a random number to my JS file URL. Like

<script src="https://www.mydomain.com/myjsfile.js?123456789"></script>

OR

<script src="https://www.mydomain.com/myjsfile.js?releaseID=123456789"></script>

That causes the client to treat it as a new file altogether. You could assign a random number to each release and that should cause your users' browsers to pull the new JS file again. Same applies to CSS.

I hope this helps. Good luck.

like image 141
Bryan Allo Avatar answered Oct 25 '22 18:10

Bryan Allo


With requirejs, there is a parameter called urlargs it adds a parameter to all your requirejs requests and can be employed to force cache update. The example on the page use a timestamp, but you probably looking for a build version.

However, you should use r.js to build a production version of your scripts (it compiles and minifies all files loaded with require and produces a single library). This will reduce load time and save you a lot of worries. You would them link just this one library and have the build version in the file name itself (something like backbone.app.1.0.23456.js).

like image 43
nxtwrld Avatar answered Oct 25 '22 17:10

nxtwrld