Merge branch 'feature_bugfix/251111_event' into feature_bugfix/251110_event

* feature_bugfix/251111_event:
  fix(socket): 保留暂存监听器,修复重连后事件监听器丢失问题
  fix(socket): 暴露 Socket 实例到 window 对象,修复生产环境事件监听器失效问题
  fix(notification): 修复 Socket 重连后通知功能失效问题(方案2)
  feat: 添加调试 API     - 我修改 NotificationContext.js,暴露 addNotification 到 window     - 或者在调试工具 (devtools/notificationDebugger.js) 中添加测试方法     - 重新构建并部署     - 可以手动触发网页通知
  feat: Service Worker 注册失败修复方案 1. 使用了 window.location.origin,但 Service Worker 环境中没有 window 对象 2. 注册逻辑缺少详细的错误处理和状态检查
  feat: 通知调试能力
This commit is contained in:
zdl
2025-11-11 18:59:00 +08:00
17 changed files with 1460 additions and 1461 deletions

View File

@@ -2,10 +2,10 @@
/**
* Service Worker for Browser Notifications
* 主要功能:支持浏览器通知的稳定运行
*
* 注意:此 Service Worker 仅用于通知功能,不拦截任何 HTTP 请求
*/
const CACHE_NAME = 'valuefrontier-v1';
// Service Worker 安装事件
self.addEventListener('install', (event) => {
console.log('[Service Worker] Installing...');
@@ -35,7 +35,7 @@ self.addEventListener('notificationclick', (event) => {
.then((windowClients) => {
// 查找是否已有打开的窗口
for (let client of windowClients) {
if (client.url.includes(window.location.origin) && 'focus' in client) {
if (client.url.includes(self.location.origin) && 'focus' in client) {
// 聚焦现有窗口并导航到目标页面
return client.focus().then(client => {
return client.navigate(urlToOpen);
@@ -56,18 +56,6 @@ self.addEventListener('notificationclose', (event) => {
console.log('[Service Worker] Notification closed:', event.notification.tag);
});
// Fetch 事件 - 基础的网络优先策略
self.addEventListener('fetch', (event) => {
// 对于通知相关的资源,使用网络优先策略
event.respondWith(
fetch(event.request)
.catch(() => {
// 网络失败时,尝试从缓存获取
return caches.match(event.request);
})
);
});
// 推送消息事件(预留,用于未来的 Push API 集成)
self.addEventListener('push', (event) => {
console.log('[Service Worker] Push message received:', event);