Skip to content

Commit

Permalink
Improve serving of prehashed assets
Browse files Browse the repository at this point in the history
  • Loading branch information
theodorton committed Sep 16, 2021
1 parent cddf9fb commit 4795c17
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Get upgrade notes from Sprockets 3.x to 4.x at https://github.com/rails/sprocket

## Master

- Allow assets already fingerprinted to be served through `Sprockets::Server`
- Do not fingerprint files that already contain a valid digest in their name
- Remove remaining support for Ruby < 2.4.[#672](https://github.com/rails/sprockets/pull/672)

Expand Down
12 changes: 11 additions & 1 deletion lib/sprockets/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def call(env)
msg = "Served asset #{env['PATH_INFO']} -"

# Extract the path from everything after the leading slash
path = Rack::Utils.unescape(env['PATH_INFO'].to_s.sub(/^\//, ''))
full_path = Rack::Utils.unescape(env['PATH_INFO'].to_s.sub(/^\//, ''))
path = full_path

unless path.valid_encoding?
return bad_request_response(env)
Expand Down Expand Up @@ -64,6 +65,15 @@ def call(env)
# Look up the asset.
asset = find_asset(path)

# Fallback to looking up the asset with the full path.
# This will make assets that are hashed with webpack or
# other js bundlers work consistently between production
# and development pipelines.
if asset.nil? && (asset = find_asset(full_path))
if_match = asset.etag if fingerprint
fingerprint = asset.etag
end

if asset.nil?
status = :not_found
elsif fingerprint && asset.etag != fingerprint
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("I was already hashed!");
14 changes: 12 additions & 2 deletions test/test_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ def app
assert_equal 304, last_response.status
end

test "200 response for prehashed asset with etag digest by sprockets" do
get "/assets/prehashed-988881adc9fc3655077dc2d4d757d480b5ea0e11.js"
assert_equal 200, last_response.status

etag = last_response.headers['ETag']
digest = etag[/"(.+)"/, 1]

assert_equal 'edabfd0f1ac5fcdae82cc7d92d1c52abb671797a3948fa9040aec1db8e61c327', digest
end

test "ok response with fingerprint and if-nonematch etags don't match" do
get "/assets/application.js"
assert_equal 200, last_response.status
Expand Down Expand Up @@ -298,7 +308,7 @@ def app

sandbox filename do
get "/assets/tree.js"
assert_equal "var foo;\n\n(function() {\n application.boot();\n})();\nvar bar;\nvar japanese = \"日本語\";\n", last_response.body
assert_equal %[var foo;\n\n(function() {\n application.boot();\n})();\nvar bar;\nconsole.log("I was already hashed!");\nvar japanese = \"日本語\";\n], last_response.body

File.open(filename, "w") do |f|
f.write "var baz;\n"
Expand All @@ -309,7 +319,7 @@ def app
File.utime(mtime, mtime, path)

get "/assets/tree.js"
assert_equal "var foo;\n\n(function() {\n application.boot();\n})();\nvar bar;\nvar baz;\nvar japanese = \"日本語\";\n", last_response.body
assert_equal %[var foo;\n\n(function() {\n application.boot();\n})();\nvar bar;\nvar baz;\nconsole.log("I was already hashed!");\nvar japanese = \"日本語\";\n], last_response.body
end
end

Expand Down

0 comments on commit 4795c17

Please sign in to comment.