From 752b767012e1de1e422b5c992d2fd3df4f2d61bf Mon Sep 17 00:00:00 2001 From: quixotically Date: Sun, 4 Jun 2017 15:47:15 -0400 Subject: [PATCH] WIP OfflinePlugin - no errors in online mode, but does not seem to work offline on localhost --- package.json | 1 + src/application.js | 20 ++++++++++++++++++++ webpack.config.js | 14 ++++++++++++++ yarn.lock | 16 +++++++++++++++- 4 files changed, 50 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 89184c381c..4ad45212dd 100644 --- a/package.json +++ b/package.json @@ -98,6 +98,7 @@ "lodash": "^4.15.0", "loop-breaker": "^0.1.0-alpha.7", "moment": "^2.14.1", + "offline-plugin": "^4.8.1", "pify": "^2.3.0", "promise-retry": "^1.1.1", "prop-types": "^15.5.10", diff --git a/src/application.js b/src/application.js index a53e567d41..5dcc83f508 100644 --- a/src/application.js +++ b/src/application.js @@ -6,9 +6,29 @@ import Immutable from 'immutable'; import installDevTools from 'immutable-devtools'; import Application from './components/Application'; import initI18n from './util/initI18n'; +import * as OfflinePluginRuntime from 'offline-plugin/runtime'; import {init as initAnalytics, logPageview} from './clients/googleAnalytics'; installDevTools(Immutable); +OfflinePluginRuntime.install({ + onUpdating: () => { + console.log('SW Event:', 'onUpdating'); + }, + onUpdateReady: () => { + console.log('SW Event:', 'onUpdateReady'); + // Tells to new SW to take control immediately + runtime.applyUpdate(); + }, + onUpdated: () => { + console.log('SW Event:', 'onUpdated'); + // Reload the webpage to load into the new version + window.location.reload(); + }, + + onUpdateFailed: () => { + console.log('SW Event:', 'onUpdateFailed'); + } +}); initI18n(); initAnalytics(); diff --git a/webpack.config.js b/webpack.config.js index ed7e6df00a..d4873cb32e 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -3,6 +3,7 @@ /* eslint-disable import/no-commonjs */ /* eslint-disable comma-dangle */ +const OfflinePlugin = require('offline-plugin'); const path = require('path'); const webpack = require('webpack'); const escapeRegExp = require('lodash/escapeRegExp'); @@ -192,6 +193,19 @@ module.exports = { WARN_ON_DROPPED_ERRORS: 'false', GOOGLE_ANALYTICS_TRACKING_ID: 'UA-90316486-2' }), + // it's always better if OfflinePlugin is the last plugin added https://github.com/NekR/offline-plugin#setup + new OfflinePlugin({ + publicPath: '/compiled/', + relativePaths: false, + responseStrategy: 'network-first', + externals: [ + '/', + '/compiled/application.css', + ], + ServiceWorker: { + navigateFallbackURL: '/', + }, + }), ], resolve: { alias: { diff --git a/yarn.lock b/yarn.lock index f159cd9610..838788b0ac 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2187,7 +2187,7 @@ deep-equal@~1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/deep-equal/-/deep-equal-1.0.1.tgz#f5d260292b660e084eff4cdbc9f08ad3247448b5" -deep-extend@~0.4.0: +deep-extend@^0.4.0, deep-extend@~0.4.0: version "0.4.2" resolved "https://registry.yarnpkg.com/deep-extend/-/deep-extend-0.4.2.tgz#48b699c27e334bf89f10892be432f6e4c7d34a7f" @@ -2460,6 +2460,10 @@ ee-first@1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d" +ejs@^2.3.4: + version "2.5.6" + resolved "https://registry.yarnpkg.com/ejs/-/ejs-2.5.6.tgz#479636bfa3fe3b1debd52087f0acb204b4f19c88" + electron-to-chromium@^1.2.7: version "1.3.11" resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.11.tgz#744761df1d67b492b322ce9aa0aba5393260eb61" @@ -5487,6 +5491,16 @@ obuf@^1.0.0, obuf@^1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/obuf/-/obuf-1.1.1.tgz#104124b6c602c6796881a042541d36db43a5264e" +offline-plugin@^4.8.1: + version "4.8.1" + resolved "https://registry.yarnpkg.com/offline-plugin/-/offline-plugin-4.8.1.tgz#1d73457081185777179c29d9d416f71077a0197a" + dependencies: + deep-extend "^0.4.0" + ejs "^2.3.4" + loader-utils "0.2.x" + minimatch "^3.0.3" + slash "^1.0.0" + on-finished@~2.3.0: version "2.3.0" resolved "https://registry.yarnpkg.com/on-finished/-/on-finished-2.3.0.tgz#20f1336481b083cd75337992a16971aa2d906947"