This site is not affiliated with or endorsed by Cloudflare, Inc. It showcases deployable experiments across most Cloudflare products.
Edge Platform

Edge Cache

Fetch and cache URLs with the Workers Cache API at the edge - Cache API reference pattern for Cloudflare Workers

Fetch external URLs through the Workers Cache API and report whether each response was served from cache, fetched from origin, or bypassed.

API Reference

GET /fetch

Fetches a URL and returns cache metadata without streaming the full response body back to the client.

Prop

Type

Example Request

curl "https://your-worker.workers.dev/fetch?url=https://www.cloudflare.com"

Success Response

url string

The normalized URL that was fetched

cacheStatus string

One of "HIT", "MISS", or "BYPASS"

statusCode number

HTTP status code from the cached or origin response

contentType string | null

Response Content-Type header, if present

bodySize number

Response body size in bytes

{
  "url": "https://www.cloudflare.com/",
  "cacheStatus": "MISS",
  "statusCode": 200,
  "contentType": "text/html; charset=utf-8",
  "bodySize": 125678
}

Error Response

{
  "error": "Missing or invalid query parameter: url",
  "code": "INVALID_URL"
}

Error Codes

  • 400 - Missing or invalid url parameter (not http/https)
  • 502 - Upstream fetch failed (FETCH_ERROR)

Use Cases

  • Learn how to use caches.default in Workers
  • Cache expensive origin fetches at the edge
  • Compare cache hit vs miss behavior during development
  • Build read-through cache patterns for APIs and static content

Limitations

  • Uses the default cache with no custom TTL or cache-key logic in this demo
  • Cache is per-colo; entries are not globally consistent
  • Only caches successful GET fetch responses for the requested URL

Use in your project

Copy these files into an existing Worker. Prefer Deployment to try the full experiment first. Source: apps/experiments/edge-cache.

DependenciesNoneBindingsNonePlatformCache API (`caches.default`), Fetch API
import { CACHE_MAX_AGE_SECONDS } from "../constants/defaults";import type { FetchResponse } from "../types/fetch";type CachedFetchOptions = {  url: string;  cache: Cache;  bypass: boolean;};export async function fetchWithEdgeCache(options: CachedFetchOptions): Promise<FetchResponse> {  const { url, cache, bypass } = options;  const cacheRequest = new Request(url, { method: "GET" });  if (!bypass) {    const cached = await cache.match(cacheRequest);    if (cached) {      const body = await cached.arrayBuffer();      return {        url,        cacheStatus: "HIT",        statusCode: cached.status,        contentType: cached.headers.get("content-type"),        bodySize: body.byteLength,      };    }  }  const response = await fetch(url);  const body = await response.arrayBuffer();  const contentType = response.headers.get("content-type");  if (!bypass && response.ok) {    const toCache = new Response(body, {      status: response.status,      headers: {        "content-type": contentType ?? "application/octet-stream",        "cache-control": `public, max-age=${CACHE_MAX_AGE_SECONDS}`,      },    });    await cache.put(cacheRequest, toCache);  }  return {    url,    cacheStatus: bypass ? "BYPASS" : "MISS",    statusCode: response.status,    contentType,    bodySize: body.byteLength,  };}

Deployment

Click the deploy button

Deploy to Cloudflare Workers

Deploy

No additional configuration required.

Test your deployment

curl "https://your-worker.workers.dev/fetch?url=https://www.cloudflare.com"

Call the same URL again to see "cacheStatus": "HIT".

Local Development

cd apps/experiments/edge-cache
npm install
npm run dev

Test locally:

curl "http://localhost:8787/fetch?url=https://www.cloudflare.com"

Cloudflare Features Used

  • Workers - Edge compute runtime
  • Cache API - Edge response caching via caches.default
  • Fetch API - Origin requests from Workers

On this page