From 858034cf8a3322f16c136413119ff6b8cf5fdc8b Mon Sep 17 00:00:00 2001 From: tianma Date: Wed, 1 Jul 2026 00:09:16 +0800 Subject: [PATCH] Make PWA update notice detect build changes --- apps/web/src/components/PwaUpdateNotice.tsx | 31 +++++++++++++++++---- apps/web/src/lib/pwa-update-notice.ts | 20 +++++++++++++ apps/web/src/main.tsx | 2 +- apps/web/src/vite-env.d.ts | 3 ++ apps/web/vite.config.ts | 15 ++++++++++ 5 files changed, 64 insertions(+), 7 deletions(-) diff --git a/apps/web/src/components/PwaUpdateNotice.tsx b/apps/web/src/components/PwaUpdateNotice.tsx index 761a1322..bd650e06 100644 --- a/apps/web/src/components/PwaUpdateNotice.tsx +++ b/apps/web/src/components/PwaUpdateNotice.tsx @@ -3,6 +3,7 @@ import { CheckCircle2, RefreshCw, X } from "lucide-react"; import { Button } from "@/components/ui/button"; import { cn } from "@/lib/utils"; import { + consumePwaBuildUpdate, consumePwaUpdateReloadPending, PWA_UPDATE_NOTICE_EVENT, type PwaUpdateNoticeEvent, @@ -10,6 +11,7 @@ import { } from "@/lib/pwa-update-notice"; type Notice = { + buildLabel?: string; id: number; kind: PwaUpdateNoticeKind; }; @@ -63,6 +65,14 @@ const noticeCopy: Record { + if (notice.kind === "updated" && notice.buildLabel) { + return `当前 PWA 已是最新版本(${notice.buildLabel})。`; + } + + return fallbackDescription; +}; + export const PwaUpdateNotice = () => { const [displayMode, setDisplayMode] = useState(() => getDisplayMode()); const [notice, setNotice] = useState(null); @@ -73,18 +83,27 @@ export const PwaUpdateNotice = () => { }, []); useEffect(() => { - if (consumePwaUpdateReloadPending()) { - setNotice({ id: Date.now(), kind: "updated" }); + if (!displayMode.visible) { + return; + } + + const buildLabel = __EDGEEVER_BUILD_LABEL__; + + if ( + consumePwaUpdateReloadPending() || + consumePwaBuildUpdate(__EDGEEVER_BUILD_ID__, { notifyWhenMissingBaseline: true }) + ) { + setNotice({ buildLabel, id: Date.now(), kind: "updated" }); } const handleNotice = (event: Event) => { - const { kind } = (event as PwaUpdateNoticeEvent).detail; - setNotice({ id: Date.now(), kind }); + const { buildLabel: eventBuildLabel, kind } = (event as PwaUpdateNoticeEvent).detail; + setNotice({ buildLabel: eventBuildLabel ?? buildLabel, id: Date.now(), kind }); }; window.addEventListener(PWA_UPDATE_NOTICE_EVENT, handleNotice); return () => window.removeEventListener(PWA_UPDATE_NOTICE_EVENT, handleNotice); - }, []); + }, [displayMode.visible]); useEffect(() => { if (!notice) { @@ -126,7 +145,7 @@ export const PwaUpdateNotice = () => {
{copy.title}
-
{copy.description}
+
{getNoticeDescription(notice, copy.description)}
{isReloadRequired ? (