Astro · npm

Cache Astro by tag, in one line.

@canner-ca/astro-cache sets the Cache-Control and Surrogate-Key headers Canner needs — exactly right, every time — so your server-rendered pages cache on Canner and clear by tag when your CMS publishes. Zero dependencies. Works with any framework that speaks the Web Headers API.

$npm install @canner-ca/astro-cache

Use it

Call cache() in any server-rendered route with a TTL and the tags that identify the content on the page. That's the whole integration on the code side.

---
// src/pages/blog/[slug].astro
import { cache } from '@canner-ca/astro-cache';

const post = await getPost(Astro.params.slug);

cache(Astro.response, { ttl: 3600, tags: [post.id, 'blog-listing'] });
---

Which sets

Cache-Control: public, s-maxage=3600
Surrogate-Key: <post.id> blog-listing

When the post is published, your DatoCMS webhook purges its record id (Canner reads the changed record automatically) and Canner clears every page carrying that tag. Add a stable tag like blog-listing to your index page to clear it too.

Options

ttlRequired. Seconds Canner may serve the cached response — sets s-maxage.tagsString, number, or array. Sets Surrogate-Key. Numbers are coerced; duplicates and whitespace tags are dropped.browserTtlOptional. Seconds the visitor's browser may cache (sets max-age). Omit to keep tag purges instant.

What Canner caches

A response is cached only when all of these hold — the same rules this helper produces:

  • GET or HEAD, status 200
  • Cache-Control: public with a positive s-maxage (or max-age)
  • no Set-Cookie
  • Vary absent or only Accept-Encoding
  • body under 8 MB

It only ever adds headers

The helper never strips or mutates anything your app set. It does not remove Set-Cookie — Canner already declines to cache a response that sets a cookie, so if you mark such a route cacheable you get a development-only warning, and your cookie is left untouched. A bad TTL sets no headers and warns; only passing something that isn't a Response or Headers throws.

Then point your CMS at Canner

The code side is done. The other half is two copy-paste values — the webhook URL and an Authorization header — shown pre-filled in the dashboard under Settings → Caching. No request body needed; Canner reads the DatoCMS payload automatically. Full caching guide.

Source and issues: tools/astro-cache/ in the canner repo on GitHub. Licensed MIT.