Skip to content

Commit f33eee3

Browse files
committed
Add support for Service Worker
* Add Service Worker registration * Add sw-precache support to npm run scripts * Add SW runtime caching and pre-cache config * gitignore: ignore sw-toolbox and generated sw script * Include empty SW script for local testing * Improve Service Worker registration script * Drop explicit registration update.
1 parent 17443dc commit f33eee3

File tree

6 files changed

+69
-4
lines changed

6 files changed

+69
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/public/build
22
/node_modules
3+
/public/sw-toolbox.js

package.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
"url": "http://github.com/insin/react-hn.git"
1010
},
1111
"scripts": {
12-
"build": "npm run lint && nwb build",
13-
"lint": "eslint .",
12+
"build": "npm run lint && cp node_modules/sw-toolbox/sw-toolbox.js public/sw-toolbox.js && nwb build && npm run precache",
13+
"lint": "eslint src",
1414
"lint:fix": "eslint --fix .",
15-
"start": "nwb serve"
15+
"start": "nwb serve",
16+
"precache": "sw-precache --root=public --config=sw-precache-config.json"
1617
},
1718
"dependencies": {
1819
"events": "1.1.0",
@@ -28,6 +29,8 @@
2829
},
2930
"devDependencies": {
3031
"eslint-config-jonnybuchanan": "2.0.3",
31-
"nwb": "0.8.1"
32+
"nwb": "0.8.1",
33+
"sw-precache": "^3.1.1",
34+
"sw-toolbox": "^3.1.1"
3235
}
3336
}

public/index.html

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,35 @@
3636
<div id="app"></div>
3737
<script src="build/vendor.js"></script>
3838
<script src="build/app.js"></script>
39+
<script>
40+
if ('serviceWorker' in navigator) {
41+
navigator.serviceWorker.register('./service-worker.js', {
42+
scope: './'
43+
})
44+
.then(function(registration) {
45+
registration.onupdatefound = function() {
46+
if (navigator.serviceWorker.controller) {
47+
var installingWorker = registration.installing;
48+
49+
installingWorker.onstatechange = function() {
50+
switch (installingWorker.state) {
51+
case 'installed':
52+
break;
53+
case 'redundant':
54+
throw new Error('The installing ' +
55+
'service worker became redundant.');
56+
default:
57+
// Ignore
58+
}
59+
};
60+
}
61+
};
62+
}).catch(function(e) {
63+
console.error('Error during service worker registration:', e);
64+
});
65+
} else {
66+
console.log('service worker is not supported');
67+
}
68+
</script>
3969
</body>
4070
</html>

public/runtime-caching.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// global.toolbox is defined in a different script, sw-toolbox.js, which is part of the
2+
// https://github.com/GoogleChrome/sw-toolbox project.
3+
// That sw-toolbox.js script must be executed first, so it needs to be listed before this in the
4+
// importScripts() call that the parent service worker makes.
5+
(function(global) {
6+
'use strict'
7+
8+
// See https://github.com/GoogleChrome/sw-toolbox/blob/6e8242dc328d1f1cfba624269653724b26fa94f1/README.md#toolboxroutergeturlpattern-handler-options
9+
// and https://github.com/GoogleChrome/sw-toolbox/blob/6e8242dc328d1f1cfba624269653724b26fa94f1/README.md#toolboxfastest
10+
// for more details on how this handler is defined and what the toolbox.fastest strategy does.
11+
global.toolbox.router.get('/(.*)', global.toolbox.fastest, {
12+
origin: /\.(?:googleapis|gstatic|firebaseio)\.com$/
13+
})
14+
global.toolbox.router.get('/(.+)', global.toolbox.fastest, {
15+
origin: 'https://hacker-news.firebaseio.com'
16+
})
17+
global.toolbox.router.get('/(.+)', global.toolbox.fastest, {
18+
origin: 'https://s-usc1c-nss-136.firebaseio.com'
19+
})
20+
})(self)
21+

public/service-worker.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// This file is intentionally without code.
2+
// It's present so that service worker registration will work when serving from the 'public' directory.

sw-precache-config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"importScripts": [
3+
"sw-toolbox.js",
4+
"runtime-caching.js"
5+
],
6+
"stripPrefix": "public/",
7+
"verbose": true
8+
}

0 commit comments

Comments
 (0)