首页 > 解决方案 > 从通过 CacheStorage api 通过 cache.match(req) 返回的响应中检索“时间缓存”

问题描述

我正在使用新的 CacheStorage api,并希望有一个小工作来清理高于某个 ttl(生存时间)的旧缓存条目。

在 Chrome 开发工具 - > 应用程序选项卡中,我看到一个“缓存时间”列,但无法通过 api 检索它。

const cache = await caches.open(`my-cache`);
const response = await cache.match(new Request(`https://someurl`))
console(response.cachedTime) <- // doesn't exist

我尝试通过 Date 标头获取它,但这仅适用于非 cors 请求。devtools 以某种方式跟踪所有请求。有没有办法通过 CacheStorage api 动态获取这些信息?

// this is not a bullet proof method
// doesn't work for cors or if response doesn't send a Date header
console.log(response.headers.get(`date`));

尝试转储整个响应并检查它,但我没有看到任何有用的东西。

标签: javascriptbrowsergoogle-chrome-devtools

解决方案


推荐阅读