34 lines
1006 B
JavaScript
34 lines
1006 B
JavaScript
const CACHE_NAME = 'bt-preview-v1'
|
|
const SHELL = ['/', '/gate', '/settings', '/manifest.json', '/icon.svg']
|
|
|
|
self.addEventListener('install', (event) => {
|
|
event.waitUntil(
|
|
caches.open(CACHE_NAME).then((cache) => cache.addAll(SHELL)).then(() => self.skipWaiting())
|
|
)
|
|
})
|
|
|
|
self.addEventListener('activate', (event) => {
|
|
event.waitUntil(self.clients.claim())
|
|
})
|
|
|
|
self.addEventListener('fetch', (event) => {
|
|
const { request } = event
|
|
const url = new URL(request.url)
|
|
|
|
// API、图片预览、下载等动态请求走网络,不做缓存
|
|
if (url.pathname.startsWith('/api/')) return
|
|
|
|
event.respondWith(
|
|
caches.match(request).then((cached) => {
|
|
if (cached) return cached
|
|
return fetch(request).then((res) => {
|
|
if (request.method === 'GET' && res.status === 200 && res.type === 'basic') {
|
|
const clone = res.clone()
|
|
caches.open(CACHE_NAME).then((cache) => cache.put(request, clone)).catch(() => {})
|
|
}
|
|
return res
|
|
})
|
|
})
|
|
)
|
|
})
|