Skip to content
This repository has been archived by the owner on Mar 30, 2022. It is now read-only.

ryankshaw/querystringless_cache_buster

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Querystringless Cache Buster

Why use this plugin?

Everyone knows by now that it is a best practice to serve up static assets and set a far futures expires header so that you dont even have to have 304 requests.

BUT, that means that you have to somehow invalidate stale uris. The rails default is to put do things like: http://assets1.example.com/images/someimage.png?12341234

from api documentation at http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html#M001720

By default, Rails appends asset‘s timestamps to all asset paths. This allows you to set a cache-expiration 
date for the asset far into the future, but still be able to instantly invalidate it by simply updating the
file (and hence updating the timestamp, which then updates the URL as the timestamp is part of that, which 
in turn busts the cache).

This plugin turns that around and does it like: http://assets1.example.com/12341234/images/someimage.png

Why?

Basically it comes down to stylesheets. If you are doing things the way you should be, then most of your layout images are being linked to from your external css files.
so for example you have: in application.html.erb

<link href="/stylesheets/application.css?1249677471" media="screen" rel="stylesheet" type="text/css" />

and in application.css you have:

body { 
  background-image: url("../images/bg.png"); 
}

The problem is that, while application.css will always be instantly invalidated if it changes, bg.png will never be invalidated because it is being served as http://example.com/images/bg.png. This is because only rails can put the ?12345 cachebusters at the end of the uris, and only when it is using one of the asset tag helpers (ie: stylesheet_link_tag, image_tag, javascript_include_tag, etc.) SO ANY IMAGE LINKED TO FROM A STYLESHEET WILL NOT EVER BE INVALIDATED BECAUSE IT DOES NOT HAVE A ?12341234 AT THE END OF THE URL.

When you use this plugin to flip the location of the cachebuster in the urls generated by rails' asset_tag_helpers and you do the same thing: in application.html.erb

<link href="1249677471/stylesheets/application.css" media="screen" rel="stylesheet" type="text/css" />

and in application.css you have:

body { 
  background-image: url("../images/bg.png"); 
}

The absolute uri of the background-image being linked to is now: http://example.com/1249677471/images/bg.png Notice the difference, the cachebuster is in BOTH the stylesheet's uri AND the image's so even if you edit bg.png it will instantly be invalidated.

One caveat though: YOUR STYLESHEETS MUST USE RELATIVE URLS WHEN THEY LINK TO IMAGES , DO NOT USE ABSOLUTE URLS.

Note: To calculate the cachebuster (the 12341234 part of this url: http://assets1.example.com/12341234/images/someimage.png), when your server starts up it finds the most recent last modified time in your public directory (or if you set ENV["RAILS_ASSET_ID"] it will use that)

In order to use this plugin you need to add:

ENV["RAILS_USE_QUERYSTRINGLESS_CACHE_BUSTER"] =  "true"

to the /config/environments/production.rb file (as well as any other envirnments you want to use )

Copyright (c) 2009 Ryan Shaw, released under the MIT license

About

have urls like /1234/stylesheets/app.css instead of /stylesheets/app.css?1234

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages