From 7cbf6562f018e2de75e25efd58dff5979668c774 Mon Sep 17 00:00:00 2001 From: kyuuseiryuu Date: Fri, 27 Mar 2026 21:40:33 +0900 Subject: [PATCH] fix(sw): disable skipWaiting to preserve old user notifications Revert the automatic skipping of the waiting phase in the install event handler. Previously, self.skipWaiting() was called, which forces the new Service Worker to activate immediately. This change disables that behavior to ensure that users with older versions of the app can continue receiving notifications without interruption during the update process. Added a console log to track the installation phase for debugging purposes. --- src/hooks/useAutoUpdateNotificationToken.ts | 6 ++++++ src/sw.ts | 5 ++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/hooks/useAutoUpdateNotificationToken.ts b/src/hooks/useAutoUpdateNotificationToken.ts index 77d456b..b3ed479 100644 --- a/src/hooks/useAutoUpdateNotificationToken.ts +++ b/src/hooks/useAutoUpdateNotificationToken.ts @@ -2,6 +2,7 @@ import { useEffect, useState } from "react"; import { useNotificationToken } from "@/hooks/useNotificationToken"; import { useNotificationTokenAPI } from "@/hooks/useNotificationTokenAPI"; import { NOTIFICATION_TOKEN_KEY } from "@/utils/constants"; +import { App } from "antd"; export const useAutoUpdateNotificationToken = () => { const [token] = useNotificationToken(); @@ -11,6 +12,7 @@ export const useAutoUpdateNotificationToken = () => { unsetTokenReq, } = useNotificationTokenAPI(); const [isNotificationEnable, setIsNotificationEnable] = useState(false); + const { message } = App.useApp(); useEffect(() => { if (!token) return; isTokenSetedReq.runAsync(token).then(res => { @@ -30,6 +32,10 @@ export const useAutoUpdateNotificationToken = () => { .then(() => { }).then(() => { console.debug('Auto udpate token', token); + message.open({ + key: 'auto-update-token', + content: `通知 Token 已更新`, + }); }); }, [updateTokenReq, unsetTokenReq, isNotificationEnable, token]); } \ No newline at end of file diff --git a/src/sw.ts b/src/sw.ts index ae35efb..a43a831 100644 --- a/src/sw.ts +++ b/src/sw.ts @@ -14,9 +14,8 @@ const messaging = getMessaging(app); // 1. 监听安装事件 self.addEventListener('install', (event) => { - // console.log('新版本 Service Worker 安装中...'); - // 强制跳过等待阶段,直接进入激活阶段 - self.skipWaiting(); + console.log('新版本 Service Worker 安装中...'); + // 不能强制更新,会影响旧版本用户的通知 }); self.addEventListener('activate', (event) => {