From 2ce922a34aefbe2ba883f26d94b3441046143f70 Mon Sep 17 00:00:00 2001 From: Sebastien Faure Date: Tue, 5 Sep 2023 15:34:57 +0200 Subject: [PATCH] Add service-worker-allowed header by default --- README.md | 5 ++++- lib/propshaft/server.rb | 21 ++++++++++----------- test/propshaft/server_test.rb | 13 +++++++++++++ 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index a18d46f..f11c1a5 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ 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: @@ -37,6 +37,9 @@ If you have a lot of assets in your project, you can improve performance by addi 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 diff --git a/lib/propshaft/server.rb b/lib/propshaft/server.rb index 67be08e..1c091b5 100644 --- a/lib/propshaft/server.rb +++ b/lib/propshaft/server.rb @@ -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 diff --git a/test/propshaft/server_test.rb b/test/propshaft/server_test.rb index c94a1e3..a091ba7 100644 --- a/test/propshaft/server_test.rb +++ b/test/propshaft/server_test.rb @@ -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}"