2

我正在尝试从 Service Worker 获取缓存的文件,但在最新版本的 chrome 中未触发 Fetch 事件。而我收到“Fetch 处理程序存在:EXISTS”。这是我使用 chrome://serviceworker-internals/ 得到的响应

Scope: https://me.development.symphoni.io:8443/resources/
Registration ID: 3
Active worker:
Installation Status: ACTIVATED
Running Status: RUNNING
Fetch handler existence: EXISTS
Script: https://me.development.symphoni.io:8443/resources/swa.js
Version ID: 4
Renderer process ID: 9704
Renderer thread ID: 6912
DevTools agent route ID: 22
Log:
Console: {"lineNumber":50,"message":"Opened cache","message_level":1,"sourceIdentifier":3,"sourceURL":"https://me.development.symphoni.io:8443/resources/swa.js"}

下面是我在 Windows 加载时加载的 app-sw.js

if ('serviceWorker' in navigator) {
            debugger;
          navigator.serviceWorker.register('resources/swa.js').then(function(reg) {
            if(reg.installing) {
              console.log('Service worker installing');
            } else if(reg.waiting) {
              console.log('Service worker installed');
            } else if(reg.active) {
              console.log('Service worker active');
            }
          }).catch(function(error) {
            // registration failed
            console.log('Registration failed with ' + error);
          });
        }

下面是 swa.js 代码,其中包含 Fetch 事件和控制台日志

// Declare the Cache names with version
var CACHE_NAME = 'site-cache-v1';
debugger;
// We can have multiple sets of files to cache
var urlsToCache = [
    "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.js"
  ];


self.addEventListener('fetch', function(event) {
    debugger;
      console.log('Handli`enter code here`ng fetch event for', event.request.url);

    });

// Add
// Add or install the files into Service Workers
self.addEventListener('install', function(event) {
  event.waitUntil(
    caches.open(CACHE_NAME).then(function(cache) {
        console.log('Opened cache');
        return cache.addAll(urlsToCache);
    })
  );
});



//Update
//Delete all Service Workers except those in the whiteList
self.addEventListener('activate', function(event) {
debugger;
var cacheWhitelist =['my-site-cache-v1','pages-cache-v1', 'blog-posts-cache-v1'];
//var cacheWhitelist = ['pages-cache-v1', 'blog-posts-cache-v1'];

event.waitUntil(self.clients.claim(),
 caches.keys().then(function(cacheNames) {
   return Promise.all(
     cacheNames.map(function(cacheName) {
       if (cacheWhitelist.indexOf(cacheName) === -1) {
         return caches.delete(cacheName);
       }
     })
   );
 })
);
});
4

0 回答 0