6

我正在尝试在最新的 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 站点使我能够独立启动。

4

2 回答 2

1

根据评论,这似乎只是一个在 Chrome 63+ 中修复的错误。

编辑:

请参阅上面的编辑 - 通过 HTTPS 托管也为我在 v63 和 Canary v66 上解决了同样的问题。本地主机可能不足以让应用程序以独立模式启动。

于 2017-10-31T20:36:28.013 回答
0

我发现的一种解决方案是在远程设备下使用开发人员工具中的代理。您必须使用 USB 电缆连接您的设备并代理应用程序,这样您也可以使用 localhost。

于 2018-02-20T13:13:34.070 回答