The best way to control Claude Code, Codex and more from any computer or phone
| 1 | // Alchemy modifications are licensed under Apache-2.0. |
| 2 | // This file includes third-party code; see /THIRD_PARTY_LICENSES.md. |
| 3 | /** |
| 4 | * Vendored from `@astrojs/cloudflare` v14.4.2 (`src/cache/index.ts`). |
| 5 | */ |
| 6 | import type { CacheProviderConfig } from "astro"; |
| 7 | |
| 8 | /** The module Astro loads into the Worker as the cache provider. */ |
| 9 | export const CACHE_PROVIDER_ENTRYPOINT = "@alchemy.run/frontend-frameworks/astro/cache/provider"; |
| 10 | |
| 11 | /** |
| 12 | * Configure the Cloudflare Workers cache provider for Astro route caching. |
| 13 | * |
| 14 | * Responses carry `Cloudflare-CDN-Cache-Control` and `Cache-Tag` headers for |
| 15 | * Cloudflare's Workers Cache. Each response is tagged with its request path, |
| 16 | * so path and tag invalidation both purge by tag through the Worker cache |
| 17 | * API. When the Worker has a `CF_VERSION_METADATA` binding, responses also |
| 18 | * carry the Worker version as an `astro-version:` tag. |
| 19 | * |
| 20 | * `Cloudflare.Website.Astro` turns on Workers Cache for the Worker when the |
| 21 | * Astro config uses this provider. |
| 22 | * |
| 23 | * ```js |
| 24 | * // astro.config.mjs |
| 25 | * import { defineConfig } from "astro/config"; |
| 26 | * import { cacheCloudflare } from "@alchemy.run/frontend-frameworks/astro/cache"; |
| 27 | * |
| 28 | * export default defineConfig({ |
| 29 | * cache: { provider: cacheCloudflare() }, |
| 30 | * routeRules: { "/blog/[...path]": { maxAge: 600, swr: 86400 } }, |
| 31 | * }); |
| 32 | * ``` |
| 33 | */ |
| 34 | export function cacheCloudflare(): CacheProviderConfig { |
| 35 | return { |
| 36 | name: "cloudflare", |
| 37 | entrypoint: CACHE_PROVIDER_ENTRYPOINT, |
| 38 | }; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Whether an Astro config uses the Cloudflare cache provider: this |
| 43 | * package's {@link cacheCloudflare} or upstream `@astrojs/cloudflare/cache`'s |
| 44 | * (both are named `"cloudflare"`). |
| 45 | */ |
| 46 | export const usesCloudflareCacheProvider = (config: unknown): boolean => |
| 47 | typeof config === "object" && |
| 48 | config !== null && |
| 49 | "cache" in config && |
| 50 | typeof config.cache === "object" && |
| 51 | config.cache !== null && |
| 52 | "provider" in config.cache && |
| 53 | typeof config.cache.provider === "object" && |
| 54 | config.cache.provider !== null && |
| 55 | "name" in config.cache.provider && |
| 56 | config.cache.provider.name === "cloudflare"; |