mirror of
https://github.com/jxxghp/MoviePilot-Frontend.git
synced 2026-09-05 23:56:42 +08:00
add service worker
This commit is contained in:
@@ -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()
|
||||
Reference in New Issue
Block a user