Skip to content

Commit

Permalink
Add service-worker-allowed header by default
Browse files Browse the repository at this point in the history
  • Loading branch information
fauresebast committed Oct 4, 2023
1 parent 18979c1 commit 2ce922a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ If you need to put multiple files that refer to each other through Propshaft, li

## Improving performance in development

Before every request Propshaft checks if any asset was updated to decide if a cache sweep is needed. This verification is done using the application's configured file watcher which, by default, is `ActiveSupport::FileUpdateChecker`.
Before every request Propshaft checks if any asset was updated to decide if a cache sweep is needed. This verification is done using the application's configured file watcher which, by default, is `ActiveSupport::FileUpdateChecker`.

If you have a lot of assets in your project, you can improve performance by adding the `listen` gem to the development group in your Gemfile, and this line to the `development.rb` environment file:

```ruby
config.file_watcher = ActiveSupport::EventedFileUpdateChecker
```

## Service-Worker-Allowed Header

By default, a **"Service-Worker-Allowed"** header is set with the value "/" to allow service workers to work normally, specifically in your development environment. This header will be present for every asset. It can be removed by setting the `Rails.configuration.assets.remove_service_worker_allowed_header` to `true` in your environment configuration file.

## 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 @@ -10,18 +10,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!("Service-Worker-Allowed" => "/") unless Rails.configuration.assets.remove_service_worker_allowed_header

[ 200, headers, [ compiled_content ] ]
else
[ 404, { Rack::CONTENT_TYPE => "text/plain", Rack::CONTENT_LENGTH => "9" }, [ "Not found" ] ]
end
Expand Down
13 changes: 13 additions & 0 deletions test/propshaft/server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,24 @@ class Propshaft::ServerTest < ActiveSupport::TestCase
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=31536000, immutable", last_response.headers['cache-control']
assert_equal ".hero { background: url(\"/foobar/source/file-3e6a129785ee3caf8eff23db339997e85334bfa9.jpg\") }\n",
last_response.body
end

test "serve a compiled file without service-worker-allowed" do
Rails.configuration.assets.stub :remove_service_worker_allowed_header, true do
asset = @assembly.load_path.find("foobar/source/test.css")
get "/#{asset.digested_path}"

assert_equal 200, last_response.status
assert_nil last_response.headers['service-worker-allowed']
assert_equal ".hero { background: url(\"/foobar/source/file-3e6a129785ee3caf8eff23db339997e85334bfa9.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

0 comments on commit 2ce922a

Please sign in to comment.