Files
Cyrene/frontend/web/public/sw.js
T
AskaEth bcf4d4e621 feat: 第五轮开发 - 14项未来路线图功能完整实现
W1-W14 全部完成:
- W1: 消息搜索 (ILIKE全文检索 + SearchModal)
- W2: 对话导出 (JSON/Markdown/TXT三格式)
- W3: 记忆时间线 DevTools 可视化
- W4: 通知推送系统 (WebSocket + Browser Notification API)
- W5: 定时提醒 (30s轮询 + 重复提醒 + WebSocket推送)
- W6: 每日简报 (08:00自动生成: 天气+新闻+提醒+AI摘要)
- W7: IoT场景自动化 (规则引擎 10s轮询 + 条件评估 + 场景执行)
- W8: 语音输入 (浏览器 Speech Recognition API)
- W9: STT服务 (voice-service + whisper.cpp)
- W10: TTS服务 (浏览器 Speech Synthesis + edge-tts三档回退)
- W11: 文件管理 (上传/下载/缩略图/纯Go bilinear缩放)
- W12: 知识库RAG (PostgreSQL tsvector + 文档分块 + 检索)
- W13: 多模态 (图片上传+分析: Vision API + 本地Go分析回退)
- W14: PWA (Service Worker + 离线页 + install prompt)

总计: 6个Go微服务 + 10+前端组件 + 10+ PostgreSQL表 + 4个后台调度器
2026-05-19 12:01:09 +08:00

108 lines
3.0 KiB
JavaScript

const CACHE_NAME = 'cyrene-v1';
const ASSETS_TO_CACHE = [
'/',
'/index.html',
];
// Install: 缓存核心资源
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
return cache.addAll(ASSETS_TO_CACHE);
})
);
self.skipWaiting();
});
// Activate: 清理旧缓存
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((keys) => {
return Promise.all(
keys.filter(key => key !== CACHE_NAME).map(key => caches.delete(key))
);
})
);
self.clients.claim();
});
// Fetch: 缓存优先策略(对 API 请求使用网络优先)
self.addEventListener('fetch', (event) => {
const url = new URL(event.request.url);
// API 请求:网络优先,失败时返回离线 JSON
if (url.pathname.startsWith('/api/')) {
event.respondWith(networkFirst(event.request));
} else if (url.pathname.startsWith('/ws')) {
// WebSocket 连接不缓存
event.respondWith(fetch(event.request));
} else {
// 静态资源:缓存优先
event.respondWith(cacheFirst(event.request));
}
});
async function cacheFirst(request) {
const cached = await caches.match(request);
if (cached) return cached;
try {
const response = await fetch(request);
if (response.ok) {
const cache = await caches.open(CACHE_NAME);
cache.put(request, response.clone());
}
return response;
} catch (e) {
// 返回离线页面(HTML 请求)
if (request.headers.get('Accept')?.includes('text/html')) {
return caches.match('/offline.html');
}
throw e;
}
}
async function networkFirst(request) {
try {
const response = await fetch(request);
const cache = await caches.open(CACHE_NAME);
cache.put(request, response.clone());
return response;
} catch (e) {
const cached = await caches.match(request);
if (cached) return cached;
return new Response(JSON.stringify({ error: '离线状态,请检查网络连接' }), {
status: 503,
headers: { 'Content-Type': 'application/json' }
});
}
}
// Push 通知
self.addEventListener('push', (event) => {
const data = event.data?.json() || { title: 'Cyrene', body: '新消息' };
event.waitUntil(
self.registration.showNotification(data.title, {
body: data.body,
icon: '/images/Cyrene_Avatar/2nd_Form/Cyrene-2F-N-Happy-1.png',
badge: '/images/Cyrene_Avatar/2nd_Form/Cyrene-2F-N-Happy-1.png',
data: data.data || {},
})
);
});
self.addEventListener('notificationclick', (event) => {
event.notification.close();
const sessionId = event.notification.data?.session_id;
const targetUrl = sessionId ? `/#/session/${sessionId}` : '/';
event.waitUntil(
clients.matchAll({ type: 'window' }).then((clientList) => {
for (const client of clientList) {
if (client.url.includes(targetUrl) && 'focus' in client) {
return client.focus();
}
}
if (clients.openWindow) return clients.openWindow(targetUrl);
})
);
});