DEV: Stop using sprockets to compile service-worker js (#31796)

The service worker isn't served via normal asset paths or the CDN.
Instead, the ERB was being compiled by sprockets, fished out of the
`public/` directory by the static_controller, and then the
sprockets-specific stuff like `sourceMappingUrl` was being removed.

Instead, we can put the ERB under `views/static/`, and have it evaluate
at runtime. There are only a couple of super-cheap interpolations, plus
the route is cached in nginx, so there is no performance concern.

This takes us one step closer to removing sprockets.
This commit is contained in:
David Taylor
2025-03-13 12:49:33 +00:00
committed by GitHub
parent f87e5aab0b
commit e8f4433872
4 changed files with 3 additions and 55 deletions

View File

@ -220,23 +220,7 @@ class StaticController < ApplicationController
# Maximum cache that the service worker will respect is 24 hours.
# However, ensure that these may be cached and served for longer on servers.
immutable_for 1.year
if Rails.application.assets_manifest.assets["service-worker.js"]
path =
File.expand_path(
Rails.root +
"public/assets/#{Rails.application.assets_manifest.assets["service-worker.js"]}",
)
response.headers["Last-Modified"] = File.ctime(path).httpdate
end
content = Rails.application.assets_manifest.find_sources("service-worker.js").first
base_url = File.dirname(helpers.script_asset_path("service-worker"))
content =
content.sub(%r{^//# sourceMappingURL=(service-worker-.+\.map)$}) do
"//# sourceMappingURL=#{base_url}/#{Regexp.last_match(1)}"
end
render(plain: content, content_type: "application/javascript")
render "service-worker"
end
end
end

View File

@ -1,7 +1,7 @@
'use strict';
var chatRegex = /\/chat\/channel\/(\d+)\//;
var inlineReplyIcon = "<%= UrlHelper.absolute("/images/push-notifications/inline_reply.png") %>";
var inlineReplyIcon = "<%= ::UrlHelper.absolute("/images/push-notifications/inline_reply.png") %>";
function showNotification(title, body, icon, badge, tag, baseUrl, url) {
var notificationOptions = {

View File

@ -25,7 +25,6 @@ Rails.application.config.assets.precompile += [
Rails.application.config.assets.precompile += %w[
break_string.js
service-worker.js
locales/i18n.js
scripts/discourse-test-listen-boot
]

View File

@ -339,43 +339,8 @@ RSpec.describe StaticController 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.content_type).to start_with("text/javascript")
expect(response.body).to include("addEventListener")
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
{
"/assets/service-worker.js" => "/assets/service-worker-abcde.js.map",
"/assets/service-worker.js.br" => "/assets/service-worker-abcde.js.map",
"/assets/service-worker.br.js" => "/assets/service-worker-abcde.js.map",
"/assets/service-worker.js.gz" => "/assets/service-worker-abcde.js.map",
"/assets/service-worker.gz.js" => "/assets/service-worker-abcde.js.map",
"https://example.com/assets/service-worker.js" =>
"https://example.com/assets/service-worker-abcde.js.map",
"https://example.com/subfolder/assets/service-worker.js" =>
"https://example.com/subfolder/assets/service-worker-abcde.js.map",
}.each do |asset_path, expected_map_url|
ActionController::Base
.helpers
.stubs(:asset_path)
.with("service-worker.js")
.returns(asset_path)
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=#{expected_map_url}\n")
end
end
end
end