Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial version of basic service worker #698

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@

# Add back any keep files that might have been excluded by the above
!.keep
node_modules
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ gem 'jbuilder'
gem 'paperclip', '~> 5.3'
gem 'maxminddb'
gem 'redcarpet'

gem 'serviceworker-rails'
gem 'aws-sdk', '~> 2.0'

group :development, :test do
Expand Down
9 changes: 6 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ GEM
database_cleaner (1.6.2)
delayed-web (0.4.4)
rails (>= 3.2.13)
delayed_job (4.1.3)
activesupport (>= 3.0, < 5.2)
delayed_job (4.1.5)
activesupport (>= 3.0, < 5.3)
delayed_job_active_record (4.1.2)
activerecord (>= 3.0, < 5.2)
delayed_job (>= 3.0, < 5)
Expand Down Expand Up @@ -266,6 +266,8 @@ GEM
tilt (>= 1.1, < 3)
scrypt (3.0.5)
ffi-compiler (>= 1.0, < 2.0)
serviceworker-rails (0.5.5)
railties (>= 3.1)
shoulda-matchers (3.1.2)
activesupport (>= 4.0.0)
slack-notifier (2.3.1)
Expand Down Expand Up @@ -349,6 +351,7 @@ DEPENDENCIES
redcarpet
rspec-rails
sass-rails (~> 5.0)
serviceworker-rails
shoulda-matchers
slack-notifier
test_after_commit
Expand All @@ -359,4 +362,4 @@ DEPENDENCIES
will_paginate

BUNDLED WITH
1.16.6
1.17.2
32 changes: 32 additions & 0 deletions app/assets/javascripts/serviceworker.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Welcome to your Workbox-powered service worker!
*
* You'll need to register this file in your web app and you should
* disable HTTP caching for this file too.
* See https://goo.gl/nhQhGp
*
* The rest of the code is auto-generated. Please don't update this file
* directly; instead, make changes to your Workbox build configuration
* and re-run your build process.
* See https://goo.gl/2aRDsh
*/

importScripts("https://storage.googleapis.com/workbox-cdn/releases/4.1.1/workbox-sw.js");

workbox.core.skipWaiting();

workbox.core.clientsClaim();

/**
* The workboxSW.precacheAndRoute() method efficiently caches and responds to
* requests for URLs in the manifest.
* See https://goo.gl/S9QRab
*/
self.__precacheManifest = [].concat(self.__precacheManifest || []);
workbox.precaching.precacheAndRoute(self.__precacheManifest, {});

workbox.routing.registerRoute(/.*.html/, new workbox.strategies.NetworkOnly({ "cacheName":"parliament-petitions-html-cache-", plugins: [] }), 'GET');
workbox.routing.registerRoute(/https:\/\/www.(?:googletagmanager|google-analytics).com\/(.*)/, new workbox.strategies.NetworkOnly({ "cacheName":"parliament-petitions-analytics-cache-", plugins: [] }), 'GET');
workbox.routing.registerRoute(/count.json/, new workbox.strategies.NetworkOnly({ "cacheName":"parliament-petitions-data-cache-", plugins: [] }), 'GET');
workbox.routing.registerRoute(/.*.(?:css|js)/, new workbox.strategies.CacheFirst({ "cacheName":"parliament-petitions-application-cache-", plugins: [new workbox.expiration.Plugin({ maxAgeSeconds: 2592000, purgeOnQuotaError: false })] }), 'GET');
workbox.routing.registerRoute(/.*.(?:png|jpg|jpeg|ico|gif)/, new workbox.strategies.CacheFirst({ "cacheName":"parliament-petitions-image-cache-", plugins: [new workbox.expiration.Plugin({ maxAgeSeconds: 2592000, purgeOnQuotaError: false })] }), 'GET');
9 changes: 9 additions & 0 deletions app/views/application/_service_worker.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script type="module">
import {Workbox} from 'https://storage.googleapis.com/workbox-cdn/releases/4.0.0/workbox-window.prod.mjs';

if ('serviceWorker' in navigator) {
const wb = new Workbox('/serviceworker.js');

wb.register();
}
</script>
2 changes: 2 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,7 @@
<%= javascript_include_tag 'character-counter' %>
<% end %>
<% end %>

<%= render 'application/service_worker' %>
</body>
</html>
1 change: 1 addition & 0 deletions config/initializers/assets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@

# Compress JavaScript assets.
Rails.application.config.assets.js_compressor = :uglifier
Rails.configuration.assets.precompile += %w[serviceworker.js]
25 changes: 25 additions & 0 deletions config/initializers/serviceworker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Rails.application.configure do
config.serviceworker.routes.draw do
# map to assets implicitly
match "/serviceworker.js"

# 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
end
57 changes: 57 additions & 0 deletions config/workbox-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
module.exports = {
// output to an ERB file so can be consumed by 'serviceworker-rails' gem
swDest: 'app/assets/javascripts/serviceworker.js.erb',
// globbing only used for pre-caching
globDirectory: '.',
globPatterns: [],
// only allow caching of files under 1MB
maximumFileSizeToCacheInBytes: 1 * 1024 * 1024,
// use CDN version of WB (can also be local or disabled)
importWorkboxFrom: 'cdn',
// claim any browser tabs that are running immediately
clientsClaim: true,
// skip the wait and init immediately
skipWaiting: true,
// define custom caching routes for more flexibility
runtimeCaching: [
// HTML will always be served from the network
{
urlPattern: new RegExp('.*\.html'),
handler: 'NetworkOnly'
},
// Analytics always comes from the network
{
urlPattern: new RegExp('https://www.(?:googletagmanager|google-analytics).com/(.*)'),
handler: 'NetworkOnly'
},
// JSON file to update the counter always comes from the network
{
urlPattern: new RegExp('count.json'),
handler: 'NetworkOnly'
},
// CSS / JS assets using cache first strategy since filenames are revisioned
{
urlPattern: new RegExp('.*.(?:css|js)'),
handler: 'CacheFirst',
options: {
cacheName: 'parliament-petitions-application-cache-',
// assets only cached for 30 days ensuring older revisions clear out after this time
expiration: {
maxAgeSeconds: 30 * 24 * 60 * 60
}
}
},
// Image assets using cache first strategy since filenames are revisioned
{
urlPattern: new RegExp('.*.(?:png|jpg|jpeg|ico|gif)'),
handler: 'CacheFirst',
options: {
cacheName: 'parliament-petitions-image-cache-',
// assets only cached for 30 days ensuring older revisions clear out after this time
expiration: {
maxAgeSeconds: 30 * 24 * 60 * 60
}
}
}
]
};
Loading