Skip to content

Commit

Permalink
Add custom HTTP headers configuration for assets
Browse files Browse the repository at this point in the history
  • Loading branch information
fauresebast committed Sep 11, 2023
1 parent f2b7a39 commit 5bb5f80
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ If you have a lot of assets in your project, you can improve performance by addi
config.file_watcher = ActiveSupport::EventedFileUpdateChecker
```

## Customizing the HTTP headers for assets

If you need to custom you response header not only for standard web requests but also for your assets, you can specify them in your environment file:

```ruby
config.propshaft_headers = { "your-custom-key" => "your-value" }
```

Since `content-length`, `content-type` and `etag` are computed values, you are not allowed to modified them. `vary` and `cache-control` have default values, respectively `"Accept-Encoding"` and `public, max-age=31536000, immutable`, that you can overwrite.

## Migrating from Sprockets

Expand Down
25 changes: 16 additions & 9 deletions lib/propshaft/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,23 @@ def call(env)

if (asset = @assembly.load_path.find(path)) && asset.fresh?(digest)
compiled_content = @assembly.compilers.compile(asset)
computed_headers = {
"content-length" => compiled_content.length.to_s,
"content-type" => asset.content_type.to_s,
"etag" => asset.digest
}

[
200,
{
"content-length" => compiled_content.length.to_s,
"content-type" => asset.content_type.to_s,
"vary" => "Accept-Encoding",
"etag" => asset.digest,
"cache-control" => "public, max-age=31536000, immutable"
},
headers = if Rails.configuration.respond_to?(:propshaft_headers)
Rails.configuration.propshaft_headers.transform_keys(&:downcase).merge(computed_headers)
else
computed_headers
end
headers["vary"] ||= "Accept-Encoding"
headers["cache-control"] ||= "public, max-age=31536000, immutable"

[
200,
headers,
[ compiled_content ]
]
else
Expand Down
24 changes: 24 additions & 0 deletions test/propshaft/server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,30 @@ class Propshaft::ServerTest < ActiveSupport::TestCase
last_response.body
end

test "serve a compiled file with custom headers" do
Rails.configuration.propshaft_headers = { "custom-header" => "CustomHeaderValue"}
asset = @assembly.load_path.find("foobar/source/test.css")
get "/#{asset.digested_path}"

assert_equal 200, last_response.status
assert_equal "CustomHeaderValue", last_response.headers['custom-header']
assert_equal ".hero { background: url(\"/foobar/source/file-3e6a129785ee3caf8eff23db339997e85334bfa9.jpg\") }\n",
last_response.body
Rails.configuration.propshaft_headers = {}
end

test "serve a compiled file with custom headers overriding default value" do
Rails.configuration.propshaft_headers = { "cache-control" => "public, max-age=2592000, immutable"}
asset = @assembly.load_path.find("foobar/source/test.css")
get "/#{asset.digested_path}"

assert_equal 200, last_response.status
assert_equal "public, max-age=2592000, immutable", last_response.headers['cache-control']
assert_equal ".hero { background: url(\"/foobar/source/file-3e6a129785ee3caf8eff23db339997e85334bfa9.jpg\") }\n",
last_response.body
Rails.configuration.propshaft_headers = {}
end

test "serve a predigested file" do
asset = @assembly.load_path.find("file-already-abcdefVWXYZ0123456789.digested.css")
get "/#{asset.digested_path}"
Expand Down

0 comments on commit 5bb5f80

Please sign in to comment.