简体   繁体   English

服务人员错误的缓存文件

[英]service worker wrong caching file

I put service worker on my website but something goes wrong, i only put 3 files to cache in my service-worker.js我将 service worker 放在我的网站上但出了点问题,我只在 service-worker.js 中缓存了 3 个文件

const CACHE_NAME = "firstpwav1";
var urlsToCache = [
 "http://localhost/dev/assets/js/jquery/jquery.min.js",
 "http://localhost/dev/assets/images/my-logo.png",
 "http://localhost/dev/assets/images/loader.gif"
 ];

self.addEventListener("install", function(event) {
 event.waitUntil(
   caches.open(CACHE_NAME).then(function(cache) {
     return cache.addAll(urlsToCache);
   })
     );
   });

    self.addEventListener("fetch", function(event) {
     event.respondWith(
       caches
         .match(event.request, { cacheName: CACHE_NAME })
      .then(function(response) {
        if (response) {
          console.log("ServiceWorker: Using cache from: ", response.url);
          return response;
        }

        console.log(
          // "ServiceWorker: Load from server",
          // event.request.url
        );
        return fetch(event.request);
      })
  );
});


self.addEventListener("activate", function(event) {
  event.waitUntil(
    caches.keys().then(function(cacheNames) {
      return Promise.all(
        cacheNames.map(function(cacheName) {
          if (cacheName != CACHE_NAME) {
            console.log("ServiceWorker: cache " + cacheName + " deleted");
            return caches.delete(cacheName);
          }
        })
      );
    })
  );
});

but after i reload the page and inspect the network tab:但是在我重新加载页面并检查网络选项卡后: 在此处输入图像描述

others file appear twice and each one of them loaded from service worker although i did not cache it in my service-worker.js, is it something wrong with my service-worker.js?其他文件出现两次,每个文件都从服务工作者加载,虽然我没有将它缓存在我的 service-worker.js 中,但我的 service-worker.js 有问题吗?

Document said it browser will compare your cached file and the file get from networks: Related Documentation文档说它浏览器会比较你的缓存文件和从网络获取的文件: 相关文档

If identical, browser serving from cache.如果相同,则浏览器从缓存中提供服务。 If not, browser will cache the latest file from network.如果没有,浏览器将缓存来自网络的最新文件。

So it will always have 2 request in dev-tool as you see.因此,如您所见,它在开发工具中总是有 2 个请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM