清单:无添加到主屏幕

时间:2019-05-16 02:41:15

标签: javascript php manifest service-worker progressive-web-apps

我试图显示添加到主屏幕的横幅,我安装了service-worker和manifest,尝试了this,但没有显示横幅。

这是我的代码:

index.php

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<link rel="manifest" crossorigin="use-credentials"  href="manifest.json">
</head>
<body>

<h1>This is a Heading</h1>
<p>This is a paragraph.</p>

</body>

<script>
 if ('serviceWorker' in navigator) {
    console.log("Will the service worker register?");
    navigator.serviceWorker.register('service-worker.js')
      .then(function(reg){
        console.log("Yes, it did.");
     }).catch(function(err) {
        console.log("No it didn't. This happened:", err)
    });
 }
</script>

</html>

manifest.json

{
    "short_name": "BetaPage",
    "name": "BetaPage",
    "theme_color": "#4A90E2",
    "background_color": "#F7F8F9",
    "display": "standalone",
    "icons": [
      {
        "src": "img/apple.png",
        "type": "image/png",
        "sizes": "48x48"
      },
      {
        "src": "img/grapes.png",
        "type": "image/png",
        "sizes": "96x96"
      },
      {
        "src": "img/lemon.png",
        "type": "image/png",
        "sizes": "144x144"
      },
      {
        "src": "img/orange.png",
        "type": "image/png",
        "sizes": "192x192"
      }
    ],
    "start_url": "index.php"
  }

service-worker.js

self.addEventListener('install', function(event) {
    console.log("installed");
});

self.addEventListener('activate', function(event) {
      // Perform some task
    console.log("activated");
    });

self.addEventListener('fetch', function(event) {
    console.log("fetched");
      event.respondWith(
        fetch(event.request).catch(function() {
          return caches.match(event.request);
        })
      );
    });

但是仍然没有显示横幅,我的清单上没有错误。 enter image description here

这是我的服务工

enter image description here

我该如何解决?我目前正在本地主机上执行此操作。

更新 好的,我设法使用本指南https://codelabs.developers.google.com/codelabs/add-to-home-screen/#0

我现在的问题是在Firefox中没有显示提示。 下图既是Firefox,又是可工作的,也是我的本地主机。

工作示例 enter image description here

我的工作 enter image description here

如您所见,另一个有另一个图标,而我的作品没有,但是横幅可以在chrome上使用

2 个答案:

答案 0 :(得分:0)

看看这篇文章:PWA Tips and Tricks

答案 1 :(得分:0)

将lighthouse插件安装到Chrome中,然后在您的页面上运行它并查看结果。