From 176ccba64529fd015caa544186edb8694e6a4ec1 Mon Sep 17 00:00:00 2001 From: Haoqun Jiang Date: Tue, 16 Nov 2021 15:45:52 +0800 Subject: [PATCH] docs: add a self-destroying service worker to the vitepress build output --- docs/.vitepress/config.js | 47 +++++++------------ .../self-destroying-service-worker.js | 38 +++++++++++++++ 2 files changed, 56 insertions(+), 29 deletions(-) create mode 100644 docs/.vitepress/self-destroying-service-worker.js diff --git a/docs/.vitepress/config.js b/docs/.vitepress/config.js index 7e949dbdf6..6e9786b363 100644 --- a/docs/.vitepress/config.js +++ b/docs/.vitepress/config.js @@ -1,6 +1,23 @@ -const { VitePWA } = require('vite-plugin-pwa') +const fs = require('fs') +const path = require('path') + +const selfDestroyingSWVitePlugin = { + name: 'generate-self-destroying-service-worker', + buildStart() { + this.emitFile({ + type: 'asset', + fileName: 'service-worker.js', + source: fs.readFileSync(path.join(__dirname, './self-destroying-service-worker.js'), 'utf-8') + }) + } +} module.exports = { + vite: { + // to destroy the service worker used by the previous vuepress build + plugins: [selfDestroyingSWVitePlugin] + }, + locales: { '/': { lang: 'en-US', @@ -647,32 +664,4 @@ module.exports = { } } }, - - vite: { - plugins: [ - VitePWA({ - registerType: 'prompt', - manifest: { - 'name': 'Vue CLI', - 'short_name': 'Vue CLI', - 'icons': [ - { - 'src': '/icons/android-chrome-192x192.png', - 'sizes': '192x192', - 'type': 'image/png' - }, - { - 'src': '/icons/android-chrome-512x512.png', - 'sizes': '512x512', - 'type': 'image/png' - } - ], - 'start_url': '/index.html', - 'display': 'standalone', - 'background_color': '#fff', - 'theme_color': '#3eaf7c' - } - }) - ] - } } diff --git a/docs/.vitepress/self-destroying-service-worker.js b/docs/.vitepress/self-destroying-service-worker.js new file mode 100644 index 0000000000..9cfd20cecd --- /dev/null +++ b/docs/.vitepress/self-destroying-service-worker.js @@ -0,0 +1,38 @@ +// https://github.com/NekR/self-destroying-sw + +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 Arthur Stolyar + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + self.addEventListener('install', function(e) { + self.skipWaiting(); +}); + +self.addEventListener('activate', function(e) { + self.registration.unregister() + .then(function() { + return self.clients.matchAll(); + }) + .then(function(clients) { + clients.forEach(client => client.navigate(client.url)) + }); +});