Skip to content

Instantly share code, notes, and snippets.

@jugglinmike
Created March 16, 2013 22:55
Show Gist options
  • Save jugglinmike/5178676 to your computer and use it in GitHub Desktop.
Save jugglinmike/5178676 to your computer and use it in GitHub Desktop.
Using nanoc to generate a cache-busting URL for external assets.
# Given an item identifier, generate its fully-qualified URL along with the
# item's hash as a query string parameter. Omit this cache-busting mechanism
# in development environments so that HTML documents including this resource
# aren't re-rendered with every change to the asset.
def cache_bust_path( identifier )
static = StorageHelper::static
item = static[:assets][identifier]
if !item
return identifier
end
if static[:live]
# Use the path to the minified representation (where available)
if item.rep_named(:minified)
path = item.path({ rep: :minified })
else
path = item.path()
end
# The `checksum` attribute specifies a checksum for both the the item's
# contents and the item's attributes. These two checksums are
# comma-separated. Remove the attribute checksum to avoid generating
# overlong URLs.
path += "?checksum=" + item.checksum.split(",")[0]
else
path = item.path()
end
path
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment