fix(pwa): show install prompt only once
This commit is contained in:
@@ -6,8 +6,8 @@
|
||||
* - 等聊天消息数量达到触发阈值后才开始检查
|
||||
* - 检查 PWA 支持(pwaUtil.isSupported)
|
||||
* - 检查 PWA 是否已安装(pwaUtil.isInstalled)—— 已安装则不弹
|
||||
* - 检查每日是否已显示(使用 AppStorage 持久化)
|
||||
* - 延迟 3.5s 后弹出 PwaInstallDialog
|
||||
* - 检查是否已经显示过(使用 AppStorage 持久化)
|
||||
* - 延迟后弹出 PwaInstallDialog
|
||||
* - 生产环境有效;开发环境 1s 后立即弹出(测试用)
|
||||
*
|
||||
* 注意:PWA install prompt API(`beforeinstallprompt`)在浏览器差异较大,
|
||||
@@ -15,12 +15,15 @@
|
||||
*/
|
||||
import { useEffect } from "react";
|
||||
|
||||
import { BrowserDetector, SpAsyncUtil, pwaUtil } from "@/utils";
|
||||
import {
|
||||
canShowPwaInstallPromptOnce,
|
||||
recordPwaInstallPromptShown,
|
||||
} from "@/lib/chat/pwa_install_prompt";
|
||||
import { BrowserDetector, pwaUtil } from "@/utils";
|
||||
|
||||
import { PwaInstallDialog } from "./pwa-install-dialog";
|
||||
import styles from "./pwa-install-overlay.module.css";
|
||||
|
||||
const PWA_DIALOG_KEY = "cozsweet:lastPwaDialogShown";
|
||||
const DEVELOPMENT_DIALOG_DELAY_MS = 1000;
|
||||
const PRODUCTION_DIALOG_DELAY_MS = 1500;
|
||||
|
||||
@@ -70,22 +73,17 @@ async function shouldShowPwaInstallDialog(isDevelopment: boolean) {
|
||||
if (pwaUtil.isInstalled()) return false;
|
||||
if (BrowserDetector.isInAppBrowser()) return false;
|
||||
|
||||
// 开发环境:跳过"每日一次"门禁,每次进入都弹,方便 UI 测试。
|
||||
// 开发环境:跳过"只弹一次"门禁,每次进入都弹,方便 UI 测试。
|
||||
if (isDevelopment) return true;
|
||||
|
||||
// 生产环境:每日只弹一次(防骚扰)。
|
||||
const lastResult = await SpAsyncUtil.getString(PWA_DIALOG_KEY);
|
||||
if (!lastResult.success) return false;
|
||||
|
||||
const today = new Date().toISOString().slice(0, 10);
|
||||
return lastResult.data !== today;
|
||||
// 生产环境:永久只弹一次(防骚扰)。
|
||||
return canShowPwaInstallPromptOnce();
|
||||
}
|
||||
|
||||
function showPwaDialog() {
|
||||
if (typeof document === "undefined") return;
|
||||
// 记录显示时间
|
||||
const today = new Date().toISOString().slice(0, 10);
|
||||
void SpAsyncUtil.setString(PWA_DIALOG_KEY, today);
|
||||
// 记录已显示,后续不再弹出。
|
||||
void recordPwaInstallPromptShown();
|
||||
|
||||
// 创建挂载点
|
||||
const root = document.createElement("div");
|
||||
|
||||
Reference in New Issue
Block a user