Skip to content

Commit

Permalink
docs: add a self-destroying service worker to the vitepress build output
Browse files Browse the repository at this point in the history
  • Loading branch information
haoqunjiang committed Nov 16, 2021
1 parent e76a7a8 commit 176ccba
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 29 deletions.
47 changes: 18 additions & 29 deletions docs/.vitepress/config.js
Original file line number Diff line number Diff line change
@@ -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',
Expand Down Expand Up @@ -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'
}
})
]
}
}
38 changes: 38 additions & 0 deletions docs/.vitepress/self-destroying-service-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// https://github.com/NekR/self-destroying-sw

/**
* The MIT License (MIT)
*
* Copyright (c) 2017 Arthur Stolyar <nekr.fabula@gmail.com>
*
* 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))
});
});

0 comments on commit 176ccba

Please sign in to comment.