javascript - 渐进式 Web 应用程序 - Service Worker 不提供 start_URL

标签 javascript visual-studio web progressive-web-apps

正在玩渐进式网络应用程序。我试图让网络应用安装横幅工作,但我一直收到 service worker does not successfully serve the manifest's start_url在我使用 Lighthouse 调试之后(下图)。目前我正在使用 azure 托管我的网站。

新:
我检查了我所有的缓存、start_url 以及我的服务人员,以查看所有内容是否匹配。但它仍然给我同样的错误。

enter image description here

我不确定是不是我的 start_url或我的service-worker问题。下面是我的代码。
manifest.json

  {
   "short_name": "MY EXPERTS",
   "name": "MYEXPERT",
   "icons": [
    {
      "src": "Images/petronas_logo_192.png",
      "type": "image/png",
      "sizes": "192x192"
    },
    {
      "src": "Images/petronas_logo_192.png",
      "type": "image/png",
      "sizes": "512x512"
    }
 ],

 "background_color": "#FFFFFF",
 "theme_color": "#67BCFF",
 "display": "standalone",
 "start_url": "/Home/Index"
}
AddServiceWorker.js
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/service-worker.js').
    then(function (registration) {
        // Registration was successful``
        console.log('ServiceWorker registration successful with scope: ', 
registration.scope);
    }).catch(function (err) {
        // registration failed :(
        console.log('ServiceWorker registration failed: ', err);
    });
}
service-worker.js
 self.addEventListener('install', e => {
 e.waitUntil(
    caches.open('airhorner').then(cache => {
        return cache.addAll([
            '/',
            '/?utm_source=homescreen',
            '/Home/About',
            '/Home/Index',
            '/Home/Contact'
        ])
            .then(() => self.skipWaiting());
    })
  )
});

self.addEventListener('activate', event => {
 event.waitUntil(self.clients.claim());
});

self.addEventListener('fetch', event => {
  event.respondWith(
    caches.match(event.request).then(response => {
        return response || fetch(event.request);
    })
  );
});

我的浏览器缓存:

enter image description here

最佳答案

来自 Register A service worker ,

If we register the service worker file at /example/sw.js, then the service worker would only see fetch events for pages whose URL starts with /example/ (i.e. /example/page1/, /example/page2/).



内联显示的错误并在此 documentation 中给出,请检查以下内容:

  1. Define a start_url property in your manifest.json file.
  2. Ensure that your service worker properly caches a resource that matches the value of start_url.


另外,请查看 tutorial看看它是否会帮助你。

关于javascript - 渐进式 Web 应用程序 - Service Worker 不提供 start_URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46726966/

相关文章:

javascript - 如何在javascript中动态创建标签和复选框?

javascript - javascript delete object属性正在删除整个对象

c# - 使用按钮打开 Outlook session 窗口

javascript - 如何在 JavaScript 中将字符串中每个单词的第一个字母大写并将所有空格更改为下划线

c# - 如何从 C# 代码在 Visual Studio 中创建 native DLL?

c# - Azure WebApp 部署 : Throw expression causes compiler error CS1525

javascript - jQuery 计算一个 div 中的换行符数?

html - 在网站中使用 CSS 创建 "inset"效果

javascript - 在网址末尾添加特殊字符会破坏网页

javascript - jquery 空闲秒数计数器