我正在尝试在最新的 Chrome 和 Android (7) 上实现添加到主屏幕。我在清单文件中指定了“独立”,但该应用程序仅在浏览器中打开。我之前在同一设备上获得了所需的行为,但似乎无法重现它。
我看到有人在这个问题上有类似的问题。我遵循了建议的解决方案 - 使用 Lighthouse 验证 PWA 属性 - 但即使 Lighthouse 获得完美的 100 分,我仍然无法让应用程序以独立模式打开。
有任何想法吗?
我的参考代码(也在GitHub 上并托管在 GitHub Pages 上):
索引.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>A2HS Test</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#0077c2"/>
<link rel="manifest" href="manifest.json">
</head>
<body>
<p>Add To Home Screen</p>
<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('sw.js')
.then(reg => console.log('Registration success. Scope is ', reg.scope))
.catch(err => console.log('Registration failed. ', err));
}
</script>
</body>
</html>
sw.js
const cacheName = 'a2hs-test';
const resourcesToCache = [
'index.html',
'manifest.json',
'icons/icon-512.png',
'icons/icon-256.png',
'icons/icon-96.png',
];
self.addEventListener('install', function(event) {
event.waitUntil(
caches.open(cacheName).then(function(cache) {
return cache.addAll(resourcesToCache);
})
);
});
self.addEventListener('fetch', function(event) {
event.respondWith(
caches.match(event.request).then(function(response) {
return response || fetch(event.request);
})
);
});
清单.json
{
"short_name": "A2HS",
"name": "Add To Home Screen",
"theme_color": "#0077c2",
"background_color": "#42a5f5",
"start_url": "index.html",
"display": "standalone",
"icons": [
{
"src": "icons/icon-96.png",
"sizes": "96x96"
},
{
"src": "icons/icon-256.png",
"sizes": "256x256"
},
{
"src": "icons/icon-512.png",
"sizes": "512x512"
}
]
}
编辑:
我在 v63 和 Canary v66 上再次遇到了类似的问题,似乎使用 localhost 导致了同样的问题 - 无法独立启动。托管完全相同的代码并访问 HTTPS 站点使我能够独立启动。