簡體   English   中英

即使我有一個 Service Worker 並且我的 PWA 工作正常,也沒有檢測到匹配的 Service Worker

[英]No matching service worker detected even though I have a service worker and my PWA is working fine

我一直在研究一個用 HTML、CSS 和 JS 編寫的項目,並在屏幕上隨機顯示不同的視頻。 我希望它成為 PWA,所以它做到了。 它工作得很好,但是當我打開 Chrome DevTools 和“應用程序”頁面時,在“清單”部分它會發出警告:“沒有檢測到匹配的服務工作者。您可能需要重新加載頁面,或檢查服務工作者是否為當前頁面還控制清單中的起始 URL。”

我對服務工作者知之甚少,我想知道它為什么有效,但似乎沒有。 另一個問題是我從來沒有彈出“添加到主屏幕”。 但是如果我手動添加它,它就完美無缺。 它也可以離線使用。

網站鏈接: https : //ondersumer07.github.io/vinematik/

我的 main.js 中的 Service Worker 代碼:

window.addEventListener("load", () => {
  registerSW();
});

async function registerSW() {
  if ('serviceWorker' in navigator) {
    try {
      await navigator.serviceWorker.register('./sw.js');
    } catch (e) {
      console.log(`SW registration failed`);
    }
  }
}

我的 sw.js:

//CACHING FILES//
const filesToCache = [
  './',
  './offline.html',
  './css/offline.css',
];

const staticCacheName = 'pages-cache-v2';

self.addEventListener('install', event => {
  console.log('Attempting to install service worker and cache static assets');
  event.waitUntil(
    caches.open(staticCacheName)
    .then(cache => {
      return cache.addAll(filesToCache);
    })
  );
});

//LOADING FILES WHEN OFFLINE//
self.addEventListener('fetch', event => {
  console.log('Fetch event for ', event.request.url);
  event.respondWith(
    caches.match(event.request)
    .then(response => {
      if (response) {
        console.log('Found ', event.request.url, ' in cache');
        return response;
      }
      console.log('Network request for ', event.request.url);
      return fetch(event.request)

        .then(response => {
          // TODO 5 - Respond with custom 404 page
          return caches.open(staticCacheName).then(cache => {
            cache.put(event.request.url, response.clone());
            return response;
          });
        });


    }).catch(error => {
      console.log('Error, ', error);
      return caches.match('./offline.html');
    })
  );
});
// TODO 6 - Respond with custom offline page

self.addEventListener('activate', event => {
  console.log('Activating new service worker...');

  const cacheWhitelist = [staticCacheName];

  event.waitUntil(
    caches.keys().then(cacheNames => {
      return Promise.all(
        cacheNames.map(cacheName => {
          if (cacheWhitelist.indexOf(cacheName) === -1) {
            return caches.delete(cacheName);
          }
        })
      );
    })
  );
});

我的網絡清單:

{
  "short_name": "Vinematik",
  "name": "Vinematik",
  "description": "Rastgele ve sınırsız vine izleme uygulaması!",
  "icons": [
    {
      "src": "images/android-chrome-192x192.png",
      "type": "image/png",
      "sizes": "192x192"
    },
    {
      "src": "images/android-chrome-512x512.png",
      "type": "image/png",
      "sizes": "512x512"
    },
    {
      "src": "images/apple-touch-icon.png",
      "type": "image/png",
      "sizes": "180x180"
    },
    {
      "src": "images/favicon-16x16.png",
      "type": "image/png",
      "sizes": "16x16"
    },
    {
      "src": "images/favicon-32x32.png",
      "type": "image/png",
      "sizes": "32x32"
    }
  ],
  "start_url": "https://ondersumer07.github.io/vinematik/",
  "background_color": "#00adb5",
  "display": "standalone",
  "scope": "/",
  "theme_color": "#00adb5"
}

解決方案是我沒有使manifest.json 中scopestart_url相同,這需要是網站地址。 所以我把代碼改成:

  "start_url": "https://ondersumer07.github.io/vinematik/",
  "background_color": "#00adb5",
  "display": "standalone",
  "scope": "https://ondersumer07.github.io/vinematik/",
  "theme_color": "#00adb5"

問題就解決了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM