From 39acf1f6f70c48e02589f52d7bbe9466a924bf3d Mon Sep 17 00:00:00 2001 From: Arthur Stolyar Date: Sun, 8 Apr 2018 23:01:58 +0300 Subject: [PATCH] Add ServiceWorker.updateViaCache and ServiceWorker.forceInstall options --- lib/app-cache.js | 2 +- lib/default-options.js | 6 ++++ lib/index.js | 4 +-- lib/service-worker.js | 6 +++- src/default-options.js | 5 ++++ src/index.js | 8 +++-- src/service-worker.js | 6 +++- tests/legacy/compare.js | 2 +- .../__expected/webpack2/main.js | 18 ++++++----- .../__expected/webpack3/main.js | 18 ++++++----- .../__expected/webpack4/main.js | 18 ++++++----- .../__expected/webpack2/main.js | 18 ++++++----- .../__expected/webpack3/main.js | 18 ++++++----- .../__expected/webpack4/main.js | 18 ++++++----- .../__expected/webpack2/main.js | 18 ++++++----- .../__expected/webpack3/main.js | 18 ++++++----- .../__expected/webpack4/main.js | 18 ++++++----- .../webpack2/appcache/manifest.appcache | 2 +- .../webpack2/appcache/manifest.html | 2 +- .../__expected/webpack2/dist/main.js | 18 ++++++----- .../__expected/webpack2/sw.js | 4 +-- .../webpack3/appcache/manifest.appcache | 2 +- .../webpack3/appcache/manifest.html | 2 +- .../__expected/webpack3/dist/main.js | 18 ++++++----- .../__expected/webpack3/sw.js | 4 +-- .../webpack4/appcache/manifest.appcache | 2 +- .../__expected/webpack4/dist/main.js | 18 ++++++----- .../__expected/webpack4/sw.js | 4 +-- tpls/runtime-template.js | 30 +++++++++++++------ 29 files changed, 193 insertions(+), 114 deletions(-) diff --git a/lib/app-cache.js b/lib/app-cache.js index 9e3b8e5d..90240937 100644 --- a/lib/app-cache.js +++ b/lib/app-cache.js @@ -141,7 +141,7 @@ var AppCache = (function () { }, { key: 'getPageTemplate', value: function getPageTemplate(name, content) { - return ('\n \n ' + (content || '') + '\n ').trim().replace(/^ */gm, ''); + return ('\n \n ' + (content || '') + '\n ').trim().replace(/^ */gm, ''); } }, { key: 'getPageContent', diff --git a/lib/default-options.js b/lib/default-options.js index 357f4aff..241793f1 100644 --- a/lib/default-options.js +++ b/lib/default-options.js @@ -12,6 +12,9 @@ var _path2 = _interopRequireDefault(_path); var _miscUtils = require('./misc/utils'); +var DEFAULT_AUTO_UPDATE_INTERVAL = 3600000; + +exports.DEFAULT_AUTO_UPDATE_INTERVAL = DEFAULT_AUTO_UPDATE_INTERVAL; var AppCacheOptions = { NETWORK: '*', FALLBACK: null, @@ -53,6 +56,9 @@ exports['default'] = { scope: null, events: false, minify: null, + forceInstall: false, + + updateViaCache: 'imports', prefetchRequest: { credentials: 'same-origin', diff --git a/lib/index.js b/lib/index.js index 1efef856..132ad557 100644 --- a/lib/index.js +++ b/lib/index.js @@ -52,8 +52,6 @@ var _require = require('../package.json'); var pluginVersion = _require.version; -var AUTO_UPDATE_INTERVAL = 3600000; - var hasOwn = ({}).hasOwnProperty; var updateStrategies = ['all', 'hash', 'changed']; @@ -98,7 +96,7 @@ var OfflinePlugin = (function () { var autoUpdate = this.options.autoUpdate; if (autoUpdate === true) { - this.autoUpdate = AUTO_UPDATE_INTERVAL; + this.autoUpdate = _defaultOptions.DEFAULT_AUTO_UPDATE_INTERVAL; } else if (typeof autoUpdate === 'number' && autoUpdate) { this.autoUpdate = autoUpdate; } diff --git a/lib/service-worker.js b/lib/service-worker.js index 0a383f57..c947b2ae 100644 --- a/lib/service-worker.js +++ b/lib/service-worker.js @@ -51,7 +51,9 @@ var ServiceWorker = (function () { this.navigateFallbackURL = options.navigateFallbackURL; this.navigateFallbackForRedirects = options.navigateFallbackForRedirects; this.prefetchRequest = this.validatePrefetch(options.prefetchRequest); + this.updateViaCache = (options.updateViaCache || '') + ''; this.navigationPreload = options.navigationPreload; + this.forceInstall = !!options.forceInstall; var cacheNameQualifier = ''; @@ -251,7 +253,9 @@ var ServiceWorker = (function () { return { location: this.location, scope: this.scope, - events: this.events + updateViaCache: this.updateViaCache, + events: this.events, + force: this.forceInstall }; } }, { diff --git a/src/default-options.js b/src/default-options.js index f9038cff..11a43ae3 100644 --- a/src/default-options.js +++ b/src/default-options.js @@ -1,6 +1,8 @@ import path from 'path'; import { isAbsoluteURL } from './misc/utils'; +export const DEFAULT_AUTO_UPDATE_INTERVAL = 3600000; + export const AppCacheOptions = { NETWORK: '*', FALLBACK: null, @@ -41,6 +43,9 @@ export default { scope: null, events: false, minify: null, + forceInstall: false, + + updateViaCache: 'imports', prefetchRequest: { credentials: 'same-origin', diff --git a/src/index.js b/src/index.js index 8dcfca7f..df10d4f3 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,10 @@ import AppCache from './app-cache'; import ServiceWorker from './service-worker'; import defaultOptions, - { AppCacheOptions as defaultAppCacheOptions } from './default-options'; + { + AppCacheOptions as defaultAppCacheOptions, + DEFAULT_AUTO_UPDATE_INTERVAL + } from './default-options'; import { hasMagic, interpolateString, @@ -17,7 +20,6 @@ import loaderUtils from 'loader-utils'; import slash from 'slash'; const { version: pluginVersion } = require('../package.json'); -const AUTO_UPDATE_INTERVAL = 3600000; const hasOwn = {}.hasOwnProperty; const updateStrategies = ['all', 'hash', 'changed']; @@ -59,7 +61,7 @@ export default class OfflinePlugin { const autoUpdate = this.options.autoUpdate; if (autoUpdate === true) { - this.autoUpdate = AUTO_UPDATE_INTERVAL; + this.autoUpdate = DEFAULT_AUTO_UPDATE_INTERVAL; } else if (typeof autoUpdate === 'number' && autoUpdate) { this.autoUpdate = autoUpdate; } diff --git a/src/service-worker.js b/src/service-worker.js index 983a0ac5..7d189f36 100644 --- a/src/service-worker.js +++ b/src/service-worker.js @@ -31,7 +31,9 @@ export default class ServiceWorker { this.navigateFallbackURL = options.navigateFallbackURL; this.navigateFallbackForRedirects = options.navigateFallbackForRedirects; this.prefetchRequest = this.validatePrefetch(options.prefetchRequest); + this.updateViaCache = (options.updateViaCache || '') + ''; this.navigationPreload = options.navigationPreload; + this.forceInstall = !!options.forceInstall; let cacheNameQualifier = ''; @@ -234,7 +236,9 @@ export default class ServiceWorker { return { location: this.location, scope: this.scope, - events: this.events + updateViaCache: this.updateViaCache, + events: this.events, + force: this.forceInstall }; } diff --git a/tests/legacy/compare.js b/tests/legacy/compare.js index 4891a384..37caa65e 100644 --- a/tests/legacy/compare.js +++ b/tests/legacy/compare.js @@ -10,7 +10,7 @@ var log = console.log; var options = { compareContent: true, - excludeFilter: '.DS_Store' + excludeFilter: '.DS_Store,manifest.html' }; function logFile(file, state) { diff --git a/tests/legacy/fixtures/basic-with-custom-time-update-service/__expected/webpack2/main.js b/tests/legacy/fixtures/basic-with-custom-time-update-service/__expected/webpack2/main.js index 0e105438..a59bdd3b 100644 --- a/tests/legacy/fixtures/basic-with-custom-time-update-service/__expected/webpack2/main.js +++ b/tests/legacy/fixtures/basic-with-custom-time-update-service/__expected/webpack2/main.js @@ -73,11 +73,13 @@ var appCacheIframe; function hasSW() { - return 'serviceWorker' in navigator && - // This is how I block Chrome 40 and detect Chrome 41, because first has - // bugs with history.pustState and/or hashchange - (window.fetch || 'imageRendering' in document.documentElement.style) && - (window.location.protocol === 'https:' || window.location.hostname === 'localhost' || window.location.hostname.indexOf('127.') === 0) + + return 'serviceWorker' in navigator && ( + window.location.protocol === 'https:' || + window.location.hostname === 'localhost' || + window.location.hostname.indexOf('127.') === 0 + ); + } function install(options) { @@ -87,8 +89,10 @@ function install(options) { if (hasSW()) { var registration = navigator.serviceWorker .register( - "sw.js" - + "sw.js", { + + + } ); diff --git a/tests/legacy/fixtures/basic-with-custom-time-update-service/__expected/webpack3/main.js b/tests/legacy/fixtures/basic-with-custom-time-update-service/__expected/webpack3/main.js index 179b2477..4d60302b 100644 --- a/tests/legacy/fixtures/basic-with-custom-time-update-service/__expected/webpack3/main.js +++ b/tests/legacy/fixtures/basic-with-custom-time-update-service/__expected/webpack3/main.js @@ -76,11 +76,13 @@ __webpack_require__(1); var appCacheIframe; function hasSW() { - return 'serviceWorker' in navigator && - // This is how I block Chrome 40 and detect Chrome 41, because first has - // bugs with history.pustState and/or hashchange - (window.fetch || 'imageRendering' in document.documentElement.style) && - (window.location.protocol === 'https:' || window.location.hostname === 'localhost' || window.location.hostname.indexOf('127.') === 0) + + return 'serviceWorker' in navigator && ( + window.location.protocol === 'https:' || + window.location.hostname === 'localhost' || + window.location.hostname.indexOf('127.') === 0 + ); + } function install(options) { @@ -90,8 +92,10 @@ function install(options) { if (hasSW()) { var registration = navigator.serviceWorker .register( - "sw.js" - + "sw.js", { + + + } ); diff --git a/tests/legacy/fixtures/basic-with-custom-time-update-service/__expected/webpack4/main.js b/tests/legacy/fixtures/basic-with-custom-time-update-service/__expected/webpack4/main.js index 7022281f..e78e479b 100644 --- a/tests/legacy/fixtures/basic-with-custom-time-update-service/__expected/webpack4/main.js +++ b/tests/legacy/fixtures/basic-with-custom-time-update-service/__expected/webpack4/main.js @@ -82,11 +82,13 @@ __webpack_require__(1); var appCacheIframe; function hasSW() { - return 'serviceWorker' in navigator && - // This is how I block Chrome 40 and detect Chrome 41, because first has - // bugs with history.pustState and/or hashchange - (window.fetch || 'imageRendering' in document.documentElement.style) && - (window.location.protocol === 'https:' || window.location.hostname === 'localhost' || window.location.hostname.indexOf('127.') === 0) + + return 'serviceWorker' in navigator && ( + window.location.protocol === 'https:' || + window.location.hostname === 'localhost' || + window.location.hostname.indexOf('127.') === 0 + ); + } function install(options) { @@ -96,8 +98,10 @@ function install(options) { if (hasSW()) { var registration = navigator.serviceWorker .register( - "sw.js" - + "sw.js", { + + + } ); diff --git a/tests/legacy/fixtures/basic-with-default-update-service/__expected/webpack2/main.js b/tests/legacy/fixtures/basic-with-default-update-service/__expected/webpack2/main.js index e07df40d..6db7d390 100644 --- a/tests/legacy/fixtures/basic-with-default-update-service/__expected/webpack2/main.js +++ b/tests/legacy/fixtures/basic-with-default-update-service/__expected/webpack2/main.js @@ -73,11 +73,13 @@ var appCacheIframe; function hasSW() { - return 'serviceWorker' in navigator && - // This is how I block Chrome 40 and detect Chrome 41, because first has - // bugs with history.pustState and/or hashchange - (window.fetch || 'imageRendering' in document.documentElement.style) && - (window.location.protocol === 'https:' || window.location.hostname === 'localhost' || window.location.hostname.indexOf('127.') === 0) + + return 'serviceWorker' in navigator && ( + window.location.protocol === 'https:' || + window.location.hostname === 'localhost' || + window.location.hostname.indexOf('127.') === 0 + ); + } function install(options) { @@ -87,8 +89,10 @@ function install(options) { if (hasSW()) { var registration = navigator.serviceWorker .register( - "sw.js" - + "sw.js", { + + + } ); diff --git a/tests/legacy/fixtures/basic-with-default-update-service/__expected/webpack3/main.js b/tests/legacy/fixtures/basic-with-default-update-service/__expected/webpack3/main.js index 9de30861..99ac49e9 100644 --- a/tests/legacy/fixtures/basic-with-default-update-service/__expected/webpack3/main.js +++ b/tests/legacy/fixtures/basic-with-default-update-service/__expected/webpack3/main.js @@ -76,11 +76,13 @@ __webpack_require__(1); var appCacheIframe; function hasSW() { - return 'serviceWorker' in navigator && - // This is how I block Chrome 40 and detect Chrome 41, because first has - // bugs with history.pustState and/or hashchange - (window.fetch || 'imageRendering' in document.documentElement.style) && - (window.location.protocol === 'https:' || window.location.hostname === 'localhost' || window.location.hostname.indexOf('127.') === 0) + + return 'serviceWorker' in navigator && ( + window.location.protocol === 'https:' || + window.location.hostname === 'localhost' || + window.location.hostname.indexOf('127.') === 0 + ); + } function install(options) { @@ -90,8 +92,10 @@ function install(options) { if (hasSW()) { var registration = navigator.serviceWorker .register( - "sw.js" - + "sw.js", { + + + } ); diff --git a/tests/legacy/fixtures/basic-with-default-update-service/__expected/webpack4/main.js b/tests/legacy/fixtures/basic-with-default-update-service/__expected/webpack4/main.js index c8f34b65..23e7954b 100644 --- a/tests/legacy/fixtures/basic-with-default-update-service/__expected/webpack4/main.js +++ b/tests/legacy/fixtures/basic-with-default-update-service/__expected/webpack4/main.js @@ -82,11 +82,13 @@ __webpack_require__(1); var appCacheIframe; function hasSW() { - return 'serviceWorker' in navigator && - // This is how I block Chrome 40 and detect Chrome 41, because first has - // bugs with history.pustState and/or hashchange - (window.fetch || 'imageRendering' in document.documentElement.style) && - (window.location.protocol === 'https:' || window.location.hostname === 'localhost' || window.location.hostname.indexOf('127.') === 0) + + return 'serviceWorker' in navigator && ( + window.location.protocol === 'https:' || + window.location.hostname === 'localhost' || + window.location.hostname.indexOf('127.') === 0 + ); + } function install(options) { @@ -96,8 +98,10 @@ function install(options) { if (hasSW()) { var registration = navigator.serviceWorker .register( - "sw.js" - + "sw.js", { + + + } ); diff --git a/tests/legacy/fixtures/basic-with-runtime/__expected/webpack2/main.js b/tests/legacy/fixtures/basic-with-runtime/__expected/webpack2/main.js index caef52c6..3b3286cb 100644 --- a/tests/legacy/fixtures/basic-with-runtime/__expected/webpack2/main.js +++ b/tests/legacy/fixtures/basic-with-runtime/__expected/webpack2/main.js @@ -73,11 +73,13 @@ var appCacheIframe; function hasSW() { - return 'serviceWorker' in navigator && - // This is how I block Chrome 40 and detect Chrome 41, because first has - // bugs with history.pustState and/or hashchange - (window.fetch || 'imageRendering' in document.documentElement.style) && - (window.location.protocol === 'https:' || window.location.hostname === 'localhost' || window.location.hostname.indexOf('127.') === 0) + + return 'serviceWorker' in navigator && ( + window.location.protocol === 'https:' || + window.location.hostname === 'localhost' || + window.location.hostname.indexOf('127.') === 0 + ); + } function install(options) { @@ -87,8 +89,10 @@ function install(options) { if (hasSW()) { var registration = navigator.serviceWorker .register( - "sw.js" - + "sw.js", { + + + } ); diff --git a/tests/legacy/fixtures/basic-with-runtime/__expected/webpack3/main.js b/tests/legacy/fixtures/basic-with-runtime/__expected/webpack3/main.js index 49c6e941..6d51199a 100644 --- a/tests/legacy/fixtures/basic-with-runtime/__expected/webpack3/main.js +++ b/tests/legacy/fixtures/basic-with-runtime/__expected/webpack3/main.js @@ -76,11 +76,13 @@ __webpack_require__(1); var appCacheIframe; function hasSW() { - return 'serviceWorker' in navigator && - // This is how I block Chrome 40 and detect Chrome 41, because first has - // bugs with history.pustState and/or hashchange - (window.fetch || 'imageRendering' in document.documentElement.style) && - (window.location.protocol === 'https:' || window.location.hostname === 'localhost' || window.location.hostname.indexOf('127.') === 0) + + return 'serviceWorker' in navigator && ( + window.location.protocol === 'https:' || + window.location.hostname === 'localhost' || + window.location.hostname.indexOf('127.') === 0 + ); + } function install(options) { @@ -90,8 +92,10 @@ function install(options) { if (hasSW()) { var registration = navigator.serviceWorker .register( - "sw.js" - + "sw.js", { + + + } ); diff --git a/tests/legacy/fixtures/basic-with-runtime/__expected/webpack4/main.js b/tests/legacy/fixtures/basic-with-runtime/__expected/webpack4/main.js index a8dd697b..2d4d50ff 100644 --- a/tests/legacy/fixtures/basic-with-runtime/__expected/webpack4/main.js +++ b/tests/legacy/fixtures/basic-with-runtime/__expected/webpack4/main.js @@ -82,11 +82,13 @@ __webpack_require__(1); var appCacheIframe; function hasSW() { - return 'serviceWorker' in navigator && - // This is how I block Chrome 40 and detect Chrome 41, because first has - // bugs with history.pustState and/or hashchange - (window.fetch || 'imageRendering' in document.documentElement.style) && - (window.location.protocol === 'https:' || window.location.hostname === 'localhost' || window.location.hostname.indexOf('127.') === 0) + + return 'serviceWorker' in navigator && ( + window.location.protocol === 'https:' || + window.location.hostname === 'localhost' || + window.location.hostname.indexOf('127.') === 0 + ); + } function install(options) { @@ -96,8 +98,10 @@ function install(options) { if (hasSW()) { var registration = navigator.serviceWorker .register( - "sw.js" - + "sw.js", { + + + } ); diff --git a/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack2/appcache/manifest.appcache b/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack2/appcache/manifest.appcache index d206c4f6..9e5f3b26 100644 --- a/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack2/appcache/manifest.appcache +++ b/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack2/appcache/manifest.appcache @@ -1,5 +1,5 @@ CACHE MANIFEST -#ver:0a03f2003f9a697edfcafb1c4b7512d58756a658 +#ver:7a678897002d45bad07ea78af2e6439b031c37ad CACHE: /dist/main.js diff --git a/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack2/appcache/manifest.html b/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack2/appcache/manifest.html index 7e4d9c0b..971c12d2 100644 --- a/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack2/appcache/manifest.html +++ b/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack2/appcache/manifest.html @@ -1,2 +1,2 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack2/dist/main.js b/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack2/dist/main.js index f787f904..9e6391b1 100644 --- a/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack2/dist/main.js +++ b/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack2/dist/main.js @@ -73,11 +73,13 @@ var appCacheIframe; function hasSW() { - return 'serviceWorker' in navigator && - // This is how I block Chrome 40 and detect Chrome 41, because first has - // bugs with history.pustState and/or hashchange - (window.fetch || 'imageRendering' in document.documentElement.style) && - (window.location.protocol === 'https:' || window.location.hostname === 'localhost' || window.location.hostname.indexOf('127.') === 0) + + return 'serviceWorker' in navigator && ( + window.location.protocol === 'https:' || + window.location.hostname === 'localhost' || + window.location.hostname.indexOf('127.') === 0 + ); + } function install(options) { @@ -87,8 +89,10 @@ function install(options) { if (hasSW()) { var registration = navigator.serviceWorker .register( - "/override/sw.js" - + "/override/sw.js", { + + + } ); diff --git a/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack2/sw.js b/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack2/sw.js index 5d28e3fb..e810252e 100644 --- a/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack2/sw.js +++ b/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack2/sw.js @@ -15,11 +15,11 @@ var __wpo = { "/images/logo_grey.svg" ], "hashesMap": { - "db8b0ee600fdfe442ba4023910f59753adaa81e1": "/dist/main.js" + "2dfa8809a77b4225bd82f00be33201d4569f1e97": "/dist/main.js" }, "strategy": "changed", "responseStrategy": "cache-first", - "version": "0a03f2003f9a697edfcafb1c4b7512d58756a658", + "version": "7a678897002d45bad07ea78af2e6439b031c37ad", "name": "webpack-offline", "relativePaths": false }; \ No newline at end of file diff --git a/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack3/appcache/manifest.appcache b/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack3/appcache/manifest.appcache index 1274c3ef..4960cfb7 100644 --- a/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack3/appcache/manifest.appcache +++ b/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack3/appcache/manifest.appcache @@ -1,5 +1,5 @@ CACHE MANIFEST -#ver:5c8157a3d0ffa39f8d4ac6526f0d5d40f4327650 +#ver:b14ce808a8e5959d60015521fe997086a319d816 CACHE: /dist/main.js diff --git a/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack3/appcache/manifest.html b/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack3/appcache/manifest.html index 7e4d9c0b..971c12d2 100644 --- a/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack3/appcache/manifest.html +++ b/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack3/appcache/manifest.html @@ -1,2 +1,2 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack3/dist/main.js b/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack3/dist/main.js index 090d9343..8824f2f4 100644 --- a/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack3/dist/main.js +++ b/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack3/dist/main.js @@ -76,11 +76,13 @@ __webpack_require__(1); var appCacheIframe; function hasSW() { - return 'serviceWorker' in navigator && - // This is how I block Chrome 40 and detect Chrome 41, because first has - // bugs with history.pustState and/or hashchange - (window.fetch || 'imageRendering' in document.documentElement.style) && - (window.location.protocol === 'https:' || window.location.hostname === 'localhost' || window.location.hostname.indexOf('127.') === 0) + + return 'serviceWorker' in navigator && ( + window.location.protocol === 'https:' || + window.location.hostname === 'localhost' || + window.location.hostname.indexOf('127.') === 0 + ); + } function install(options) { @@ -90,8 +92,10 @@ function install(options) { if (hasSW()) { var registration = navigator.serviceWorker .register( - "/override/sw.js" - + "/override/sw.js", { + + + } ); diff --git a/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack3/sw.js b/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack3/sw.js index b5911649..1cc89e62 100644 --- a/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack3/sw.js +++ b/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack3/sw.js @@ -15,11 +15,11 @@ var __wpo = { "/images/logo_grey.svg" ], "hashesMap": { - "f7d57f70d9938f9062535c11b50f91b7618e5c70": "/dist/main.js" + "5dfbebb5592e532657357587fe4d97cdb0976882": "/dist/main.js" }, "strategy": "changed", "responseStrategy": "cache-first", - "version": "5c8157a3d0ffa39f8d4ac6526f0d5d40f4327650", + "version": "b14ce808a8e5959d60015521fe997086a319d816", "name": "webpack-offline", "relativePaths": false }; \ No newline at end of file diff --git a/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack4/appcache/manifest.appcache b/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack4/appcache/manifest.appcache index 02625207..0f07c45d 100644 --- a/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack4/appcache/manifest.appcache +++ b/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack4/appcache/manifest.appcache @@ -1,5 +1,5 @@ CACHE MANIFEST -#ver:c50e0d4feb90c7a5a9218d55b41e32058098eab7 +#ver:f8ee847484683a2795e0271515bf8f78831fb425 CACHE: /dist/main.js diff --git a/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack4/dist/main.js b/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack4/dist/main.js index c4dda8f9..555705ba 100644 --- a/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack4/dist/main.js +++ b/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack4/dist/main.js @@ -82,11 +82,13 @@ __webpack_require__(1); var appCacheIframe; function hasSW() { - return 'serviceWorker' in navigator && - // This is how I block Chrome 40 and detect Chrome 41, because first has - // bugs with history.pustState and/or hashchange - (window.fetch || 'imageRendering' in document.documentElement.style) && - (window.location.protocol === 'https:' || window.location.hostname === 'localhost' || window.location.hostname.indexOf('127.') === 0) + + return 'serviceWorker' in navigator && ( + window.location.protocol === 'https:' || + window.location.hostname === 'localhost' || + window.location.hostname.indexOf('127.') === 0 + ); + } function install(options) { @@ -96,8 +98,10 @@ function install(options) { if (hasSW()) { var registration = navigator.serviceWorker .register( - "/override/sw.js" - + "/override/sw.js", { + + + } ); diff --git a/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack4/sw.js b/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack4/sw.js index f1b83f35..c711859b 100644 --- a/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack4/sw.js +++ b/tests/legacy/fixtures/paths-explicit-absolute-tools-override-outside/__expected/webpack4/sw.js @@ -15,11 +15,11 @@ var __wpo = { "/images/logo_grey.svg" ], "hashesMap": { - "503268b1fca8fa6fd6de4e3eefa8b06b9dd80a85": "/dist/main.js" + "8fee76264c1c3d92c044a20297918f5ea098b43d": "/dist/main.js" }, "strategy": "changed", "responseStrategy": "cache-first", - "version": "c50e0d4feb90c7a5a9218d55b41e32058098eab7", + "version": "f8ee847484683a2795e0271515bf8f78831fb425", "name": "webpack-offline", "relativePaths": false }; \ No newline at end of file diff --git a/tpls/runtime-template.js b/tpls/runtime-template.js index 05cff080..85aaed91 100644 --- a/tpls/runtime-template.js +++ b/tpls/runtime-template.js @@ -1,11 +1,16 @@ var appCacheIframe; function hasSW() { - return 'serviceWorker' in navigator && - // This is how I block Chrome 40 and detect Chrome 41, because first has - // bugs with history.pustState and/or hashchange - (window.fetch || 'imageRendering' in document.documentElement.style) && - (window.location.protocol === 'https:' || window.location.hostname === 'localhost' || window.location.hostname.indexOf('127.') === 0) + <% if (ServiceWorker.force) { %> + // Forced install + return 'serviceWorker' in navigator; + <% } else { %> + return 'serviceWorker' in navigator && ( + window.location.protocol === 'https:' || + window.location.hostname === 'localhost' || + window.location.hostname.indexOf('127.') === 0 + ); + <% } %> } function install(options) { @@ -15,10 +20,17 @@ function install(options) { if (hasSW()) { var registration = navigator.serviceWorker .register( - <%- JSON.stringify(ServiceWorker.location) %> - <% if (ServiceWorker.scope) { %> - , { scope: <%- JSON.stringify(ServiceWorker.scope) %> } - <% } %> + <%- JSON.stringify(ServiceWorker.location) %>, { + <% if (ServiceWorker.scope) { %> + scope: <%- JSON.stringify(ServiceWorker.scope) %>, + <% } %> + <% if ( + ServiceWorker.updateViaCache && + ServiceWorker.updateViaCache !== 'imports' + ) { %> + updateViaCache: <%- JSON.stringify(ServiceWorker.updateViaCache) %>, + <% } %> + } ); <% if (ServiceWorker.events) { %>