Skip to content

@astrojs/cloudflare@14.0.0

Latest

Choose a tag to compare

@astrobot-houston astrobot-houston released this 22 Jun 10:10
f55ba4c

Major Changes

Minor Changes

  • #16335 9a53f77 Thanks @ascorbic! - Adds an opt-in CDN cache provider for Astro route caching on Cloudflare Workers

    [!WARNING]
    This provider requires the Cloudflare Workers Cache feature, which is currently in private beta. It is opt-in: nothing changes unless you import cacheCloudflare() and set it as your provider. But without beta access it does not work and should not be used. Cloudflare Workers run in front of the cache, so cached responses are never served, and calling cache.invalidate() throws an error.

    Setup

    Import cacheCloudflare() from @astrojs/cloudflare/cache and set it as your cache provider:

    import { defineConfig } from 'astro/config';
    import cloudflare from '@astrojs/cloudflare';
    import { cacheCloudflare } from '@astrojs/cloudflare/cache';
    
    export default defineConfig({
      adapter: cloudflare(),
      cache: {
        provider: cacheCloudflare(),
      },
    });

    The adapter automatically enables the Worker caching layer when a Cloudflare cache provider is configured. No manual wrangler.jsonc changes are needed.

    Caching responses

    Use Astro.cache.set() in your pages and API routes to cache responses. The provider sets Cloudflare-CDN-Cache-Control and Cache-Tag headers, which are read by Cloudflare's built-in caching layer. Cache hits bypass Worker execution entirely, meaning your Worker is not invoked for cached responses.

    ---
    Astro.cache.set({ maxAge: 300, tags: ['products'] });
    const data = await fetchProducts();
    ---
    
    <ProductList items={data} />

    You can also set cache rules for groups of routes in your config:

    cache: { provider: cacheCloudflare() },
    routeRules: {
      '/products/[...slug]': { maxAge: 3600, tags: ['products'] },
      '/api/[...path]': { maxAge: 60, swr: 600 },
    },

    Invalidation

    Purge cached responses by tag or path from any API route or server endpoint:

    // src/pages/api/purge.ts
    export async function POST({ request, cache }) {
      await cache.invalidate({ tags: ['products'] });
      return new Response('Purged');
    }
    
    // Path-based invalidation (implemented via an auto-generated path tag)
    await cache.invalidate({ path: '/products/123' });

    Both tag-based and path-based invalidation are supported.

Patch Changes

  • #16961 96398e8 Thanks @adamchal! - Speeds up astro sync by no longer starting the Cloudflare runtime during type generation

  • #16671 fd926fd Thanks @alexanderniebuhr! - Removes deprecations warnings added in Astro v6 for Cloudflare specific Astro.locals properties.

  • #17027 241250b Thanks @ocavue! - Triggers beta prereleases for packages that are still on alpha

  • Updated dependencies []:

    • @astrojs/underscore-redirects@1.0.3