Permalink
Browse files

Serve fingerprinted combinded asset in production

  • Loading branch information...
1 parent f5dd539 commit 4263e8f47f987e8254d3d71248b72e6879ff5c05 @peteog committed Dec 15, 2011
Showing with 33 additions and 1 deletion.
  1. +33 −1 lib/sinatra/simple_assets.rb
@@ -31,6 +31,32 @@ def initialize(name, type, root, files)
def name
"#{@name}.#{@type}"
end
+
+ def hash_name
+ "#{@name}-#{hash}.#{@type}"
+ end
+
+ def hashed_path
+ "#{path}/#{hash_name}"
+ end
+
+ def hash
+ @hash ||= Digest::SHA1.hexdigest combined
+ end
+
+ def content
+ combined
+ end
+
+ def combined
+ @combined ||= @files.map do |file|
+ File.open(@root + file) { |f| f.read }
+ end.join("\n")
+ end
+
+ def path
+ @type == :js ? 'javascripts' : 'stylesheets'
+ end
end
class Assets
@@ -57,7 +83,13 @@ def create_bundle(name, type, files)
def paths_for(bundle)
bundle = @bundles[bundle]
return [] unless bundle
- bundle.files
+
+ if @app.environment == :production
+ @hashes[bundle.hash_name] = bundle.name
+ [bundle.hashed_path]
+ else
+ bundle.files
+ end
end
def content_for(bundle)

0 comments on commit 4263e8f

Please sign in to comment.