Skip to content

Commit

Permalink
fix: service worker
Browse files Browse the repository at this point in the history
  • Loading branch information
wangshunnn committed Jan 21, 2025
1 parent 2e59ca9 commit 26b7c75
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 12 deletions.
20 changes: 17 additions & 3 deletions docs-vitepress/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,20 @@ export default withPwa(
base: "/",
head: [
["link", { rel: "icon", href: "/favicon.ico" }],
["link", { rel: "manifest", href: "/manifest.webmanifest" }],
[
"script",
{ id: "unregister-sw" },
"if('serviceWorker' in navigator) window.addEventListener('load', (e) => navigator.serviceWorker.register('/service-worker.js', { scope: '/' }))",
],
[
"script",
{ type: "text/javascript" },
`(function(c,l,a,r,i,t,y){
c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)};
t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i;
y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y);
})(window, document, "clarity", "script", "jtvvy52wxy");`,
})(window, document, "clarity", "script", "jtvvy52wxy");`,
],
["meta", { name: "author", content: title }],
["meta", { property: "og:type", content: "website" }],
Expand All @@ -251,12 +257,20 @@ export default withPwa(
pwa: {
base: "/",
scope: "/",
filename: "service-worker.js",
includeAssets: ["favicon.ico", "logo.png"],
manifest: {
name: title,
name: "Mpx",
short_name: "Mpx",
description,
theme_color: "#ffffff",
icons: [
{
src: "https://dpubstatic.udache.com/static/dpubimg/1ESVodfAED/logo.png",
sizes: "192x192",
type: "image/png",
},
],
},
workbox: {
globPatterns: ["**/*.{css,js,html,svg,png,ico,txt,woff2}"],
Expand Down Expand Up @@ -321,7 +335,7 @@ export default withPwa(
notFound: {
title: "页面未找到",
linkText: "返回首页",
quote: "😩 抱歉,迷路了~"
quote: "😩 抱歉,迷路了~",
},
docFooter: {
prev: "上一页",
Expand Down
14 changes: 5 additions & 9 deletions docs-vitepress/package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
{
"name": "docs-vuepress",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"description": "",
"name": "docs-vitepress",
"private": true,
"type": "module",
"description": "Mpx 官方文档",
"homepage": "https://mpxjs.cn",
"license": "ISC",
"dependencies": {
"vue": "^3.4.37"
},
Expand Down
15 changes: 15 additions & 0 deletions docs-vitepress/public/manifest.webmanifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "Mpx",
"short_name": "Mpx",
"icons": [
{
"src": "https://dpubstatic.udache.com/static/dpubimg/1ESVodfAED/logo.png",
"sizes": "192x192",
"type": "image/png"
}
],
"start_url": "/index.html",
"theme_color": "#ffffff",
"background_color": "#ffffff",
"display": "standalone"
}
17 changes: 17 additions & 0 deletions docs-vitepress/public/service-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// force an unregister
// see https://discord.com/channels/325477692906536972/790509637598314527/1032614068164493402
self.addEventListener('install', function (e) {
console.log('[old service-worker] install', e)
self.skipWaiting()
})
self.addEventListener('activate', function (e) {
console.log('[old service-worker] activate', e)
self.registration
.unregister()
.then(function () {
return self.clients.matchAll()
})
.then(function (clients) {
clients.forEach((client) => client.navigate(client.url))
})
})

0 comments on commit 26b7c75

Please sign in to comment.