Module: Juicer::CacheBuster

Defined in:
lib/juicer/cache_buster.rb

Overview

Tool that assists in creating filenames that update everytime the file contents change. There’s two ways of generating filenames, soft and hard. The point of all this is to facilitate configuring web servers to send static assets with a far future expires header - improving end user performance through caching.

Soft cache busters require no web server configuration, but will not work as intended with older default configurations for popular proxy server Squid. The soft busters use query parameters to create unique file names, and these may not force an update in some cases. The soft cache busters transforms /images/logo.png to /images/logo.png?cb=1232923789

Hard cache busters change the file name itself, and thus requires either the web server to (internally) rewrite requests for these files to the original ones, or the file names to actually change. Hard cache busters transforms /images/logo.png to /images/logo-1232923789.png

Class Method Summary collapse

Class Method Details

.path(file, type = :soft, param = :undef) ⇒ Object

Creates a unique file name for every revision to the files contents. Default parameter name for soft cache busters is cb (ie ?cb=<timestamp>) while default parameter names for hard cache busters is none (ie file-<timestamp>.png).



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/juicer/cache_buster.rb', line 27

def self.path(file, type = :soft, param = :undef)
  param = (type == :soft ? "jcb" : nil) if param == :undef
  f = File.new(file.split("?").first)
  mtime = f.mtime.to_i
  f.close

  if type == :soft
    param = "#{param}".length == 0 ? "" : "#{param}="
    file = file.sub(/#{param}\d+/, "").sub(/(\?|\&)$/, "")
    "#{file}#{file.index('?') ? '&' : '?'}#{param}#{mtime}"
  else
    parts = file.split(".")
    suffix = parts.pop
    file = parts.join.sub(/-#{param}\d+/, "")
    "#{parts.join('.')}-#{param}#{mtime}.#{suffix}"
  end
end