Skip to content

Commit

Permalink
Removed experiment with code sharing between front and back end. PITA…
Browse files Browse the repository at this point in the history
… for users of the SW Client.
  • Loading branch information
daffinm committed Apr 4, 2020
1 parent d4f525a commit bf0c358
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 21 deletions.
3 changes: 1 addition & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
content="user-scalable=yes, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">

<script src="js/assert.js"></script>
<script src="js/debug-console.js"></script>
<script src="js/sw-utils.js"></script>
<script src="js/sw-client.js"></script>
<script src="js/debug-console.js"></script>

<link rel="manifest" href="manifest.json">
<style>
Expand Down
15 changes: 12 additions & 3 deletions js/sw-client.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Uses assert from assert.js
// Uses SwUtils.logRegistration from sw-utils.js

function ServiceWorkerClient(url, debug, ui) {
assert.isDefined(url);
Expand Down Expand Up @@ -31,7 +30,7 @@ function ServiceWorkerClient(url, debug, ui) {
debug.log('Registering service worker...');
navigator.serviceWorker.register(url)
.then(function (reg) {
SwUtils.logRegistration(reg, 'Service worker registered', debug);
logRegistration(reg, 'Service worker registered', debug);
navigator.serviceWorker.waiting.then(function(reg) {
debug.log('New waiting service worker found.');
handleUpdateTo(reg, false);
Expand All @@ -47,7 +46,7 @@ function ServiceWorkerClient(url, debug, ui) {
navigator.serviceWorker.register(url).then(async function (reg) {
await fetch(url, {method: 'HEAD'}); // trigger error if offline.
reg.update().then(function (reg) {
SwUtils.logRegistration(reg, 'Checking for updates', debug);
logRegistration(reg, 'Checking for updates', debug);
if (updateIsAvailable(reg)) {
debug.log('Update found by update checker. Handling it...');
handleUpdateTo(reg, updateButtonPressed);
Expand All @@ -68,6 +67,16 @@ function ServiceWorkerClient(url, debug, ui) {
//--------------------------------------------------------------------------------------------------------------
// Private methods
//--------------------------------------------------------------------------------------------------------------
function logRegistration(reg, message) {
message = message ? message : 'Service Worker registration';
const yes = '✓';
const no = '𐄂';
let installing = reg.installing ? yes : no;
let waiting = reg.waiting ? yes : no;
let active = reg.active ? yes : no;
message = `${message}\n - installing: ${installing}\n - waiting: ${waiting}\n - active: ${active}`;
debug.log(message);
}
function updateIsAvailable(reg) {
let newSw = (reg.installing || reg.waiting);
let activeSw = reg.active;
Expand Down
12 changes: 0 additions & 12 deletions js/sw-utils.js

This file was deleted.

18 changes: 14 additions & 4 deletions sw.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
const DEBUG_LOGGING = true;
const WORKBOX_DEBUG = false;

function logRegistration(reg, message) {
message = message ? message : 'Service Worker registration';
const yes = '✓';
const no = '𐄂';
let installing = reg.installing ? yes : no;
let waiting = reg.waiting ? yes : no;
let active = reg.active ? yes : no;
message = `${message}\n - installing: ${installing}\n - waiting: ${waiting}\n - active: ${active}`;
debug.log(message);
}

importScripts('js/debug-console.js');
importScripts('js/sw-utils.js');
const debug = new DebugConsole(DEBUG_LOGGING, 'Service Worker', 'indianred');
debug.heading('New Service Worker installing...');

SwUtils.logRegistration(self.registration, 'At startup: registration state', debug);
logRegistration(self.registration, 'At startup: registration state', debug);

const WORKBOX_VERSION = "5.1.2";
importScripts(`https://storage.googleapis.com/workbox-cdn/releases/${WORKBOX_VERSION}/workbox-sw.js`);
Expand All @@ -25,7 +35,7 @@ self.addEventListener('message', (event) => {
}
});
self.addEventListener('install', function (event) {
SwUtils.logRegistration(self.registration, 'On install: registration state', debug);
logRegistration(self.registration, 'On install: registration state', debug);
});
const FIRST_TIME = (!self.registration.active);
if (FIRST_TIME) {
Expand All @@ -45,7 +55,7 @@ self.clients.matchAll().then(function (clientArray) {
// If there are no controlled clients then a new service worker will activate automatically, even if there
// is a previous active version. And it will carry on doing this each time until a client becomes controlled.
self.addEventListener('activate', function(event) {
SwUtils.logRegistration(self.registration, 'On activate: registration state', debug);
logRegistration(self.registration, 'On activate: registration state', debug);
// Note. Our client page is initially uncontrolled by a service worker because it was not served by a the
// service worker. So how do we bring it under control in a way that does not take control away from the user?
// ---
Expand Down

0 comments on commit bf0c358

Please sign in to comment.