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 Jan 10, 2024
1 parent 18979c1 commit 5336800
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 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

A **"Service-Worker-Allowed"** header can be added through the `Rails.configuration.assets.service_worker_scope` variable 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" => Rails.configuration.assets.service_worker_scope) if Rails.configuration.assets.service_worker_scope

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

test "serve a compiled file" do
Rails.configuration.assets.stub :service_worker_scope, "/" do
asset = @assembly.load_path.find("foobar/source/test.css")
get "/#{asset.digested_path}"

assert_equal 200, last_response.status
assert_equal "94", 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=31536000, immutable", last_response.headers['cache-control']
assert_equal ".hero { background: url(\"/foobar/source/file-3e6a129785ee3caf8eff23db339997e85334bfa9.jpg\") }\n",
last_response.body
end
end

test "serve a compiled file without service-worker-allowed" do
asset = @assembly.load_path.find("foobar/source/test.css")
get "/#{asset.digested_path}"

assert_equal 200, last_response.status
assert_equal "94", 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 "public, max-age=31536000, immutable", last_response.headers['cache-control']
assert_nil last_response.headers['service-worker-allowed']
assert_equal ".hero { background: url(\"/foobar/source/file-3e6a129785ee3caf8eff23db339997e85334bfa9.jpg\") }\n",
last_response.body
last_response.body
end

test "serve a predigested file" do
Expand Down

0 comments on commit 5336800

Please sign in to comment.