#Cache method?

4 messages · Page 1 of 1 (latest)

old flicker
#

Running on Cloudflare workers in SSR mode

I have 3 Astro pages, and in front matter of all of them, I get the userID based on the URL, and call an async method to get the users first name from the database.

This makes pages not change instantaneously.

How can I resolve this?
Can I cache locally?

halcyon girderBOT
#

Hi @old flicker. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Astro’s docs don’t describe a built‑in “local cache” for arbitrary async calls in SSR on Cloudflare Workers, so I can’t point you to an official per‑request or in‑memory cache API.

However, there is an experimental mechanism for caching content loader results via “cache hints”. If your user data were coming from an Astro live content collection (via a custom loader), you could:

  1. Add cache metadata (tags, lastModified, etc.) in your loader’s return value.
  2. Read those hints in your page and set HTTP cache headers (e.g. Cache-Control, Cache-Tag, Last-Modified) so your CDN/edge cache can serve responses faster. [Live collections]

Example from the docs:

// my-loader.ts
import type { LiveLoader } from "astro/loaders";

export function myLoader(config): LiveLoader<MyData> {
  return {
    name: 'cached-loader',
    loadEntry: async ({ filter }) => {
      const item = await loadStoreProduct(filter);
      return {
        id: item.id,
        data: item,
        cacheHint: {
          lastModified: new Date(item.lastModified),
          tags: [`product-${item.id}`, `category-${item.category}`],
        },
      };
    },
  }
}
---
// page.astro
export const prerender = false;
import { getLiveEntry } from 'astro:content';

const { entry, error, cacheHint } = await getLiveEntry('products', Astro.params.id);
if (error) {
  return Astro.redirect('/404');
}

if (cacheHint?.tags) {
  Astro.response.headers.set('Cache-Tag', cacheHint.tags.join(','));
}
if (cacheHint?.lastModified) {
  Astro.response.headers.set('Last-Modified', cacheHint.lastModified.toUTCString());
}
---
<h1>{entry.data.name}</h1>
#

These hints don’t automatically cache the response; they just give you data to drive your own caching strategy (e.g. via Cloudflare’s cache/CDN). [Live collections]

For your specific case (fetching a first name from a database in frontmatter), the knowledge sources don’t show an official pattern for:

  • Caching arbitrary async DB calls in memory on Workers, or
  • Using Cloudflare KV as a manual cache for that lookup.

They only cover KV usage for sessions when using the Cloudflare adapter, not for general caching. [Cloudflare sessions]

So, based on the docs provided:

  • There’s no documented “local cache” API for your DB call.
  • If you can model this data via a live content collection, you can use cache hints + HTTP headers to let Cloudflare cache whole responses.
  • Otherwise, the docs don’t give enough detail on how to speed up that specific DB lookup beyond general Cloudflare/DB tuning outside of Astro.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: