javascript - 在 Cache API 中,使用 caches.match(event.request) 和 caches.match(event.request.url) 有什么区别
问题描述
我正在与服务人员一起工作,我有以下代码
self.addEventListener('fetch', function (event) {
const url = new URL(event.request.url)
if (event.request.mode == "navigate") {
const fetchData = fetch(event.request).catch(function () {
//console.log("errr")
return caches.match("/core/index.php/offline_controlador/index")
});
event.respondWith(fetchData)
return;
}
event.respondWith(
caches.match(event.request).then(function (response) {
return response || fetch(event.request);
})
);
});
当我试图从缓存中获取这些文件时,它不起作用,但是当我将代码更改为
event.respondWith(
caches.match(event.request.url).then(function(response) {
return response || fetch(event.request);
})
);
代替
event.respondWith(
caches.match(event.request).then(function(response) {
return response || fetch(event.request);
})
);
它完美无缺
解决方案
Cache Storage API 规范的相关部分是5.4.2。(虽然这适用于对象的matchAll()
方法,Cache
但当你这样做时,这最终被称为“幕后” caches.match()
。
与您的问题特别相关的是第 2 步:
如果可选参数 request 没有被省略,那么:
如果
request
是一个Request
对象,那么:
设置
r
为request
的请求。如果
r
' 的方法不是GET
并且options.ignoreMethod
为假,则返回一个用空数组解析的承诺。否则,如果 request 是一个字符串,那么:
- 设置为调用as 构造函数
r
的初始值并作为其参数的结果的关联请求。如果这引发异常,则返回一个因该异常而被拒绝的承诺。Request
request
总而言之,最大的区别在于,如果传入一个Request
对象作为第一个参数可能会导致match()
失败,如果Request
有 amethod
以外的东西'GET'
。也许这就是你遇到的。
除此之外,这两种方法应该基本相同,尽管传入 aRequest
稍微更有效,因为浏览器不必Request
基于字符串隐式创建对象。
推荐阅读
- reactjs - MUI-Datatables 在表标题旁边添加一个按钮
- javascript - 如何通过 Python 使用 Selenium 在 https://www.expedia.co.in/ 内的签入和签出字段中发送不同的日期
- c# - 如何将数据从控制器异步发送到剃须刀页面?
- swift - 当我向下滚动 viewController 摇晃
- apache - 无法从界面文件夹加载 php
- c++ - CreateRemoteThread 成功,但某些目标应用的 LoadLibrary 失败
- reactjs - 是否可以访问 DOM 并在反应中通过渲染方法对其进行操作?
- javascript - Redux 状态更改后 React 组件不会重新渲染
- string - Powershell替换位置x处的字符
- swift - 有没有办法在没有 AppGroups 的情况下将 Firebase 与 ShareExtension 一起使用