在使用 Cloudflare 页面规则缓存之前验证响应



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

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

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

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

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

const response = await fetch(event.request);
if(response.ok) {
// ... do caching logic
}

最新更新