Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add service-worker-allowed header by default #153

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ If you have a lot of assets in your project, you can improve performance by addi
config.file_watcher = ActiveSupport::EventedFileUpdateChecker
```

## Custom Headers

Customs headers can be added through the `Rails.configuration.assets.headers` variable in your environment configuration file. Your headers will be merged with default headers, overriding existing headers.

```ruby
Rails.configuration.assets.headers = {"service-worker-allowed" => "/", "cache-control" => "public, max-age=604800, immutable"}
```

## Migrating from Sprockets

Expand Down
21 changes: 10 additions & 11 deletions lib/propshaft/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,17 @@ def call(env)

if (asset = @assembly.load_path.find(path)) && asset.fresh?(digest)
compiled_content = @assembly.compilers.compile(asset)
headers = {
Rack::CONTENT_LENGTH => compiled_content.length.to_s,
Rack::CONTENT_TYPE => asset.content_type.to_s,
VARY => "Accept-Encoding",
Rack::ETAG => asset.digest,
Rack::CACHE_CONTROL => "public, max-age=31536000, immutable"
}

[
200,
{
Rack::CONTENT_LENGTH => compiled_content.length.to_s,
Rack::CONTENT_TYPE => asset.content_type.to_s,
VARY => "Accept-Encoding",
Rack::ETAG => asset.digest,
Rack::CACHE_CONTROL => "public, max-age=31536000, immutable"
},
[ compiled_content ]
]
headers.merge!(Rails.configuration.assets.headers) if Rails.configuration.assets.headers

[ 200, headers, [ compiled_content ] ]
else
[ 404, { Rack::CONTENT_TYPE => "text/plain", Rack::CONTENT_LENGTH => "9" }, [ "Not found" ] ]
end
Expand Down
17 changes: 17 additions & 0 deletions test/propshaft/server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ class Propshaft::ServerTest < ActiveSupport::TestCase
last_response.body
end

test "serve a compiled file with custom headers" do
Rails.configuration.assets.stub :headers, {"service-worker-allowed" => "/", "cache-control" => "public, max-age=604800, immutable"} do
asset = @assembly.load_path.find("foobar/source/test.css")
get "/#{asset.digested_path}"

assert_equal 200, last_response.status
assert_equal "62", last_response.headers['content-length']
assert_equal "text/css", last_response.headers['content-type']
assert_equal "Accept-Encoding", last_response.headers['vary']
assert_equal asset.digest, last_response.headers['etag']
assert_equal "/", last_response.headers['service-worker-allowed']
assert_equal "public, max-age=604800, immutable", last_response.headers['cache-control']
assert_equal ".hero { background: url(\"/foobar/source/file-3e6a1297.jpg\") }\n",
last_response.body
end
end

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