DEV: Correct service-worker sourceMappingURL (#15916)

We serve `service-worker.js` in an unusual way, which means that the sourcemap is not available on an adjacent path. This means that the browser fails to fetch the map, and shows an error in the console.

This commit re-writes the source map reference in the static_controller to be an absolute link to the asset (including the appropriate CDN, if enabled), and adds a spec for the behavior.

It's important to do this at runtime, rather than JS precompile time, so that changes to CDN configuration do not require re-compilation to take effect.
This commit is contained in:
David Taylor
2022-02-14 12:47:56 +00:00
committed by GitHub
parent 6ab4d26d84
commit 07893779df
3 changed files with 29 additions and 4 deletions

View File

@ -404,4 +404,27 @@ describe StaticController do
end
end
end
describe "#service_worker_asset" do
it "works" do
get "/service-worker.js"
expect(response.status).to eq(200)
expect(response.content_type).to start_with("application/javascript")
expect(response.body).to include("workbox")
end
it "replaces sourcemap URL" do
Rails.application.assets_manifest.stubs(:find_sources).with("service-worker.js").returns([
<<~JS
someFakeServiceWorkerSource();
//# sourceMappingURL=service-worker-abcde.js.map
JS
])
get "/service-worker.js"
expect(response.status).to eq(200)
expect(response.content_type).to start_with("application/javascript")
expect(response.body).to include("sourceMappingURL=/assets/service-worker-abcde.js.map")
end
end
end