Check Cloudflare Cache Status

Check if a resource is served from Cloudflare's cache by inspecting the CF-Cache-Status header.

Code

General
status=$(curl -sI "$url" | grep -i "cf-cache-status:" | cut -d' ' -f2 | tr -d '\r')
echo "${status:-No CF-Cache-Status header}"

Parameters

URL to check

Server

CF-Cache-Status Values

StatusMeaning
HITServed from Cloudflare cache
MISSNot in cache, fetched from origin
EXPIREDWas cached but has expired, refetched
STALEServed stale while revalidating
DYNAMICNot eligible for caching (e.g., POST requests, dynamic content)
BYPASSCache bypassed due to configuration

Real-World Usage

check_cf_cache() {
    local url="$1"
    local headers=$(curl -sI "$url")
    local status=$(echo "$headers" | grep -i "cf-cache-status:" | cut -d' ' -f2 | tr -d '\r')
    local age=$(echo "$headers" | grep -i "^age:" | cut -d' ' -f2 | tr -d '\r')

    echo "cached: $([ "$status" = "HIT" ] && echo "true" || echo "false")"
    echo "status: ${status:-null}"
    echo "age: ${age:-null}"
}

Testing with httpbin

Use httpbin to simulate different cache statuses:

  • https://httpbin.org/response-headers?CF-Cache-Status=HIT
  • https://httpbin.org/response-headers?CF-Cache-Status=MISS
  • https://httpbin.org/response-headers?CF-Cache-Status=EXPIRED

More Bash Snippets