fix: 图片预览错误返回可读 503 + feat: PWA 支持(manifest/sw/图标)
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
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
|
||||
})
|
||||
})
|
||||
)
|
||||
})
|
||||
Reference in New Issue
Block a user