Rewrite asset hash extension with new extension class

This commit is contained in:
Ben Hollis 2013-04-12 21:53:44 -07:00
parent f7cd0e231e
commit 6bf4059f4a
2 changed files with 21 additions and 19 deletions

View file

@ -83,7 +83,7 @@ module Middleman
# to avoid browser caches failing to update to your new content. # to avoid browser caches failing to update to your new content.
Middleman::Extensions.register(:asset_hash) do Middleman::Extensions.register(:asset_hash) do
require "middleman-more/extensions/asset_hash" require "middleman-more/extensions/asset_hash"
Middleman::Extensions::AssetHash Middleman::Extensions::AssetHash::Extension
end end
# AssetHost allows you to setup multiple domains to host your static # AssetHost allows you to setup multiple domains to host your static

View file

@ -1,27 +1,29 @@
module Middleman module Middleman
module Extensions module Extensions
module AssetHash module AssetHash
class << self class Extension < ::Middleman::Extension
def registered(app, options={}) option :exts, %w(.jpg .jpeg .png .gif .js .css .otf .woff .eot .ttf .svg), "List of extensions that get asset hashes appended to them."
option :ignore, [], "Regexes of filenames to skip adding asset hashes to"
def initialize(app, options_hash={})
super
require 'digest/sha1' require 'digest/sha1'
require 'rack/test' require 'rack/test'
require 'uri' require 'uri'
exts = options[:exts] || %w(.jpg .jpeg .png .gif .js .css .otf .woff .eot .ttf .svg)
# Allow specifying regexes to ignore, plus always ignore apple touch icons
ignore = Array(options[:ignore]) << /^apple-touch-icon/
app.ready do
sitemap.register_resource_list_manipulator(
:asset_hash,
AssetHashManager.new(self, exts, ignore)
)
use Middleware, :exts => exts, :middleman_app => self, :ignore => ignore
end
end end
alias :included :registered
def after_configuration
# Allow specifying regexes to ignore, plus always ignore apple touch icons
ignore = Array(options.ignore) + [/^apple-touch-icon/]
app.sitemap.register_resource_list_manipulator(
:asset_hash,
AssetHashManager.new(app, options.exts, ignore)
)
app.use Middleware, :exts => options.exts, :middleman_app => app, :ignore => ignore
end
end end
# Central class for managing asset_hash extension # Central class for managing asset_hash extension
@ -38,6 +40,7 @@ module Middleman
# Process resources in order: binary images and fonts, then SVG, then JS/CSS. # Process resources in order: binary images and fonts, then SVG, then JS/CSS.
# This is so by the time we get around to the text files (which may reference # This is so by the time we get around to the text files (which may reference
# images and fonts) the static assets' hashes are already calculated. # images and fonts) the static assets' hashes are already calculated.
rack_client = ::Rack::Test::Session.new(@app.class.to_rack_app)
resources.sort_by do |a| resources.sort_by do |a|
if %w(.svg).include? a.ext if %w(.svg).include? a.ext
0 0
@ -51,7 +54,6 @@ module Middleman
next if @ignore.any? { |ignore| Middleman::Util.path_match(ignore, resource.destination_path) } next if @ignore.any? { |ignore| Middleman::Util.path_match(ignore, resource.destination_path) }
# Render through the Rack interface so middleware and mounted apps get a shot # Render through the Rack interface so middleware and mounted apps get a shot
rack_client = ::Rack::Test::Session.new(@app.class)
response = rack_client.get(URI.escape(resource.destination_path), {}, { "bypass_asset_hash" => "true" }) response = rack_client.get(URI.escape(resource.destination_path), {}, { "bypass_asset_hash" => "true" })
raise "#{resource.path} should be in the sitemap!" unless response.status == 200 raise "#{resource.path} should be in the sitemap!" unless response.status == 200