javascript - 使用响应类型“cors”在缓存中创建响应
问题描述
我在更新缓存存储中的缓存以在应用程序离线时反映我们的更改时遇到问题,因为我无法创建具有“cors”类型的响应对象,该对象是应用程序在线时缓存的对象。
我需要用 a 更新以前的缓存响应,因为应用程序在离线Response-Type: cors
时尝试再次获取请求时会查找具有相似属性的请求-响应对。GET
我尝试使用cache.put(req, res)
. 但是,我不能在程序上创建一个响应类型为“cors”的响应对象,因为它只创建“默认”,除非通过它似乎产生的实际网络调用。
Response-Type: cors
有什么方法可以使用或更新现有的缓存主体来制作 Response 对象?
我尝试对extends
Response 对象启用修改Response-Type
这是更新缓存的代码:
// Extend Response to use Response-Type cors
class CacheResponse extends Response {
type = "cors";
url = "https://localhost:5001/api/Customer?id=e2b9b098-2fd8-423b-8928-e234d80d39ed";
}
// PUT the cache
caches.open("ngsw:1:data:dynamic:api-customer:cache").then((cache) => {
let req = new Request("https://localhost:5001/api/Customer?id=e2b9b098-2fd8-423b-8928-e234d80d39ed", {
method: "GET",
mode: undefined,
headers: {
'Accept': "application/json",
'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36",
'Origin': 'https://localhost:9001',
'Referer': 'https://localhost:9001/app/customers/e2b9b098-2fd8-423b-8928-e234d80d39ed'
}
})
let res = new CacheResponse(
JSON.stringify({
"id": "e2b9b098-2fd8-423b-8928-e234d80d39ed",
"name": "Alvin Long",
}), {
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Access-Control-Allow-Origin': '*',
'Date': 'Fri, 05 Jul 2019 14:47:27 GMT',
'Server': 'Kestrel',
'Transfer-Encoding': 'chunked',
},
type: "cors"
});
cache.put(req, res)
});
这将创建另一个缓存,除了Response-Type
解决方案
推荐阅读
- django - 无法从 django 模板中的表单中获取数据?
- android - 如何在android中使用TextInputLayout
- javascript - JS Class:在类中创建变量而不被构造函数调用的最聪明的方法
- python - Python SQLAlchemy在给定时间戳之前获取最后一个条目(按时间戳)
- angular - Mat-Button Error: mat-form-field must contain a MatFormFieldControl
- python - 为 Scipy 的 cdist(或 pdist)使用具有自定义功能的附加 kwargs?
- css - Bootstrap 4 Img + 跨度对齐
- c++ - 为什么我的程序会泄漏内存?(在 C++ 中使用树)
- python - 通过重复日期 + uniqueID 在 Pandas 中合并 Intervalindex
- wordpress - 如何将媒体文件附加到 woocommerce 订单?