feat: 同步服务托管网页应用,daywk 单域名部署就绪

This commit is contained in:
root
2026-10-16 12:05:47 +08:00
parent acfb0cfdd2
commit b73d5ef2cc
16 changed files with 188 additions and 3 deletions
+46
View File
@@ -0,0 +1,46 @@
const CACHE = 'intern-log-v1'
const CORE = ['./', './index.html']
self.addEventListener('install', event => {
event.waitUntil(caches.open(CACHE).then(c => c.addAll(CORE)).then(() => self.skipWaiting()))
})
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys()
.then(keys => Promise.all(keys.filter(k => k !== CACHE).map(k => caches.delete(k))))
.then(() => self.clients.claim())
)
})
self.addEventListener('fetch', event => {
const req = event.request
if (req.method !== 'GET') return
const url = new URL(req.url)
if (url.origin !== location.origin) return
// 页面导航:网络优先,离线回退缓存的应用壳
if (req.mode === 'navigate') {
event.respondWith(
fetch(req)
.then(resp => {
const clone = resp.clone()
caches.open(CACHE).then(c => c.put('./index.html', clone))
return resp
})
.catch(() => caches.match('./index.html'))
)
return
}
// 静态资源:缓存优先,未命中时拉取并写入缓存
event.respondWith(
caches.match(req).then(hit => hit || fetch(req).then(resp => {
if (resp.ok) {
const clone = resp.clone()
caches.open(CACHE).then(c => c.put(req, clone))
}
return resp
}))
)
})