From 472cef16deb23f4ac7fd43bc91a8372131cad3bd Mon Sep 17 00:00:00 2001 From: eric Date: Thu, 20 Aug 2026 07:53:31 -0500 Subject: [PATCH] fix(pwa): clearer Android install feedback + harden SW precache Explain app-drawer vs home screen; show manual Chrome menu steps; avoid SW install fail on auth redirect. Co-authored-by: Cursor --- frontend/public/manifest.webmanifest | 3 +- frontend/public/sw.js | 13 +++- frontend/src/components/PwaInstallBanner.tsx | 71 ++++++++++++++++---- 3 files changed, 70 insertions(+), 17 deletions(-) diff --git a/frontend/public/manifest.webmanifest b/frontend/public/manifest.webmanifest index 15442ac..791e028 100644 --- a/frontend/public/manifest.webmanifest +++ b/frontend/public/manifest.webmanifest @@ -1,8 +1,9 @@ { + "id": "/", "name": "直播监控中心", "short_name": "LiveRadar", "description": "抖音 + YouTube 直播监控 · Shorts / TikTok 分发", - "start_url": "/", + "start_url": "/?source=pwa", "scope": "/", "display": "standalone", "orientation": "any", diff --git a/frontend/public/sw.js b/frontend/public/sw.js index 1e16a40..3bcfa96 100644 --- a/frontend/public/sw.js +++ b/frontend/public/sw.js @@ -1,10 +1,17 @@ /* Minimal service worker — enables installability; network-first for app/API. */ -const CACHE = "dylive-shell-v1"; -const PRECACHE = ["/", "/manifest.webmanifest", "/icon-192.png", "/icon-512.png", "/apple-touch-icon.png"]; +const CACHE = "dylive-shell-v2"; +const PRECACHE = ["/manifest.webmanifest", "/icon-192.png", "/icon-512.png", "/apple-touch-icon.png"]; self.addEventListener("install", (event) => { event.waitUntil( - caches.open(CACHE).then((cache) => cache.addAll(PRECACHE)).then(() => self.skipWaiting()), + (async () => { + const cache = await caches.open(CACHE); + // Prefer one-by-one so a single miss (e.g. auth redirect on "/") does not fail SW install. + await Promise.all( + PRECACHE.map((url) => cache.add(url).catch(() => undefined)), + ); + await self.skipWaiting(); + })(), ); }); diff --git a/frontend/src/components/PwaInstallBanner.tsx b/frontend/src/components/PwaInstallBanner.tsx index 2cb7c73..006d783 100644 --- a/frontend/src/components/PwaInstallBanner.tsx +++ b/frontend/src/components/PwaInstallBanner.tsx @@ -10,34 +10,63 @@ type BeforeInstallPromptEvent = Event & { const DISMISS_KEY = "dylive_pwa_install_dismissed"; +function isStandalone(): boolean { + if (typeof window === "undefined") return false; + if (window.matchMedia("(display-mode: standalone)").matches) return true; + // iOS Safari + return Boolean((navigator as Navigator & { standalone?: boolean }).standalone); +} + export function PwaInstallBanner() { const [deferred, setDeferred] = useState(null); const [visible, setVisible] = useState(false); const [iosHint, setIosHint] = useState(false); + const [androidHint, setAndroidHint] = useState(false); + const [status, setStatus] = useState(""); + const [busy, setBusy] = useState(false); useEffect(() => { if (typeof window === "undefined") return; - if (window.matchMedia("(display-mode: standalone)").matches) return; + if (isStandalone()) return; if (localStorage.getItem(DISMISS_KEY) === "1") return; const ua = navigator.userAgent || ""; - const isIos = /iPad|iPhone|iPod/.test(ua) || (navigator.platform === "MacIntel" && navigator.maxTouchPoints > 1); + const isIos = + /iPad|iPhone|iPod/.test(ua) || (navigator.platform === "MacIntel" && navigator.maxTouchPoints > 1); const isSafari = /Safari/.test(ua) && !/CriOS|FxiOS|EdgiOS|Chrome/.test(ua); + const isAndroid = /Android/i.test(ua); + const isChrome = /Chrome|CriOS/i.test(ua) && !/Edg/i.test(ua); const onBip = (e: Event) => { e.preventDefault(); setDeferred(e as BeforeInstallPromptEvent); setVisible(true); setIosHint(false); + setAndroidHint(false); + setStatus(""); }; window.addEventListener("beforeinstallprompt", onBip); + const onInstalled = () => { + setStatus("已安装。若桌面没有图标,到手机「应用列表」找 LiveRadar,长按添加到桌面。"); + setVisible(true); + setDeferred(null); + }; + window.addEventListener("appinstalled", onInstalled); + if (isIos && isSafari) { setIosHint(true); setVisible(true); + } else if (isAndroid && isChrome) { + // Chrome 可能不触发 beforeinstallprompt(已装过 / 策略限制),仍给出手动步骤 + setAndroidHint(true); + setVisible(true); } - return () => window.removeEventListener("beforeinstallprompt", onBip); + return () => { + window.removeEventListener("beforeinstallprompt", onBip); + window.removeEventListener("appinstalled", onInstalled); + }; }, []); if (!visible) return null; @@ -49,15 +78,25 @@ export function PwaInstallBanner() { }; const install = async () => { - if (!deferred) return; - await deferred.prompt(); + if (!deferred || busy) return; + setBusy(true); + setStatus(""); try { - await deferred.userChoice; + await deferred.prompt(); + const choice = await deferred.userChoice; + if (choice.outcome === "accepted") { + setStatus("系统已确认安装。请到桌面或「应用列表」找 LiveRadar;没有的话长按应用图标 → 添加到桌面。"); + } else { + setStatus("你取消了系统安装弹窗。也可手动:Chrome 右上角 ⋮ →「安装应用」或「添加到主屏幕」。"); + setAndroidHint(true); + } } catch { - /* ignore */ + setStatus("系统安装弹窗失败。请用 Chrome 右上角 ⋮ →「安装应用」/「添加到主屏幕」。"); + setAndroidHint(true); + } finally { + setDeferred(null); + setBusy(false); } - setDeferred(null); - setVisible(false); }; return ( @@ -71,17 +110,23 @@ export function PwaInstallBanner() {

安装到主屏幕

{iosHint - ? "Safari:点分享 →「添加到主屏幕」,可像 App 一样打开。" - : "可选安装为 PWA,方便手机快速打开与操作。"} + ? "Safari:点底部分享 →「添加到主屏幕」。" + : androidHint && !deferred + ? "Chrome:右上角 ⋮ →「安装应用」或「添加到主屏幕」。装完若桌面没有,到应用列表找 LiveRadar。" + : "点安装后会弹出系统确认框;确认后图标可能在应用列表,不一定直接出现在桌面。"}

+ {status ? ( +

{status}

+ ) : null}
{deferred ? ( ) : null}