javascript React应用程序离线工作,部署后无法缓存文件

sh7euo9m  于 2023-02-15  发布在  Java
关注(0)|答案(1)|浏览(138)

我正在使react应用程序离线工作,为此我缓存的文件在sw.js文件,这是在公共文件夹下。文件缓存在本地,但当我部署应用程序,它没有缓存文件,并给出错误-未捕获(承诺)TypeError:无法对“缓存”执行“addAll”:请求失败。下面是我的sw.js文件-'let CACHE_NAME =“codePwa”;

var urlCache = \[
"/manifest.json",
"/logo192.png",
"/",
"/index.html",
"/static/js/main.4d5113ea.js",
"/static/css/main.073c9b0a.css",
"./App.js",
\];

/// install service worker
this.addEventListener("install", (event) =\> {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) =\> {
return cache.addAll(urlCache);
})
);
});

// fetch cache data

this.addEventListener("fetch", (event) =\> {
if (!navigator.onLine) {
console.log("offline");
if (event.request.url === "http://localhost:3000/static/js/main.chunk.js") {
event.waitUntil(
this.registration.showNotification("modeNet", {
body: "Offline",
icon: "http://localhost:3000/logo192.png",
})
);
}
event.respondWith(
caches.match(event.request).then((response) =\> {
if (response) {
return response;
}
let fUrl = event.request.clone();
fetch(fUrl);
})
);
}
});

this.addEventListener("activate", function (event) {
event.waitUntil(
caches.keys().then(function (cacheNames) {
return Promise.all(
cacheNames
.filter(function (cacheNames) {
//
})
.map(function (cacheNames) {
return caches.delete(cacheNames);
})
);
})
);
});`

我应该进行哪些更改,以便在部署应用后缓存文件,并在应用离线时和刷新应用后该高速缓存中提取文件。我正在Netlify上部署我的应用(url -https://calm-salmiakki-411347.netlify.app/

irlmq6kh

irlmq6kh1#

当一个或多个URL不存在并且找不到要缓存的URL时,会导致此错误
我检查了url,似乎./App.js不存在。如果它是react项目的App.js,则不需要缓存它(因为它在生产中不存在)。因此,请从urlCache数组中删除它
如果您使用缓存优先策略,则需要使用www.example.com然后使用cache.match来处理它cache.open and then cache.match
因此更改此部分:

event.respondWith(
  caches.match(event.request).then((response) =\> {
  if (response) {
    return response;
  }
  let fUrl = event.request.clone();
  fetch(fUrl);
})
);

改为:

event.respondWith(
  caches.open(cacheName)
    .then(function(cache) {
      cache.match(event.request)
        .then( function(cacheResponse) {
          if(cacheResponse)
            return cacheResponse
          else
            return fetch(event.request)
              .then(function(networkResponse) {
                cache.put(event.request, networkResponse.clone())
                return networkResponse
              })
        })
    })
)

为了更有条理我重写了你的代码

let CACHE_NAME = "codePwa";

var urlCache = [
    "./manifest.json",
    "./logo192.png",
    "./",
    "./index.html",
    "./favicon.ico",
    "./static/js/main.4d5113ea.js",
    "./static/js/bundle.js",
    "./static/css/main.073c9b0a.css",
    "/about",
    "/contact",
];
self.addEventListener('install', function (evt) {
    console.log('The service worker is being installed.');
    evt.waitUntil(precache());
});

self.addEventListener('fetch', function (evt) {

    if (!navigator.onLine) {
        console.log("offline");
        if (evt.request.url === "http://localhost:3000/static/js/main.chunk.js") {
            evt.waitUntil(
                this.registration.showNotification("modeNet", {
                    body: "Offline",
                    icon: "http://localhost:3000/logo192.png",
                })
            );
        }
    }

    evt.respondWith(fromCache(evt.request));
    if(navigator.onLine)
    evt.waitUntil(update(evt.request));
});

function precache() {
    return caches.open(CACHE_NAME).then(function (cache) {
        return cache.addAll(urlCache);
    });
}

function fromCache(request) {
    return caches.open(CACHE_NAME).then(function (cache) {
        return cache.match(request).then(function (matching) {
            return matching || Promise.reject('no-match');
        });
    });
}

function update(request) {
    return caches.open(CACHE_NAME).then(function (cache) {
        return fetch(request).then(function (response) {
            return cache.put(request, response);
        });
    });
}

self.addEventListener('activate', evt =>
    evt.waitUntil(
        caches.keys().then(cacheNames => {
            return Promise.all(
                cacheNames.map(cacheName => {
                    if (cacheName !== CACHE_NAME) {
                        return caches.delete(cacheName);
                    }
                })
            );
        })
    )
);

相关问题