小编use*_*988的帖子

通过cache.match(req)从通过CacheStorage api返回的响应中检索“时间缓存”

我正在使用新的 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
Run Code Online (Sandbox Code Playgroud)

我尝试通过 Date 标头获取它,但这仅适用于非 cors 请求。开发工具以某种方式跟踪所有请求。有没有办法通过 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`));
Run Code Online (Sandbox Code Playgroud)

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

javascript browser google-chrome-devtools

7
推荐指数
1
解决办法
734
查看次数