Skip to content

Commit 507aa6b

Browse files
authored
Land Workbox support. (#82)
* package.json: [sw-precache, sw-toolbox] => sw-cli * precache config, runtime => sw-cli-config, sw-lib * Add service-worker and runtime caching rules * Move over to using sw-build * Moves over to the Workbox libraries. (#81)
1 parent 0040242 commit 507aa6b

File tree

6 files changed

+31
-44
lines changed

6 files changed

+31
-44
lines changed

generate-sw.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const workboxBuild = require('workbox-build');
2+
3+
workboxBuild.injectManifest({
4+
swSrc: 'public/service-worker.js',
5+
swDest: 'dist/service-worker.js',
6+
globDirectory: 'dist',
7+
staticFileGlobs: ['**/!(*map*)'],
8+
globIgnores: ['**/service-worker.js'],
9+
}).then(() => {
10+
console.log('The production service worker has been generated.');
11+
});

package.json

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@
99
"url": "http://github.com/insin/react-hn.git"
1010
},
1111
"scripts": {
12-
"build": "mkdir -p dist && npm run lint && cp node_modules/sw-toolbox/sw-toolbox.js dist/sw-toolbox.js && ./node_modules/.bin/nwb build && npm run copy-manifest && npm run precache",
12+
"build": "mkdir -p dist && nwb build && npm run copy-manifest && npm run clean && npm run generate-sw",
1313
"deploy": "gcloud preview app deploy",
14-
"lint": "./node_modules/eslint-config-jonnybuchanan/bin/lint.js src",
14+
"lint-old": "./node_modules/eslint-config-jonnybuchanan/bin/lint.js src",
1515
"lint:fix": "./node_modules/eslint-config-jonnybuchanan/bin/lint.js --fix .",
1616
"start": "node server.js",
1717
"postinstall": "npm run build",
18-
"serve": "./node_modules/.bin/nwb serve",
18+
"serve": "nwb serve",
1919
"copy-manifest": "cp public/manifest.json dist/manifest.json",
20-
"clean": "rm -rf *.json.gzip dist/index.html",
21-
"precache": "./node_modules/sw-precache/cli.js --root=public --config=sw-precache-config.json && npm run clean"
20+
"generate-sw": "node generate-sw.js",
21+
"clean": "rm -rf *.json.gzip dist/index.html"
2222
},
2323
"engines": {
2424
"node": "6.1.0"
@@ -46,12 +46,11 @@
4646
"reactfire": "1.0.0",
4747
"scroll-behavior": "0.8.1",
4848
"setimmediate": "1.0.5",
49-
"sw-precache": "^4.1.0",
50-
"sw-toolbox": "^3.1.1",
5149
"url-parse": "^1.1.1"
5250
},
5351
"devDependencies": {
54-
"html-webpack-plugin": "^2.22.0",
55-
"webpack": "^1.13.2"
52+
"html-webpack-plugin": "2.22.0",
53+
"webpack": "1.13.2",
54+
"workbox-build": "0.0.2"
5655
}
5756
}

public/index-static.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@
3636
<div id="app"></div>
3737
<script>
3838
if ('serviceWorker' in navigator) {
39-
navigator.serviceWorker.register('./service-worker.js', {
40-
scope: './'
41-
})
39+
navigator.serviceWorker.register('/service-worker.js')
4240
.then(function(registration) {
4341
registration.onupdatefound = function() {
4442
if (navigator.serviceWorker.controller) {

public/runtime-caching.js

Lines changed: 0 additions & 22 deletions
This file was deleted.

public/service-worker.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
1-
// This file is intentionally without code.
2-
// It's present so that service worker registration will work when serving from the 'public' directory.
1+
importScripts('https://unpkg.com/[email protected]');
2+
3+
const workboxSW = new WorkboxSW({clientsClaim: true});
4+
5+
// This array will be populated by workboxBuild.injectManifest() when the
6+
// production service worker is generated.
7+
workboxSW.precache([]);
8+
9+
workboxSW.router.setDefaultHandler({
10+
handler: workboxSW.strategies.staleWhileRevalidate()
11+
});

sw-precache-config.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)