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 <cursoragent@cursor.com>
This commit is contained in:
eric 2026-08-20 07:53:31 -05:00
parent c9c3c8e1b4
commit 472cef16de
3 changed files with 70 additions and 17 deletions

View File

@ -1,8 +1,9 @@
{
"id": "/",
"name": "直播监控中心",
"short_name": "LiveRadar",
"description": "抖音 + YouTube 直播监控 · Shorts / TikTok 分发",
"start_url": "/",
"start_url": "/?source=pwa",
"scope": "/",
"display": "standalone",
"orientation": "any",

View File

@ -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();
})(),
);
});

View File

@ -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<BeforeInstallPromptEvent | null>(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() {
<p className="text-sm font-medium text-slate-100">安装到主屏幕</p>
<p className="mt-0.5 text-[11px] leading-relaxed text-slate-400">
{iosHint
? "Safari:点分享 →「添加到主屏幕」,可像 App 一样打开。"
: "可选安装为 PWA,方便手机快速打开与操作。"}
? "Safari:点底部分享 →「添加到主屏幕」。"
: androidHint && !deferred
? "Chrome:右上角 ⋮ →「安装应用」或「添加到主屏幕」。装完若桌面没有,到应用列表找 LiveRadar。"
: "点安装后会弹出系统确认框;确认后图标可能在应用列表,不一定直接出现在桌面。"}
</p>
{status ? (
<p className="mt-1.5 text-[11px] leading-relaxed text-amber-100/90">{status}</p>
) : null}
<div className="mt-2 flex flex-wrap gap-2">
{deferred ? (
<button
type="button"
disabled={busy}
onClick={() => void install()}
className="rounded-lg bg-teal-500/20 px-3 py-1.5 text-xs text-teal-100 hover:bg-teal-500/30"
className="rounded-lg bg-teal-500/20 px-3 py-1.5 text-xs text-teal-100 hover:bg-teal-500/30 disabled:opacity-50"
>
安装
{busy ? "弹出中…" : "安装"}
</button>
) : null}
<button