From 7e1fc789d647a4f8ee062482230e8acaa7ce9975 Mon Sep 17 00:00:00 2001 From: Richard Park Date: Sun, 9 Jul 2017 02:26:37 -0400 Subject: [PATCH] feat(service-worker): add sw-precache for offline --- package-lock.json | 146 ++++++++++++++++++++++++++++++++++++------ package.json | 37 ++++++++++- src/main.ts | 7 ++ sw-precache.config.js | 31 +++++++++ 4 files changed, 200 insertions(+), 21 deletions(-) create mode 100644 sw-precache.config.js diff --git a/package-lock.json b/package-lock.json index 3b107b8..22f7556 100644 --- a/package-lock.json +++ b/package-lock.json @@ -513,8 +513,7 @@ "version": "1.0.2", "resolved": "https://registry.npmjs.org/array-find-index/-/array-find-index-1.0.2.tgz", "integrity": "sha1-3wEKoSh+Fku9pvlyOwqWoexBh6E=", - "dev": true, - "optional": true + "dev": true }, "array-flatten": { "version": "1.1.1", @@ -1028,8 +1027,7 @@ "version": "2.1.0", "resolved": "https://registry.npmjs.org/camelcase-keys/-/camelcase-keys-2.1.0.tgz", "integrity": "sha1-MIvur/3ygRkFHvodkyITyRuPkuc=", - "dev": true, - "optional": true + "dev": true }, "caniuse-api": { "version": "1.6.1", @@ -1709,8 +1707,7 @@ "version": "0.4.1", "resolved": "https://registry.npmjs.org/currently-unhandled/-/currently-unhandled-0.4.1.tgz", "integrity": "sha1-mI3zP+qxke95mmE2nddsF635V+o=", - "dev": true, - "optional": true + "dev": true }, "custom-event": { "version": "1.0.1", @@ -1926,6 +1923,12 @@ } } }, + "dom-urls": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/dom-urls/-/dom-urls-1.1.0.tgz", + "integrity": "sha1-AB3fgWKM0ecGElxxdvU8zsVdkY4=", + "dev": true + }, "domain-browser": { "version": "1.1.7", "resolved": "https://registry.npmjs.org/domain-browser/-/domain-browser-1.1.7.tgz", @@ -2138,6 +2141,12 @@ "integrity": "sha1-jjGcnwRTv1ddN0lAplWSDlnKVRI=", "dev": true }, + "es6-promise": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/es6-promise/-/es6-promise-4.1.1.tgz", + "integrity": "sha512-OaU1hHjgJf+b0NzsxCg7NdIYERD6Hy/PEmFLTjw+b65scuisG3Kt4QoTvJ66BBkPZ581gr0kpoVzKnxniM8nng==", + "dev": true + }, "es6-set": { "version": "0.1.5", "resolved": "https://registry.npmjs.org/es6-set/-/es6-set-0.1.5.tgz", @@ -4975,8 +4984,7 @@ "version": "2.1.0", "resolved": "https://registry.npmjs.org/indent-string/-/indent-string-2.1.0.tgz", "integrity": "sha1-ji1INIdCEhtKghi3oTfppSBJ3IA=", - "dev": true, - "optional": true + "dev": true }, "indexes-of": { "version": "1.0.1", @@ -5823,6 +5831,12 @@ "integrity": "sha1-fAt/admKH3ZSn4kLDNsbTf7BHBE=", "dev": true }, + "lodash._reinterpolate": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/lodash._reinterpolate/-/lodash._reinterpolate-3.0.0.tgz", + "integrity": "sha1-DM8tiRZq8Ds2Y8eWU4t1rG4RTZ0=", + "dev": true + }, "lodash._shimkeys": { "version": "2.4.1", "resolved": "https://registry.npmjs.org/lodash._shimkeys/-/lodash._shimkeys-2.4.1.tgz", @@ -5848,6 +5862,12 @@ "integrity": "sha1-4j8/nE+Pvd6HJSnBBxhXoIblzO8=", "dev": true }, + "lodash.defaults": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/lodash.defaults/-/lodash.defaults-4.2.0.tgz", + "integrity": "sha1-0JF4cW/+pN3p5ft7N/bwgCJ0WAw=", + "dev": true + }, "lodash.isarguments": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/lodash.isarguments/-/lodash.isarguments-3.1.0.tgz", @@ -5891,6 +5911,18 @@ "integrity": "sha1-0jM6NtnncXyK0vfKyv7HwytERmQ=", "dev": true }, + "lodash.template": { + "version": "4.4.0", + "resolved": "https://registry.npmjs.org/lodash.template/-/lodash.template-4.4.0.tgz", + "integrity": "sha1-5zoDhcg1VZF0bgILmWecaQ5o+6A=", + "dev": true + }, + "lodash.templatesettings": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/lodash.templatesettings/-/lodash.templatesettings-4.1.0.tgz", + "integrity": "sha1-K01OlbpEDZFf8IvImeRVNmZxMxY=", + "dev": true + }, "lodash.uniq": { "version": "4.5.0", "resolved": "https://registry.npmjs.org/lodash.uniq/-/lodash.uniq-4.5.0.tgz", @@ -5965,8 +5997,7 @@ "version": "1.6.0", "resolved": "https://registry.npmjs.org/loud-rejection/-/loud-rejection-1.6.0.tgz", "integrity": "sha1-W0b4AUft7leIcPCG0Eghz5mOVR8=", - "dev": true, - "optional": true + "dev": true }, "lower-case": { "version": "1.1.4", @@ -6052,14 +6083,12 @@ "resolved": "https://registry.npmjs.org/meow/-/meow-3.7.0.tgz", "integrity": "sha1-cstmi0JSKCkKu/qFaJJYcwioAfs=", "dev": true, - "optional": true, "dependencies": { "minimist": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=", - "dev": true, - "optional": true + "dev": true } } }, @@ -7129,6 +7158,12 @@ "integrity": "sha1-gV7R9uvGWSb4ZbMQwHE7yzMVzks=", "dev": true }, + "pretty-bytes": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/pretty-bytes/-/pretty-bytes-4.0.2.tgz", + "integrity": "sha1-sr+C5zUNZcbDOqlaqlpPYyf2HNk=", + "dev": true + }, "pretty-error": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/pretty-error/-/pretty-error-2.1.1.tgz", @@ -7516,8 +7551,7 @@ "version": "1.0.0", "resolved": "https://registry.npmjs.org/redent/-/redent-1.0.0.tgz", "integrity": "sha1-z5Fqsf1fHxbfsggi3W7H9zDCr94=", - "dev": true, - "optional": true + "dev": true }, "reduce-css-calc": { "version": "1.3.0", @@ -7889,6 +7923,12 @@ "integrity": "sha1-n0uhni8wMMVH+K+ZEHg47DjVseI=", "dev": true }, + "serviceworker-cache-polyfill": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/serviceworker-cache-polyfill/-/serviceworker-cache-polyfill-4.0.0.tgz", + "integrity": "sha1-3hnuc77yGrPAdAo3sz22JGS6ves=", + "dev": true + }, "set-blocking": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/set-blocking/-/set-blocking-2.0.0.tgz", @@ -8313,8 +8353,7 @@ "version": "1.0.1", "resolved": "https://registry.npmjs.org/strip-indent/-/strip-indent-1.0.1.tgz", "integrity": "sha1-DHlipq3vp7vUrDZkYKY4VSrhoKI=", - "dev": true, - "optional": true + "dev": true }, "strip-json-comments": { "version": "2.0.1", @@ -8466,6 +8505,70 @@ "integrity": "sha1-n1dyQTlSE1xv779Ar+ak+qiLS7U=", "dev": true }, + "sw-precache": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/sw-precache/-/sw-precache-5.2.0.tgz", + "integrity": "sha512-sKctdX+5hUxkqJ/1DM88ubQ+QRvyw7CnxWdk909N2DgvxMqc1gcQFrwL7zpVc87wFmCA/OvRQd0iMC2XdFopYg==", + "dev": true, + "dependencies": { + "configstore": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/configstore/-/configstore-2.1.0.tgz", + "integrity": "sha1-c3o6cDbpiGECqmCZ5HuzOrGroaE=", + "dev": true + }, + "dot-prop": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/dot-prop/-/dot-prop-3.0.0.tgz", + "integrity": "sha1-G3CK8JSknJoOfbyteQq6U52sEXc=", + "dev": true + }, + "update-notifier": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/update-notifier/-/update-notifier-1.0.3.tgz", + "integrity": "sha1-j5LFFUgr1oMbfJMBPnD4dVLHz1o=", + "dev": true + }, + "uuid": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-2.0.3.tgz", + "integrity": "sha1-Z+LoY3lyFVMN/zGOW/nc6/1Hsho=", + "dev": true + }, + "write-file-atomic": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/write-file-atomic/-/write-file-atomic-1.3.4.tgz", + "integrity": "sha1-+Aek8LHZ6ROuekgRLmzDrxmRtF8=", + "dev": true + }, + "xdg-basedir": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/xdg-basedir/-/xdg-basedir-2.0.0.tgz", + "integrity": "sha1-7byQPMOF/ARSPZZqM1UEtVBNG9I=", + "dev": true + } + } + }, + "sw-toolbox": { + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/sw-toolbox/-/sw-toolbox-3.6.0.tgz", + "integrity": "sha1-Jt8dHHA0hljk3qKIQxkUm3sxg7U=", + "dev": true, + "dependencies": { + "isarray": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz", + "integrity": "sha1-ihis/Kmo9Bd+Cav8YDiTmwXR7t8=", + "dev": true + }, + "path-to-regexp": { + "version": "1.7.0", + "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-1.7.0.tgz", + "integrity": "sha1-Wf3g9DW62suhA6hOnTvGTpa5k30=", + "dev": true + } + } + }, "symbol-observable": { "version": "1.0.4", "resolved": "https://registry.npmjs.org/symbol-observable/-/symbol-observable-1.0.4.tgz", @@ -8582,8 +8685,7 @@ "version": "1.0.0", "resolved": "https://registry.npmjs.org/trim-newlines/-/trim-newlines-1.0.0.tgz", "integrity": "sha1-WIeWa7WCpFA6QetST301ARgVphM=", - "dev": true, - "optional": true + "dev": true }, "trim-right": { "version": "1.0.1", @@ -8854,6 +8956,12 @@ "integrity": "sha1-9rRQHC7EzdJrp4vnIilh3ndiFZg=", "dev": true }, + "urijs": { + "version": "1.18.10", + "resolved": "https://registry.npmjs.org/urijs/-/urijs-1.18.10.tgz", + "integrity": "sha1-uURj6rpZoaeWA2pGe7YzxmfyIas=", + "dev": true + }, "url": { "version": "0.11.0", "resolved": "https://registry.npmjs.org/url/-/url-0.11.0.tgz", diff --git a/package.json b/package.json index 7e5b619..5426533 100644 --- a/package.json +++ b/package.json @@ -13,12 +13,18 @@ "node": ">=8.1", "npm": ">= 5" }, + "browserslist": [ + "last 2 versions", + "not ie <= 10", + "not ie_mob <= 10" + ], "scripts": { "build": "ng build --prod", "e2e": "ng e2e", "lint": "ng lint", "ng": "ng", - "prebuild": "ng lint", + "postbuild": "npm run precache", + "precache": "sw-precache --config=sw-precache.config.js --verbose", "start": "ng serve", "test": "ng test" }, @@ -57,8 +63,35 @@ "karma-jasmine-html-reporter": "^0.2.2", "minx": "r-park/minx.git", "protractor": "^5.1.2", + "sw-precache": "^5.2.0", "ts-node": "^3.2.0", "tslint": "^5.5.0", "typescript": "^2.4.1" - } + }, + "author": { + "name": "Richard Park", + "email": "objectiv@gmail.com" + }, + "contributors": [ + { + "name": "Cody Lundquist", + "email": "cody.lundquist@gmail.com" + }, + { + "name": "Jason Shultz", + "email": "" + }, + { + "name": "Luke Schlangen", + "email": "luke@lukeschlangen.com" + }, + { + "name": "Sinan Bolel", + "email": "sinanbolel@gmail.com" + }, + { + "name": "Yannick Koehler", + "email": "yannick@koehler.name" + } + ] } diff --git a/src/main.ts b/src/main.ts index 52df6f1..081c25c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -11,4 +11,11 @@ if (environment.production) { platformBrowserDynamic().bootstrapModule(AppModule) + .then(() => { + if (environment.production && 'serviceWorker' in navigator) { + navigator.serviceWorker + .register('/service-worker.js') + .then(registration => console.log('[Service Worker] registered with scope', registration.scope)); + } + }) .catch(error => console.error(error)); diff --git a/sw-precache.config.js b/sw-precache.config.js new file mode 100644 index 0000000..3619cfd --- /dev/null +++ b/sw-precache.config.js @@ -0,0 +1,31 @@ +module.exports = { + navigateFallback: '/index.html', + + // @see https://github.com/Polymer/polymer-build/issues/35 + navigateFallbackWhitelist: [ + /^(?!\/__)/ + ], + + root: 'dist', + runtimeCaching: [ + { + urlPattern: /^https:\/\/fonts\.googleapis\.com\//, + handler: 'cacheFirst' + }, + { + urlPattern: /^https:\/\/maxcdn\.bootstrapcdn\.com\//, + handler: 'cacheFirst' + }, + { + urlPattern: /^https:\/\/use\.typekit\.net\//, + handler: 'cacheFirst' + } + ], + staticFileGlobs: [ + 'dist/assets/**/*', + 'dist/*.css', + 'dist/*.html', + 'dist/*.js' + ], + stripPrefix: 'dist/' +};