繁体   English   中英

从服务工作者缓存中删除特定文件?

[英]Remove a specific file from service worker cache?

我编写的代码可以返回给定缓存中不再使用的所有文件的响应,然后删除。 我已经尝试将cache.delete与来自cache.matchAll()的请求一起使用,这是返回 false 的请求。

Response {type: "basic" url: "http://127.0.0.1:5500/bundles/bundle-f6d8ec63.css" redirected: false status: 404 ok: false statusText: "Not Found" headers: Headers {} body: (...) bodyUsed: false __proto__: Response }

你可以在这里看到它确实存在于缓存中在此处输入图像描述

看起来 cache.delete 是为了删除整个缓存,但我只想删除一些 select 文件。 有没有办法做到这一点?

您可以使用cache.delete(request, options)删除缓存中的项目。

此方法在缓存中找到与请求匹配的项目,将其删除,并返回解析为 true 的 Promise。 如果它没有找到该项目,它会解析为 false。

以下是 MDN 定义的选项:

ignoreSearch:一个Boolean,指定是否忽略URL中的查询字符串。 例如,如果设置为 true 的 ?value=bar 部分

htt://foo.com/?value=bar

执行匹配时将被忽略。 它默认为假。

ignoreMethod:一个 Boolean,当设置为 true 时,会阻止匹配操作验证 Request HTTP 方法(通常只允许 GET 和 HEAD。)默认为 false。

ignoreVary: A Boolean that when set to true tells the matching operation not to perform VARY header matching — that is, if the URL matches you will get a match regardless of whether the Response object has a VARY header. 它默认为假。

cacheName:一个 DOMString,表示要在其中搜索的特定缓存。 请注意,Cache.match() 会忽略此选项。

原来我正在使用的测试本地主机(我认为它基于 npm http-server)正在搞乱缓存并且不让我删除东西。 使用py -m http.server进行测试时,我没有任何问题。

暂无
暂无

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

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