简体   繁体   English

Service Worker 不从缓存中返回文件

[英]Service worker doesn't return file from cache

I'm trying to cache a single page webapp with a service worker.我正在尝试使用服务工作者缓存单页 Web 应用程序。 It should get all it's files from the cache and update that cache only when a new service worker-version is published.它应该从缓存中获取所有文件,并仅在发布新的服务工作者版本时更新该缓存。

With a precache function i'm writing some files to the cache as follows:使用预缓存功能,我将一些文件写入缓存,如下所示:

function precache() {
return caches.open(CACHE).then(function(cache) {
    return cache.addAll([
        'index.html',
        'js/script.js',
        'img/bg.png',
        'img/logo.svg',
        ...
    ]);
});
}

(I've tried to cache with and without "/" before the paths, and even with absolute paths. Makes no difference) (我尝试在路径前使用和不使用“/”进行缓存,甚至使用绝对路径。没有区别)

In Chrome's Cache Storage, the content of all those files is exactly as it should be.在 Chrome 的缓存存储中,所有这些文件的内容都和它应该的一样。 But when I try to serve the files from cache on reload of the page, none of the requests match with the cache, they all get rejected, even when I'm still online.但是,当我尝试在重新加载页面时从缓存中提供文件时,没有任何请求与缓存匹配,它们都被拒绝了,即使我仍然在线。

self.addEventListener('fetch', function(evt) {
    evt.respondWith(
        caches.match(evt.request).then(function(response) {
            if(response){
                return response;
            } else {
                reject('no result');
            }
        }).catch(function(){
            if(evt.request.url == 'https://myurl.com'){
                return caches.match('/index.html');
            }
        });
    )
});

The index.html from the catch-function gets served correctly, and in turn requests the other files, like /js/script.js.来自 catch 函数的 index.html 得到正确的服务,然后请求其他文件,比如 /js/script.js。 Those request show up like this in the console:这些请求在控制台中显示如下:

Request { method: 'GET', url: ' https://myurl.com/js/script.js ', ... referrer: ' https://myurl.com ' }请求 { method: 'GET', url: ' https://myurl.com/js/script.js ', ... referrer: ' https://myurl.com ' }

But they do not return a response, only a notice like this shows:但是他们没有返回响应,只有这样的通知显示:

The FetchEvent for " https://myurl.com/js/script.js " resulted in a network error response: an object that was not a Response was passed to respondWith(). https://myurl.com/js/script.js ” 的 FetchEvent 导致网络错误响应:一个不是响应的对象被传递给 respondWith()。

Am I missing something here?我在这里错过了什么吗?

Thanks to the link from Rajit https://developer.mozilla.org/en-US/docs/Web/API/Cache/match I've found that the caches.match() function accepts an options-object. 感谢Rajit的链接https://developer.mozilla.org/en-US/docs/Web/API/Cache/match我发现caches.match()函数接受了options-object。

I've updated that line in my service worker to 我在服务工作者中更新了该行

caches.match(evt.request,{cacheName:CACHE,ignoreVary:true}).then(function(response) {

so it includes the cache-name and an ignores VARY header matching, and now it returns the correct files. 所以它包含cache-name和忽略VARY头匹配,现在它返回正确的文件。

I had the same problem and it seems to have been solved by using the ignoreVary:true parameter. 我遇到了同样的问题,似乎已经通过使用ignoreVary:true参数解决了。 The documentation explicitly states that the cacheName parameter is ignored by Cache.match() 该文档明确声明Cache.match()忽略cacheName参数

Important note is to add all possible url versions, with, without trailing slash, because even when autocompleted, it seems to bee seen as two different things.重要注意事项是添加所有可能的 url 版本,带或不带尾部斜杠,因为即使自动完成,它似乎也被视为两个不同的东西。 So, for example, if you had a pwa in domain/folder/ ,因此,例如,如果您在domain/folder/中有一个 pwa,

calling domain/folder/ online and caching wont make domain/folder work offline (in some cases) unless you previously accessed the later online as well.在线调用domain/folder/缓存不会使domain/folder离线工作(在某些情况下),除非您之前也在线访问过后者。

Solution:解决方案:

when adding via caches.addAll or similar, add both通过 caches.addAll 或类似方式添加时,同时添加

'/folder/'

AND

'/folder' . '/folder'

What never did a thing for me on the other hand, was ignoreVary .另一方面,对我没有任何作用的是ignoreVary

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

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