# Caching and ISR

> Serve pages and API answers again without running your function: Incremental Static Regeneration, `s-maxage` caching, and purging by tag.

- Category: Routing and caching
- URL: https://www.forkrail.com/docs/caching

## What gets cached

The routers keep two kinds of answers and serve them again without running your function, as Vercel does. Every answer says how it was served in its `x-vercel-cache` header.

| x-vercel-cache | Meaning |
| --- | --- |
| `HIT` | Served from the cache. |
| `STALE` | Served from the cache while it’s made again in the background. |
| `MISS` | Not in the cache: the function ran. |
| `PRERENDER` | The build’s fallback page, until the page is made. |
| `REVALIDATED` | Made again on request, then served. |
| `BYPASS` | Draft Mode: the function ran and nothing was kept. |

## Pages (ISR)

Pages your framework marks for Incremental Static Regeneration (SvelteKit’s `config.isr`, Astro’s `isr`, Nitro’s `isr` route rules, Next.js’s `revalidate`) are kept until they expire, then served once more as `STALE` while the function makes them again, one at a time.

**src/routes/blog/[slug]/+page.server.ts (SvelteKit)**

```ts
export const config = {
  isr: { expiration: 60 }, // seconds
}
```

- A regeneration that fails leaves the page as it was, and is tried again after 30 seconds.
- Pages are made without the visitor’s cookies and `Authorization`, since they’re kept for everyone. Draft Mode still gets them.
- Each router keeps pages on its own disk (up to 2 GB) and the most used in memory. A page unused for 31 days is dropped.
- To make a page again now, send a GET with `x-prerender-revalidate: <the prerender’s bypass token>`.

## API and function answers

Any function can ask for its answer to be cached with `s-maxage`, as on Vercel’s CDN. The first of `Vercel-CDN-Cache-Control`, `CDN-Cache-Control` and `Cache-Control` that has one is used.

**api/prices.ts**

```ts
export async function GET() {
  return Response.json(await loadPrices(), {
    headers: { 'Cache-Control': 's-maxage=60, stale-while-revalidate=300' },
  })
}
```

That answer is kept for 60 seconds in each router’s memory, then served stale for up to 5 more minutes while one request refreshes it. Visitors get it without the `s-maxage` part.

**What can be cached:** GET and HEAD requests without `Authorization`, with status 200, 301, 302, 307, 308, 404 or 410, up to 10 MB, and without `set-cookie`, `private`, `no-cache`, `no-store` or `Vary: *`/`Vary: Cookie`. The key is the host, deployment, path and query, plus `Accept`, `Accept-Encoding` and any headers `Vary` names. POST answers are never cached.

An answer that sets no caching gets `cache-control: public, max-age=0, must-revalidate`, as on Vercel.

## Purge by tag

Tag what’s cached with a `Vercel-Cache-Tag` header (comma-separated; visitors never see it) or `addCacheTag()` from `@vercel/functions`, then purge by tag:

- **Invalidate**: tagged answers are served once more as `STALE` while they’re made again. This is the safe default.
- **Delete**: tagged answers are made again while the next visitor waits. Many paths at once can flood your functions, so prefer invalidate.
- `*` purges everything the project’s deployments cached.

**From your app**

```ts
import { invalidateByTag, dangerouslyDeleteByTag } from '@vercel/functions'

await invalidateByTag(['products', 'product-123'])
await dangerouslyDeleteByTag(['products'], { revalidationDeadlineSeconds: 3600 })
```

**From the CLI**

```bash
forkrail cache invalidate -p my-site --tag products,product-123   # --target production|preview
forkrail cache dangerously-delete -p my-site --tag products --revalidation-deadline-seconds 3600
forkrail cache purge -p my-site     # invalidate everything
forkrail cache ls my-site           # the latest purges
```

A purge from your app reaches every router in about half a round trip between regions; one from the CLI or dashboard (**Settings → Caches**) within a second. Tags are case-sensitive, up to 256 bytes each and 128 per answer.

## Cost

Caching has no fixed cost and stores nothing in S3: it lives on the routers’ own memory and disks. Every cached answer is a function run you don’t pay for, and a regeneration is one function request. **Observability → Cache** shows your hit rate.
