From 604052b73eb7763c6c83082e92b500dea9c7197d Mon Sep 17 00:00:00 2001 From: meteorlxy Date: Wed, 18 Mar 2020 19:11:03 +0800 Subject: [PATCH] fix($plugin-pwa): work with register-service-worker 1.7.0 (close #2222) (#2229) --- .../@vuepress/plugin-pwa/lib/enhanceAppFile.js | 18 +++++++++--------- packages/@vuepress/plugin-pwa/package.json | 2 +- yarn.lock | 7 ++++--- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/packages/@vuepress/plugin-pwa/lib/enhanceAppFile.js b/packages/@vuepress/plugin-pwa/lib/enhanceAppFile.js index 1a1ae43ed2..e96d1df491 100644 --- a/packages/@vuepress/plugin-pwa/lib/enhanceAppFile.js +++ b/packages/@vuepress/plugin-pwa/lib/enhanceAppFile.js @@ -1,7 +1,6 @@ /* global SW_BASE_URL, SW_ENABLED, GA_ID, ga, SW_UPDATE_POPUP, SW_POPUP_COMPONENT */ import Vue from 'vue' -import { register } from 'register-service-worker' import SWUpdateEvent from './SWUpdateEvent' import event from './event' @@ -10,12 +9,13 @@ if (SW_UPDATE_POPUP && SW_POPUP_COMPONENT === 'SWUpdatePopup') { Vue.component('SWUpdatePopup', () => import('./SWUpdatePopup.vue')) } -export default ({ router, isServer }) => { - // Register service worker - router.onReady(() => { - if (process.env.NODE_ENV === 'production' - && !isServer - && SW_ENABLED) { +export default async ({ router, isServer }) => { + if (process.env.NODE_ENV === 'production' && !isServer && SW_ENABLED) { + // register-service-worker@1.7.0 references `window` in outer scope, so we have to import it dynamically in client + const { register } = await import('register-service-worker') + + // Register service worker + router.onReady(() => { register(`${SW_BASE_URL}service-worker.js`, { registrationOptions: {}, ready () { @@ -49,6 +49,6 @@ export default ({ router, isServer }) => { } } }) - } - }) + }) + } } diff --git a/packages/@vuepress/plugin-pwa/package.json b/packages/@vuepress/plugin-pwa/package.json index 14f77a7643..02a1373ae5 100644 --- a/packages/@vuepress/plugin-pwa/package.json +++ b/packages/@vuepress/plugin-pwa/package.json @@ -22,7 +22,7 @@ "main": "index.js", "dependencies": { "@vuepress/shared-utils": "^1.3.1", - "register-service-worker": "^1.5.2", + "register-service-worker": "^1.7.0", "workbox-build": "^4.3.1" }, "publishConfig": { diff --git a/yarn.lock b/yarn.lock index ab0d6c343d..1e9f1485d0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -11104,9 +11104,10 @@ regexpu-core@^4.6.0: unicode-match-property-ecmascript "^1.0.4" unicode-match-property-value-ecmascript "^1.1.0" -register-service-worker@^1.5.2: - version "1.6.2" - resolved "https://registry.yarnpkg.com/register-service-worker/-/register-service-worker-1.6.2.tgz#9297e54c205c371c6e49bfa88f6997e8dd315f4c" +register-service-worker@^1.7.0: + version "1.7.0" + resolved "https://registry.yarnpkg.com/register-service-worker/-/register-service-worker-1.7.0.tgz#b4b60d87a1d117f119bfd24ece073dcdb85dae64" + integrity sha512-sJQIxgodrulyN4d+bTkRnroPNMH3i1J4kP7Wm+vLhTP5CdbDSRr5jCeC9sJ6jyL603IZVbMAZ5HcU0hWCbJQQA== registry-auth-token@^4.0.0: version "4.0.0"