Skip to content

Latest commit

 

History

19 Commits

Folders and files

Repository files navigation

Cache Checker

HTTP Cache Header Analyzer - checks caching headers for a URL and its assets against best practices.

Based on Jono Alderson's HTTP Caching Guide.

Write-up: I built a tool to audit HTTP cache headers — why it exists and what it catches.

Features

  • Analyzes Cache-Control, Expires, ETag, Last-Modified, Vary, Age headers
  • Extracts and checks JS, CSS, images, and fonts from HTML
  • Detects fingerprinted assets and CDN usage
  • Flags common misconfigurations (conflicting directives, missing immutable, etc.)
  • Multiple output formats: text, JSON, HTML table

Installation

As WordPress Plugin

  1. Upload the cache-checker folder to /wp-content/plugins/
  2. Activate the plugin through the 'Plugins' menu in WordPress

Standalone (no WordPress)

Just clone/copy the files - no installation needed.


Usage

1. WP-CLI Command

The most common way to use the tool within WordPress:

# Basic usage
wp cache-check https://example.com

# JSON output
wp cache-check https://example.com --format=json

# Skip asset checking (faster, main page only)
wp cache-check https://example.com --no-assets

# Limit number of assets
wp cache-check https://example.com --max-assets=20

# Filter by issue level
wp cache-check https://example.com --level=warning

# Protocol can be omitted (defaults to https)
wp cache-check example.com

Options:

Option Description
--format=<format> Output format: text (default), json, table
--no-assets Skip checking assets, analyze main page only
--max-assets=<num> Maximum assets to check (default: 100)
--level=<level> Minimum issue level: error, warning, info

2. WordPress Plugin (PHP)

Use the CacheChecker class directly in your code:

use CacheChecker\CacheChecker;
use CacheChecker\Report;

// Create checker with options
$checker = new CacheChecker([
    'timeout'      => 30,      // Request timeout in seconds
    'max_assets'   => 50,      // Maximum assets to check
    'check_assets' => true,    // Whether to check assets
]);

// Analyze a URL
$result = $checker->analyze('https://example.com');

// Create a report
$report = new Report($result);

// Output formats
echo $report->to_text();           // Human-readable text
echo $report->to_json();           // JSON
echo $report->to_html();           // HTML table

// Check for issues
if ($report->has_issues('error')) {
    // Has errors
}
if ($report->has_issues('warning')) {
    // Has warnings or errors
}

// Get raw data
$data = $report->to_array();

3. Standalone Script (no WordPress)

For use outside WordPress:

# Basic usage
php standalone.php https://example.com

# JSON output
php standalone.php https://example.com --json

# Protocol can be omitted
php standalone.php example.com

Exit codes:

  • 0 - No errors found
  • 1 - Errors found (or invalid URL)

Cloudflare Worker Mode

For high-volume checking or to avoid IP-based rate limiting, offload the analysis to a Cloudflare Worker.

Benefits

  • Distributed IPs: Requests come from Cloudflare's IP ranges
  • No origin load: Your server doesn't perform HTTP requests
  • Global edge: Checks run from nearest Cloudflare location
  • Scalable: Handle many concurrent checks

Setup

cd worker
npm install
npm run login
npm run deploy

After deployment, you'll get a URL like:

https://cache-checker.your-subdomain.workers.dev

Protect with API Key (recommended)

npx wrangler secret put API_KEY
# Enter a strong random key when prompted

Using the Worker

Via curl:

# Without API key (dev mode)
curl "https://cache-checker.your-subdomain.workers.dev/analyze?url=example.com"

# With API key (header)
curl -H "X-API-Key: your-key" \
  "https://cache-checker.your-subdomain.workers.dev/analyze?url=example.com"

# With API key (query param)
curl "https://cache-checker.your-subdomain.workers.dev/analyze?url=example.com&api_key=your-key"

Via PHP (WorkerClient):

use CacheChecker\WorkerClient;
use CacheChecker\Report;

$client = new WorkerClient(
    'https://cache-checker.your-subdomain.workers.dev',
    'your-api-key'  // Leave empty if no API_KEY secret is set
);

// Check worker health
if ($client->health_check()) {
    // Analyze URL
    $result = $client->analyze('https://example.com', true, 50);

    if (!is_wp_error($result)) {
        $report = new Report($result);
        echo $report->to_text();
    }
}

Health check (no auth required):

curl https://cache-checker.your-subdomain.workers.dev/health

Worker NPM Scripts

npm run dev          # Local dev server
npm run deploy       # Deploy to Cloudflare
npm run tail         # Stream real-time logs
npm run list         # List deployments
npm run rollback     # Rollback to previous version

See worker/README.md for full Wrangler CLI documentation.


Architecture

PHP Classes

Class Description
CacheChecker Main orchestrator - fetches URL/assets and runs analysis
HeaderParser Parses HTTP headers into structured format
AssetExtractor Extracts JS/CSS/image/font URLs from HTML
RuleEngine Checks headers against best practice rules
Report Formats results for output
ResourceType Resource type constants (HTML, JS, CSS, etc.)
WorkerClient Client for calling Cloudflare Worker API

JavaScript (Worker)

The worker contains JavaScript ports of the PHP classes:

  • header-parser.js
  • asset-extractor.js
  • rule-engine.js
  • resource-type.js

Issues Detected

Each issue carries a source: guide when the rule comes from Jono Alderson's HTTP caching guide (checked against the 29 Aug 2025 version), spec when it goes beyond the guide and rests on the HTTP/Fetch specs.

Errors (critical)

  • no-store on static assets
  • no-store with max-age (conflicting)
  • public and private together
  • Vary: * (uncacheable)
  • immutable on HTML
  • Missing max-age on fingerprinted assets

Warnings

  • No Cache-Control and no Expires: caches guess (heuristic freshness)
  • private together with s-maxage (s-maxage can never apply)
  • Set-Cookie on a response CDNs may cache (spec; CDN edge cookies such as Cloudflare's __cf_bm are ignored)
  • Missing immutable on fingerprinted assets
  • Short max-age on fingerprinted assets
  • Vary: User-Agent / Vary: Cookie (cache fragmentation)
  • Missing validators (ETag/Last-Modified) on HTML
  • private on static assets
  • Missing CORS on a font served from another host (spec; same-host fonts don't need it)
  • Uncached 301 redirects
  • CDN not caching a static asset: only when a second request is still MISS/BYPASS/DYNAMIC (one MISS is a cold edge)

Info

  • Only the legacy Expires sets freshness
  • Missing Date header
  • no-store on HTML (keeps the page out of the back/forward cache)
  • Short max-age on an unversioned static asset (the guide has no recipe for these, so it's a hint, not a warning)
  • Missing stale-while-revalidate on HTML
  • Missing stale-if-error on API
  • Missing s-maxage behind CDN
  • Uncached temporary redirects

What counts as fingerprinted

Following the guide, a fingerprint is a content hash in the filename (app.9f2d1.js, Vite/Astro about.B1Y0NK_Y.css). Query-string versions (?ver=, ?v=) don't count. Base64-style hashes need a digit, so names like SemiBold or EBGaramond aren't mistaken for hashes; digit-free hashes (about 1 in 4 Vite hashes) are missed, which only makes the checks more lenient.


Output Example

=== HTTP Cache Header Analysis ===
URL: https://example.com
Analyzed: 2024-01-15T10:30:00+00:00

--- Summary ---
Total assets checked: 25
Issues found: 3 (Errors: 0, Warnings: 2, Info: 1)

--- Main Page ---
[HTML] https://example.com
  Cache-Control: max-age=60, stale-while-revalidate=86400
  CDN: Cloudflare (status: HIT)

--- Assets with Issues ---
[JS] [fingerprinted] https://example.com/app.a1b2c3d4.js
  Cache-Control: max-age=3600
  Issues:
    ⚠ Missing immutable directive on fingerprinted asset
      Fingerprinted assets should use immutable to prevent revalidation on reload.

--- Assets OK (24) ---
  ✓ [CSS] https://example.com/style.min.css
  ✓ [JS] https://example.com/vendor.js
  ...

Testing

Rule tests (both engines)

tests/fixtures.json holds the shared cases; both engines must pass all of them, so the PHP and Worker rules can't drift apart again:

php tests/run.php
node tests/run.mjs

Add a case there whenever a rule changes.

Test PHP Implementation

# Via standalone script
php standalone.php https://example.com

# Via WP-CLI
wp cache-check https://example.com

Test Cloudflare Worker

# Health check
curl https://cache-checker.your-subdomain.workers.dev/health

# Analyze endpoint
curl "https://cache-checker.your-subdomain.workers.dev/analyze?url=example.com"

# Or use the test script
php test-worker.php https://example.com

License

GPL v2 or later

Releases

Packages

Contributors

Languages