add service worker

This commit is contained in:
jxxghp
2024-06-05 18:12:07 +08:00
parent da910ac670
commit 157c37c862
16 changed files with 1044 additions and 715 deletions
+39
View File
@@ -0,0 +1,39 @@
import { createHandlerBoundToURL, cleanupOutdatedCaches, precacheAndRoute } from 'workbox-precaching'
import { NavigationRoute, registerRoute } from 'workbox-routing'
import { clientsClaim } from 'workbox-core'
declare let self: ServiceWorkerGlobalScope
cleanupOutdatedCaches()
// self.__WB_MANIFEST is default injection point
precacheAndRoute(self.__WB_MANIFEST)
// to allow work offline
registerRoute(new NavigationRoute(createHandlerBoundToURL('index.html'), { denylist: [/^\/api/] }))
// 通知选项
const options = {
icon: '/logo.png',
}
// 监听 push 事件,显示通知
self.addEventListener('push', function (e) {
if (!e.data) {
return
}
// 解析获取推送消息
let payload = e.data.json()
// 根据推送消息生成桌面通知并展现出来
let promise = self.registration.showNotification(payload.title, {
body: payload.body,
icon: payload.icon ?? options.icon,
data: {
url: payload.url,
},
})
e.waitUntil(promise)
})
self.skipWaiting()
clientsClaim()