Replace serviceworker-rails gem with custome implementation (#4987) [deploy]

* Customize UserContext in Timber logs

* Replace serviceworker-rails gem with custome implementation
This commit is contained in:
Ben Halpern 2019-12-03 08:50:56 -05:00 committed by GitHub
parent 8e9c26b6ab
commit 585d672742
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 57 additions and 36 deletions

View file

@ -84,7 +84,6 @@ gem "rouge", "~> 3.13" # A pure-ruby code highlighter
gem "rubyzip", "~> 2.0" # Rubyzip is a ruby library for reading and writing zip files
gem "s3_direct_upload", "~> 0.1" # Direct Upload to Amazon S3
gem "sass-rails", "~> 6.0" # Sass adapter for the Rails asset pipeline
gem "serviceworker-rails", "~> 0.6" # Integrates ServiceWorker into the Rails asset pipeline
gem "sitemap_generator", "~> 6.0" # SitemapGenerator is a framework-agnostic XML Sitemap generator
gem "skylight", "~> 4.2" # Skylight is a smart profiler for Rails, Sinatra, and other Ruby apps
gem "slack-notifier", "~> 2.3" # A slim ruby wrapper for posting to slack webhooks

View file

@ -725,8 +725,6 @@ GEM
selenium-webdriver (3.142.6)
childprocess (>= 0.5, < 4.0)
rubyzip (>= 1.2.2)
serviceworker-rails (0.6.0)
railties (>= 3.1)
shellany (0.0.1)
shoulda-matchers (4.1.2)
activesupport (>= 4.2.0)
@ -973,7 +971,6 @@ DEPENDENCIES
s3_direct_upload (~> 0.1)
sass-rails (~> 6.0)
sdoc (~> 1.0)
serviceworker-rails (~> 0.6)
shoulda-matchers (= 4.1.2)
simplecov (~> 0.17)
sitemap_generator (~> 6.0)

View file

@ -0,0 +1,14 @@
class ServiceWorkerController < ApplicationController
skip_before_action :verify_authenticity_token
before_action :set_cache_control_headers, only: %i[index manifest]
def index
set_surrogate_key_header "serviceworker-js"
render formats: [:js]
end
def manifest
set_surrogate_key_header "manifest-json"
render formats: [:json]
end
end

View file

@ -141,4 +141,4 @@ self.addEventListener('install', onInstall);
self.addEventListener('activate', onActivate);
self.addEventListener('fetch', onFetch);
self.addEventListener('push', onPush);
self.addEventListener('notificationclick', onNotificationClick)
self.addEventListener('notificationclick', onNotificationClick)

View file

@ -30,5 +30,4 @@ Rails.application.config.assets.precompile += %w[s3_direct_upload.js]
Rails.application.config.assets.precompile += %w[classified_listings.css]
Rails.application.config.assets.precompile += %w[lib/xss.js]
Rails.application.config.assets.precompile += %w[lib/pulltorefresh.js]
Rails.application.config.assets.precompile += %w[internal.js]
Rails.application.config.assets.precompile += %w[serviceworker.js manifest.json]
Rails.application.config.assets.precompile += %w[internal.js]

View file

@ -1,29 +0,0 @@
Rails.application.configure do
config.serviceworker.routes.draw do
# map to assets implicitly
match "/serviceworker.js",
headers: { "Cache-Control" => "public, max-age=8000, s-max-age=20000, no-cache" }
match "/manifest.json",
headers: { "Cache-Control" => "public, max-age=8000, s-max-age=20000, no-cache" }
# Examples
#
# map to a named asset explicitly
# match "/proxied-serviceworker.js" => "nested/asset/serviceworker.js"
# match "/nested/serviceworker.js" => "another/serviceworker.js"
#
# capture named path segments and interpolate to asset name
# match "/captures/*segments/serviceworker.js" => "%{segments}/serviceworker.js"
#
# capture named parameter and interpolate to asset name
# match "/parameter/:id/serviceworker.js" => "project/%{id}/serviceworker.js"
#
# insert custom headers
# match "/header-serviceworker.js" => "another/serviceworker.js",
# headers: { "X-Resource-Header" => "A resource" }
#
# anonymous glob exposes `paths` variable for interpolation
# match "/*/serviceworker.js" => "%{paths}/serviceworker.js"
end
config.serviceworker.headers["Surrogate-Control"] = "max-age=20000"
config.serviceworker.headers["Cache-Control"] = "public, s-maxage=20000, max-age=0, no-cache"
end

View file

@ -329,6 +329,10 @@ Rails.application.routes.draw do
get "/embed/:embeddable" => "liquid_embeds#show"
# serviceworkers
get "/serviceworker" => "service_worker#index"
get "/manifest" => "service_worker#manifest"
get "/new" => "articles#new"
get "/new/:template" => "articles#new"

View file

@ -0,0 +1,37 @@
require "rails_helper"
RSpec.describe "ServiceWorker", type: :request do
describe "GET /serviceworker.js" do
it "renders file with proper text" do
get "/serviceworker.js"
expect(response.body).to include("var CACHE_VERSION")
end
it "renders javascript file" do
get "/serviceworker.js"
expect(response.header["Content-Type"]).to include("text/javascript")
end
it "sends a surrogate key (for Fastly's user)" do
get "/serviceworker.js"
expect(response.header["Surrogate-Key"]).to include("serviceworker-js")
end
end
describe "GET /manifest.json" do
it "renders file with proper text" do
get "/manifest.json"
expect(response.body).to include("\"name\": \"#{ApplicationConfig['COMMUNITY_NAME']} Community\"")
end
it "renders json file" do
get "/manifest.json"
expect(response.header["Content-Type"]).to include("application/json")
end
it "sends a surrogate key (for Fastly's user)" do
get "/manifest.json"
expect(response.header["Surrogate-Key"]).to include("manifest-json")
end
end
end