Files
Toolbox/public/tools/web_zen_box/sw.js
T
yangxiangyuan 212dbc4e1d refactor: 重构禅音工具路径与配置,新增私人剪贴板技能
这是一次大型重构和功能新增:
1. 将原 zen_box 工具重命名为 web_zen_box,调整所有相关路径与配置
2. 新增 web_order_box 原生Web应用的完整资源与部署脚本
3. 新增私人剪贴板后端技能与配套测试脚本
4. 修复订单盒子APP的API地址配置,避免路径重复拼接
5. 新增本地开发跨域白名单支持localhost
6. 清理旧版zen_box的冗余文件
2026-07-26 21:03:28 +08:00

62 lines
1.5 KiB
JavaScript

const CACHE = 'music-box-v4';
const AUDIO_CACHE = 'zen-audio-cache';
self.addEventListener('install', e => {
self.skipWaiting();
e.waitUntil(
caches.open(CACHE).then(c => c.addAll([
'./',
'./index.html',
'./zen_box.js',
'./zen_box.css'
]))
);
});
self.addEventListener('activate', e => {
e.waitUntil(
caches.keys().then(keys =>
Promise.all(
keys.filter(k => k !== CACHE && k !== AUDIO_CACHE).map(k => caches.delete(k))
)
).then(() => clients.claim())
);
});
self.addEventListener('fetch', e => {
// 音频文件:优先命中 zen-audio-cache(离线播放必需),否则走网络
if (e.request.url.includes('.mp3') || e.request.url.includes('oss')) {
e.respondWith(
caches.open(AUDIO_CACHE).then(audioCache =>
audioCache.match(e.request).then(cached => {
if (cached) return cached;
return fetch(e.request);
})
).catch(() => fetch(e.request))
);
return;
}
// playlist.json 使用 Network First
if (e.request.url.includes('playlist.json')) {
e.respondWith(
fetch(e.request)
.then(networkResponse => {
return caches.open(CACHE).then(cache => {
cache.put(e.request, networkResponse.clone());
return networkResponse;
});
})
.catch(() => {
return caches.match(e.request);
})
);
return;
}
// 其他文件(App Shell)使用 Cache First
e.respondWith(
caches.match(e.request).then(r => r || fetch(e.request))
);
});