Skip to content

Commit

Permalink
Merge pull request #77 from tonydehnke/Service-Worker-Name-Change-and…
Browse files Browse the repository at this point in the history
…-Error-Reporting

Update Service Worker Messages, Add error reporting message and test
  • Loading branch information
rossta authored Jul 10, 2019
2 parents 35b28be + 5c472ab commit c2ea427
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
15 changes: 10 additions & 5 deletions lib/generators/serviceworker/templates/serviceworker-companion.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
if (navigator.serviceWorker) {
navigator.serviceWorker.register('/serviceworker.js', { scope: './' })
.then(function(reg) {
console.log('[Companion]', 'Service worker registered!');
});
}
navigator.serviceWorker
.register("/serviceworker.js", { scope: "./" })
.then(function() {
console.log("[Companion]", "Rails Service worker registered!")
})
.catch(function(error) {
// registration failed :(
console.log("[Companion]", "Rails Service worker registration failed: " + error)
})
}
6 changes: 3 additions & 3 deletions lib/generators/serviceworker/templates/serviceworker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var CACHE_VERSION = 'v1';
var CACHE_NAME = CACHE_VERSION + ':sw-cache-';

function onInstall(event) {
console.log('[Serviceworker]', "Installing!", event);
console.log('[Serviceworker]', "Rails Service Worker Installing!", event);
event.waitUntil(
caches.open(CACHE_NAME).then(function prefill(cache) {
return cache.addAll([
Expand All @@ -21,7 +21,7 @@ function onInstall(event) {
}

function onActivate(event) {
console.log('[Serviceworker]', "Activating!", event);
console.log('[Serviceworker]', "Rails Service Worker Activating!", event);
event.waitUntil(
caches.keys().then(function(cacheNames) {
return Promise.all(
Expand Down Expand Up @@ -51,7 +51,7 @@ function onFetch(event) {
// if not found in cache, return default offline content for navigate requests
if (event.request.mode === 'navigate' ||
(event.request.method === 'GET' && event.request.headers.get('accept').includes('text/html'))) {
console.log('[Serviceworker]', "Fetching offline content", event);
console.log('[Serviceworker]', "Rails Service Worker Fetching offline content", event);
return caches.match('/offline.html');
}
})
Expand Down
2 changes: 1 addition & 1 deletion test/serviceworker/install_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ServiceWorker::InstallGeneratorTest < ::Rails::Generators::TestCase

test "generates companion javascript" do
assert_file "app/assets/javascripts/serviceworker-companion.js" do |content|
assert_match(/navigator.serviceWorker.register/, content)
assert_match(/navigator.serviceWorker./, content)
end

assert_file "app/assets/javascripts/application.js" do |content|
Expand Down

0 comments on commit c2ea427

Please sign in to comment.