beforeInstallPrompt事件不触发
如果页面是可安装的话,将在页面加载后不久触发。
没触发的原因?
- PWA 必须尚未安装 (chrome://apps查看已经安装的应用)
- 满足用户参与启发式(以前,用户必须与域交互至少 30 秒,这不再是要求)。
- 您的 Web 应用程序必须包含 Web 应用程序清单。
- 您的 Web 应用程序必须通过安全的 HTTPS 连接提供服务。(或者是localhost)如果证书过期了证书有问题也会有问题
这里可以直接用chrome://flags/#unsafely-treat-insecure-origin-as-secure设置一下 注意本地需要启http服务,如果是HTTPS会不生效,如果是不仅仅在Chrome测试,推荐webpack-mkcert本地就能启动https 证书有效
- 已使用 fetch 事件处理程序注册了一个服务工作者。
- manifest.json编写有问题,(注意这里也可以是json后缀) 如下
- start_url有问题或者是没设置scope,当设置的是相对路径的时候,是相对于当前json文件所在的路径,如果JSON文件是一个外链文件的话,这里的start_url需要是绝对路径,同时scope 也必须设置成绝对路径
- icons的src有问题,图片必须跟设置的sizes是同一宽高比,此外似乎只支持png jpeg格式
- icons的src没设置type
怎么debug ?
- devtool的lighthouse可以检测你的pwa存在哪些问题 为什么不生效,但是好像不准确
- 验证该浏览器是否支持?
怎么动态设置start_url?
import manifestJson from './manifests.json';
const manifestContent = {
...manifestJson,
name,
start_url: window.location.href,
scope: `${window.location.origin}${window.location.pathname}`
};
const imgType = cover.replace(/\?.*/, '').split('.').slice(-1)[0];
const {icons} = manifestContent;
icons[0].src = cover;
icons[0].type = `image/${imgType}`;
const linkDom = document.querySelector('#manifest');
const stringManifest = JSON.stringify(manifestContent);
if (linkDom) {
linkDom.setAttribute('href', 'data:application/json;charset=utf-8,' + encodeURIComponent(stringManifest));
} else {
const link = document.createElement('link');
link.id = 'manifest';
link.rel = 'manifest';
link.setAttribute('href', 'data:application/json;charset=utf-8,' + encodeURIComponent(stringManifest));
document.head.appendChild(link);
}
以上测试结果来源于chrome, 360浏览器, Safari不支持beforeInstallPrompt