Check if a resource is served from Cloudflare's cache by inspecting the CF-Cache-Status header.
Code
Generalconst response = await fetch(url);
return response.headers.get('CF-Cache-Status') || 'No CF-Cache-Status header';Parameters
URL to check
Browser·fetch() may be limited by CORS
CF-Cache-Status Values
| Status | Meaning |
|---|---|
HIT | Served from Cloudflare cache |
MISS | Not in cache, fetched from origin |
EXPIRED | Was cached but has expired, refetched |
STALE | Served stale while revalidating |
DYNAMIC | Not eligible for caching (e.g., POST requests, dynamic content) |
BYPASS | Cache bypassed due to configuration |
Real-World Usage
async function checkCFCache(url) {
const response = await fetch(url);
const status = response.headers.get('CF-Cache-Status');
const age = response.headers.get('Age');
return {
cached: status === 'HIT',
status,
age: age ? parseInt(age) : null
};
}
Testing with httpbin
Use httpbin to simulate different cache statuses:
https://httpbin.org/response-headers?CF-Cache-Status=HIThttps://httpbin.org/response-headers?CF-Cache-Status=MISShttps://httpbin.org/response-headers?CF-Cache-Status=EXPIRED
More JavaScript Snippets
Add Query Parameter
Add or update a query parameter in a URL string.
Bearer Token Authentication
Make an authenticated HTTP request using Bearer token for JWT or OAuth.
CORS Cross-Origin Request
Make a cross-origin HTTP request with CORS headers inspection.
Delayed Response (Test Timeouts)
Request a delayed response to test timeout handling with AbortController.
Download Binary File
Download an image or binary file and convert it to a blob or base64.
Fetch Error Details
Extract status, headers and body from an HTTP error response.