FEATURE: Normalize the service worker route (#12343)

FEATURE: Normalize the service worker route (#12343)

Re-lands the change initially proposed on #8359 but without a new nginx location block, so it has less change surface.

Co-authored-by: Jeff Wong awole20@gmail.com

Co-authored-by: Jeff Wong awole20@gmail.com

diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index 1709230..4972fa2 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -512,8 +512,6 @@ module ApplicationHelper
   end
 
   def client_side_setup_data
-    service_worker_url = Rails.env.development? ? 'service-worker.js' : Rails.application.assets_manifest.assets['service-worker.js']
-
     setup_data = {
       cdn: Rails.configuration.action_controller.asset_host,
       base_url: Discourse.base_url,
@@ -521,7 +519,7 @@ module ApplicationHelper
       environment: Rails.env,
       letter_avatar_version: LetterAvatar.version,
       markdown_it_url: script_asset_path('markdown-it-bundle'),
-      service_worker_url: service_worker_url,
+      service_worker_url: 'service-worker.js',
       default_locale: SiteSetting.default_locale,
       asset_version: Discourse.assets_digest,
       disable_custom_css: loading_admin?,
diff --git a/config/nginx.sample.conf b/config/nginx.sample.conf
index 2786339..9fd6c51 100644
--- a/config/nginx.sample.conf
+++ b/config/nginx.sample.conf
@@ -31,6 +31,13 @@ map $http_x_forwarded_proto $thescheme {
 
 log_format log_discourse '[$time_local] "$http_host" $remote_addr "$request" "$http_user_agent" "$sent_http_x_discourse_route" $status $bytes_sent "$http_referer" $upstream_response_time $request_time "$upstream_http_x_discourse_username" "$upstream_http_x_discourse_trackview" "$upstream_http_x_queue_time" "$upstream_http_x_redis_calls" "$upstream_http_x_redis_time" "$upstream_http_x_sql_calls" "$upstream_http_x_sql_time"';
 
+# Allow bypass cache from localhost
+geo $bypass_cache {
+  default         0;
+  127.0.0.1       1;
+  ::1             1;
+}
+
 server {
 
   access_log /var/log/nginx/access.log log_discourse;
@@ -240,6 +247,7 @@ server {
       proxy_cache_key "$scheme,$host,$request_uri";
       proxy_cache_valid 200 301 302 7d;
       proxy_cache_valid any 1m;
+      proxy_cache_bypass $bypass_cache;
       proxy_pass http://discourse;
       break;
     }
diff --git a/config/routes.rb b/config/routes.rb
index 159c6e7..7a88938 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -864,7 +864,7 @@ Discourse::Application.routes.draw do
       # current site before updating to a new Service Worker.
       # Support the old Service Worker path to avoid routing error filling up the
       # logs.
-      get "/service-worker.js" => redirect(relative_url_root + service_worker_asset, status: 302), format: :js
+      get "/service-worker.js" => "static#service_worker_asset", format: :js
       get service_worker_asset => "static#service_worker_asset", format: :js
     elsif Rails.env.development?
       get "/service-worker.js" => "static#service_worker_asset", format: :js
diff --git a/lib/tasks/assets.rake b/lib/tasks/assets.rake
index 5d9f28a..9263f01 100644
--- a/lib/tasks/assets.rake
+++ b/lib/tasks/assets.rake
@@ -65,7 +65,12 @@ end
 
 task 'assets:flush_sw' => 'environment' do
   begin
-    # Pending due to test failures.
+    hostname = Discourse.current_hostname
+    default_port = SiteSetting.force_https? ? 443 : 80
+    port = SiteSetting.port.to_i > 0 ? SiteSetting.port : default_port
+    STDERR.puts "Flushing service worker script"
+    `curl -s -m 1 --resolve '#{hostname}:#{port}:127.0.0.1' #{Discourse.base_url}/service-worker.js > /dev/null`
+    STDERR.puts "done"
   rescue
     STDERR.puts "Warning: unable to flush service worker script"
   end

GitHub sha: 9118bb20

This commit appears in #12343 which was approved by featheredtoast. It was merged by Falco.