Cache-busting with Cloudflare KV stores and Supabase

InstructorJon Meyers

Share this video with your friends

Send Tweet

Cache Busting or Invalidation, is the process of clearing stale data from a cache. After the first request, the response data from Supabase is cached in our KV store, and never fetched again. This means if data in the database changes, our KV store will never be updated.

In this lesson, we create a revalidate route, which fetches fresh data from Supabase and updates the cache.

Code Snippets

Revalidate cache for articles route

router.get(
  "/revalidate",
  async (request, { SUPABASE_URL, SUPABASE_ANON_KEY, ARTICLES }) => {
    const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY);
    const { data: articles } = await supabase.from("articles").select("*");
    await writeTo(ARTICLES, "/articles", articles);
    return json({ received: true });
  }
);

Run wrangler development server

npx wrangler dev

Resources