1

我创建了一个 Cloudflare 页面规则来缓存静态页面,如下所示:

domain.com/
Cache Level: cache everything
Edge Cache TTL: 2 hours

一般是没有问题的,但是服务器多次返回错误,或者连接超时,Cloudflare 已经缓存了这个结果。如何在缓存页面之前测试有效响应?

4

1 回答 1

1

在进行任何缓存之前,您应该能够检查响应的状态。

如果状态码在 200 到 299 之间,response.ok返回 true。您可以直接使用response.status查询状态

const response = await fetch(event.request);
if(response.ok) {
    // ... do caching logic
}
于 2019-08-11T04:06:33.037 回答