# Cloudflare Worker

This page explains how `worker/index.js` caches images at the Cloudflare edge and what to change before deploying it.

## Before Deploying

`handler` builds the origin URL with `new URL(url.pathname, "[URL]")`. `[URL]` is a placeholder: replace it with the image server origin (for example `https://img-origin.example.com`) before deploying, otherwise `new URL` throws.

## Scope

| Path extension | Behavior |
|---|---|
| `.jpg`, `.jpeg`, `.png`, `.webp`, `.svg` (case-insensitive) | Cache and fetch from origin |
| Anything else (including `.avif`, `.pdf`, no extension) | Returns `400` with body `400` |

The check uses the **original path** extension, not the `t` parameter; reading a `.jpg` original with `t=avif` is still handled. Uploaded PDFs cannot be read through this Worker.

## Cache Behavior

| Item | Behavior |
|---|---|
| Cache key | Origin URL including every query parameter; an `X-Custom-Cache-Key` header records the query |
| Storage | `caches.default`, written asynchronously via `event.waitUntil(cache.put(...))` |
| Lifetime | Response rewritten to `Cache-Control: public, max-age=604800` (7 days) |
| Diagnostic headers | `CF-Cache-Status: HIT` / `MISS`, `X-Query-String` |

Origin fetches reuse the original request's method and headers, and the origin response status is not checked.

## Relationship with Nginx

The Worker fetches from Nginx; on a miss, Nginx's `proxy_cache` and the Go service take over. See [Caching Layers](/caching) for the full lookup order.
