From 40d08ddef817c37389a16eb18cc66fcce22a251c Mon Sep 17 00:00:00 2001 From: Jeff Avallone Date: Sat, 10 Feb 2018 16:08:42 -0500 Subject: [PATCH] Adding service worker cache --- package.json | 3 ++- src/pages/index/index.js | 1 + src/service-worker.js | 11 ++++++++++ webpack.prod.js | 7 ++++++- yarn.lock | 43 ++++++++++++++++++++++++++++++++++++++-- 5 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 src/service-worker.js diff --git a/package.json b/package.json index 1d19bba..8f3b1d3 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,8 @@ "uglifyjs-webpack-plugin": "^1.1.8", "webpack": "^3.10.0", "webpack-dev-server": "^2.11.1", - "webpack-merge": "^4.1.1" + "webpack-merge": "^4.1.1", + "workbox-webpack-plugin": "^2.1.2" }, "devDependencies": { "http-server": "^0.11.1", diff --git a/src/pages/index/index.js b/src/pages/index/index.js index 999e117..b9978e8 100644 --- a/src/pages/index/index.js +++ b/src/pages/index/index.js @@ -1 +1,2 @@ import '../../style.css'; +import '../../service-worker'; diff --git a/src/service-worker.js b/src/service-worker.js new file mode 100644 index 0000000..58d1555 --- /dev/null +++ b/src/service-worker.js @@ -0,0 +1,11 @@ +if ('serviceWorker' in navigator && process.env.NODE_ENV === 'production') { + window.addEventListener('load', () => { + navigator.serviceWorker.register('/sw.js') + .then(registration => { + console.log('SW registered:', registration); + }) + .catch(registrationError => { + console.log('SW registration failed:', registrationError); + }); + }); +} diff --git a/webpack.prod.js b/webpack.prod.js index 32b83f3..afd5449 100644 --- a/webpack.prod.js +++ b/webpack.prod.js @@ -1,10 +1,15 @@ const merge = require('webpack-merge'); const common = require('./webpack.common.js'); const UglifyJSPlugin = require('uglifyjs-webpack-plugin'); +const WorkboxPlugin = require('workbox-webpack-plugin'); module.exports = merge(common, { devtool: 'source-map', plugins: [ - new UglifyJSPlugin() + new UglifyJSPlugin(), + new WorkboxPlugin({ + clientsClaim: true, + skipWaiting: true + }) ] }); diff --git a/yarn.lock b/yarn.lock index 7a04d6c..a81fff4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2019,6 +2019,14 @@ fs-extra@^1.0.0: jsonfile "^2.1.0" klaw "^1.0.0" +fs-extra@^3.0.1: + version "3.0.1" + resolved "http://nexus.home.bromidic.net:8081/repository/npm-all/fs-extra/-/fs-extra-3.0.1.tgz#3794f378c58b342ea7dbbb23095109c4b3b62291" + dependencies: + graceful-fs "^4.1.2" + jsonfile "^3.0.0" + universalify "^0.1.0" + fs-write-stream-atomic@^1.0.8: version "1.0.10" resolved "http://nexus.home.bromidic.net:8081/repository/npm-all/fs-write-stream-atomic/-/fs-write-stream-atomic-1.0.10.tgz#b47df53493ef911df75731e70a9ded0189db40c9" @@ -2122,7 +2130,7 @@ glob-parent@^3.1.0: is-glob "^3.1.0" path-dirname "^1.0.0" -glob@^7.0.3, glob@^7.0.5, glob@^7.1.2: +glob@^7.0.3, glob@^7.0.5, glob@^7.1.1, glob@^7.1.2: version "7.1.2" resolved "http://nexus.home.bromidic.net:8081/repository/npm-all/glob/-/glob-7.1.2.tgz#c19c9df9a028702d678612384a6552404c636d15" dependencies: @@ -2912,6 +2920,12 @@ jsonfile@^2.1.0: optionalDependencies: graceful-fs "^4.1.6" +jsonfile@^3.0.0: + version "3.0.1" + resolved "http://nexus.home.bromidic.net:8081/repository/npm-all/jsonfile/-/jsonfile-3.0.1.tgz#a5ecc6f65f53f662c4415c7675a0331d0992ec66" + optionalDependencies: + graceful-fs "^4.1.6" + jsonify@~0.0.0: version "0.0.0" resolved "http://nexus.home.bromidic.net:8081/repository/npm-all/jsonify/-/jsonify-0.0.0.tgz#2c74b6ee41d93ca51b7b5aaee8f503631d252a73" @@ -3059,7 +3073,7 @@ lodash.memoize@^4.1.2: version "4.1.2" resolved "http://nexus.home.bromidic.net:8081/repository/npm-all/lodash.memoize/-/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe" -lodash.template@^4.2.4: +lodash.template@^4.2.4, lodash.template@^4.4.0: version "4.4.0" resolved "http://nexus.home.bromidic.net:8081/repository/npm-all/lodash.template/-/lodash.template-4.4.0.tgz#e73a0385c8355591746e020b99679c690e68fba0" dependencies: @@ -5768,6 +5782,10 @@ unique-slug@^2.0.0: dependencies: imurmurhash "^0.1.4" +universalify@^0.1.0: + version "0.1.1" + resolved "http://nexus.home.bromidic.net:8081/repository/npm-all/universalify/-/universalify-0.1.1.tgz#fa71badd4437af4c148841e3b3b165f9e9e590b7" + unpipe@1.0.0, unpipe@~1.0.0: version "1.0.0" resolved "http://nexus.home.bromidic.net:8081/repository/npm-all/unpipe/-/unpipe-1.0.0.tgz#b2bf4ee8514aae6165b4817829d21b2ef49904ec" @@ -6078,6 +6096,27 @@ wordwrap@~0.0.2: version "0.0.3" resolved "http://nexus.home.bromidic.net:8081/repository/npm-all/wordwrap/-/wordwrap-0.0.3.tgz#a3d5da6cd5c0bc0008d37234bbaf1bed63059107" +workbox-build@^2.1.2: + version "2.1.2" + resolved "http://nexus.home.bromidic.net:8081/repository/npm-all/workbox-build/-/workbox-build-2.1.2.tgz#5425094a83ac77c991b6060dd1df3d37984ef87e" + dependencies: + chalk "^1.1.3" + fs-extra "^3.0.1" + glob "^7.1.1" + lodash.template "^4.4.0" + mkdirp "^0.5.1" + workbox-sw "^2.1.2" + +workbox-sw@^2.1.2: + version "2.1.2" + resolved "http://nexus.home.bromidic.net:8081/repository/npm-all/workbox-sw/-/workbox-sw-2.1.2.tgz#8f3a687c36f3b92ac2a66a08183bd3a163474b61" + +workbox-webpack-plugin@^2.1.2: + version "2.1.2" + resolved "http://nexus.home.bromidic.net:8081/repository/npm-all/workbox-webpack-plugin/-/workbox-webpack-plugin-2.1.2.tgz#e9d8d5cb70d5b32d5e86e92e3d099ac54c95914a" + dependencies: + workbox-build "^2.1.2" + worker-farm@^1.5.2: version "1.5.2" resolved "http://nexus.home.bromidic.net:8081/repository/npm-all/worker-farm/-/worker-farm-1.5.2.tgz#32b312e5dc3d5d45d79ef44acc2587491cd729ae"