开发者

How do I use the subversion revision of a css file to prevent browser cache

开发者 https://www.devze.com 2022-12-25 07:25 出处:网络
StackOverflow implements it like this: <link rel=\"stylesheet\" href=\"http://sstatic.net/so/all.css?v=4542\">

StackOverflow implements it like this:

 <link rel="stylesheet" href="http://sstatic.net/so/all.css?v=4542">

Every time the referenced files change, the href attribute of the link tag is updated in the HTML code, thus supporting caching and updated referenced files.

My question - how do you retrieve the subversion version of that css file to include in the link? Subversion开发者_C百科 keywords only tell you the revision of the file you are currently in.

I'm working with PHP/CodeIgniter + jQuery.


all.css is a static file.

The parameter is incremented each time the file is changed to make sure that the browser doesn't cache it incorrectly, as http://sstatic.net/so/all.css?v=4542 and http://sstatic.net/so/all.css?v=400 are seen as different files.

This is also commonly used in ad-networks where 'hits' are counted based on the number of times the graphic is downloaded, and browser caches would screw with the true value of views.


Use what is called a svn commit hook. Sorry i dont got the details, but hopefully you can find those by googling.

Btw... you can use the timestamp of the file as cachebuster

index.php?p=blabla&v=".filemtime('mystyle.css') 


Run the command svn info --xml /path/to/file, and look at the info/commit/revision. This is the last revision when that particular file changed.

Using the above, you can write a utility to create a mapping between js/css/img file and its version number. This mapping can then be loaded in php and appended as a query string like stackoverflow does.

The advantage of this approach is that the version will only change when the resource actually changes. You can therefore set aggressive cache headers - essentially telling the browser to cache the resource for ever.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号