Skip to content

Conversation

@ascorbic
Copy link
Contributor

@ascorbic ascorbic commented Oct 15, 2025

Summary

A platform-agnostic route caching API for Astro SSR pages that enables declarative cache control using web standards.

Examples

Basic route caching

---
// src/pages/products/[id].astro
import { getEntry } from 'astro:content';
const product = await getEntry('products', Astro.params.id);
Astro.cache({
  lastModified: product.updatedAt,
  maxAge: 300,  // Cache for 5 minutes
  swr: 3600,    // Stale-while-revalidate for 1 hour
  tags: ['products', `product:${product.id}`]
});
---
<h1>{product.data.name}</h1>
<p>{product.data.description}</p>

Automatic dependency tracking

// src/pages/api/revalidate.ts
import { getLiveEntry } from "astro:content";

export const POST: APIRoute = async ({ cache, request }) => {
  const { id } = await request.json();
  const { entry } = await getLiveEntry("products", id);

  // Invalidate all pages that depend on this entry
  cache.invalidate(entry);

  return Response.json({ ok: true });
};

Links

@ascorbic ascorbic mentioned this pull request Oct 15, 2025
Added cache configuration for routes in astro.config.ts and updated cache invalidation tag in webhook API.
@wildfiremedia
Copy link

I’d like to see a summary of the total cache along with a list of tags, so we can get an idea of what’s being cached or invalidated and catch any tags that might have been missed which allows us to easily debug.

@florian-lefebvre
Copy link
Member

I'm not sure that's something for Astro to provide, feels like maybe it should be achievable through the driver platform (eg. cloudflare)?

@wildfiremedia
Copy link

wildfiremedia commented Oct 21, 2025

Exclusively for self-hosted servers, these metrics (memory, SWR settings, etc.) are provided in JSON format. I think they could be useful for storefronts that need to cache product listings.

Other ideas, can it cache payload in gzipped or brotli? Thinking by 50% or more is worth saving and reduce latency.

@wildfiremedia
Copy link

wildfiremedia commented Oct 23, 2025

My other thought is that if we could set a max memory limit, it might help prevent the server from starving or running out of memory when a malicious or heavy request comes in. Should it only cache those with HTTP respond code 200?

https://example.com?test=1
https://example.com?test=...
https://example.com?test=1000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants