Skip to content

Commit 176ccba

Browse files
committed
docs: add a self-destroying service worker to the vitepress build output
1 parent e76a7a8 commit 176ccba

File tree

2 files changed

+56
-29
lines changed

2 files changed

+56
-29
lines changed

docs/.vitepress/config.js

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
1-
const { VitePWA } = require('vite-plugin-pwa')
1+
const fs = require('fs')
2+
const path = require('path')
3+
4+
const selfDestroyingSWVitePlugin = {
5+
name: 'generate-self-destroying-service-worker',
6+
buildStart() {
7+
this.emitFile({
8+
type: 'asset',
9+
fileName: 'service-worker.js',
10+
source: fs.readFileSync(path.join(__dirname, './self-destroying-service-worker.js'), 'utf-8')
11+
})
12+
}
13+
}
214

315
module.exports = {
16+
vite: {
17+
// to destroy the service worker used by the previous vuepress build
18+
plugins: [selfDestroyingSWVitePlugin]
19+
},
20+
421
locales: {
522
'/': {
623
lang: 'en-US',
@@ -647,32 +664,4 @@ module.exports = {
647664
}
648665
}
649666
},
650-
651-
vite: {
652-
plugins: [
653-
VitePWA({
654-
registerType: 'prompt',
655-
manifest: {
656-
'name': 'Vue CLI',
657-
'short_name': 'Vue CLI',
658-
'icons': [
659-
{
660-
'src': '/icons/android-chrome-192x192.png',
661-
'sizes': '192x192',
662-
'type': 'image/png'
663-
},
664-
{
665-
'src': '/icons/android-chrome-512x512.png',
666-
'sizes': '512x512',
667-
'type': 'image/png'
668-
}
669-
],
670-
'start_url': '/index.html',
671-
'display': 'standalone',
672-
'background_color': '#fff',
673-
'theme_color': '#3eaf7c'
674-
}
675-
})
676-
]
677-
}
678667
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// https://github.com/NekR/self-destroying-sw
2+
3+
/**
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 Arthur Stolyar <[email protected]>
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in all
16+
* copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
* SOFTWARE.
25+
*/
26+
self.addEventListener('install', function(e) {
27+
self.skipWaiting();
28+
});
29+
30+
self.addEventListener('activate', function(e) {
31+
self.registration.unregister()
32+
.then(function() {
33+
return self.clients.matchAll();
34+
})
35+
.then(function(clients) {
36+
clients.forEach(client => client.navigate(client.url))
37+
});
38+
});

0 commit comments

Comments
 (0)