import "astro/config"; import { AstroIntegration } from "astro"; import { CacheStorage, ExecutionContext, IncomingRequestCfProperties } from "@cloudflare/workers-types"; type Target = "pages" | "workers"; /** Bindings for the Miniflare provider, which the Wrangler config file also supplies. */ interface MiniflareBindingOptions { /** Plain variables and secrets. */ bindings?: Record; /** Names of KV namespace bindings to create. */ kvNamespaces?: string[]; /** Names of D1 database bindings to create. */ d1Databases?: string[]; /** Names of R2 bucket bindings to create. */ r2Buckets?: string[]; /** Compatibility date of the Worker in `astro preview`. Defaults to a recent date that the installed Miniflare supports. */ compatibilityDate?: string; /** Compatibility flags of the Worker in `astro preview`, for example `["nodejs_compat"]`. */ compatibilityFlags?: string[]; /** Where to keep KV, D1 and R2 data between runs: a directory, or `true` for Miniflare's default. By default data lives in memory. */ persist?: boolean | string; } interface PlatformProxyOptions { /** Path to the tool's config file, relative to the project root. */ configPath?: string; /** Named environment in the config file to use. */ environment?: string; /** Where to persist local binding data, or `false` to keep it in memory. */ persist?: boolean | { path: string; }; /** Bindings for `tool: "miniflare"`. */ miniflare?: MiniflareBindingOptions; } type Tool = "wrangler" | "cf" | "miniflare"; type ToolOption = Tool | "auto"; type ImageServiceOption = "compile" | "passthrough" | "cloudflare" | "custom"; interface RouteRule { /** A `_routes.json` pattern such as `/api/*`. */ pattern: string; } interface Options { /** * Tool whose config and local runtime the adapter uses. `"auto"` picks `cf` when a `cloudflare.config.*` file exists, and Wrangler otherwise. `"miniflare"` runs Miniflare directly, without Wrangler; it takes bindings from the Wrangler config file (`wrangler.jsonc`, `wrangler.json` or `wrangler.toml`) and from `platformProxy.miniflare`. * @default "auto" */ tool?: ToolOption; /** Adds rules to the generated `_routes.json`. Ignored when `public/_routes.json` exists. */ routes?: { extend?: { include?: RouteRule[]; exclude?: RouteRule[]; }; }; /** Emulates Cloudflare bindings in `astro dev` with the tool's local runtime (Wrangler or Miniflare). */ platformProxy?: PlatformProxyOptions & { /** @default true */ enabled?: boolean; }; /** * KV binding used by the default session driver when `session.driver` is not set. * @default "SESSION" */ sessionKVBindingName?: string; /** * How images are optimized. sharp cannot run on Cloudflare, so on-demand pages never use it. * * - `"compile"`: optimize images of prerendered pages with sharp at build time, and serve images of on-demand pages unchanged. * - `"passthrough"`: never optimize. * - `"cloudflare"`: resize through Cloudflare Images transformations (`/cdn-cgi/image/`), which must be enabled on the zone. `astro dev` keeps sharp. * - `"custom"`: use `image.service` as configured. * * A non-sharp `image.service` is always kept. * @default "compile" */ imageService?: ImageServiceOption; /** * Writes the headers Astro computes for static pages, such as the Content-Security-Policy of `security.csp`, to `_headers`, so Pages sends them. Rules you put in `public/_headers` are kept. Turn it off to let Astro put the policy in a `` tag instead. * @default true */ staticHeaders?: boolean; /** * Lets pages and endpoints import `.wasm` (also `.wasm?module`), `.bin` and `.txt` files, as `@astrojs/cloudflare` v12 did. A `.wasm` file becomes a file next to the Worker bundle, which is how WebAssembly loads on Cloudflare; `.bin` gives an `ArrayBuffer` and `.txt` a string. * @default true */ cloudflareModules?: boolean; /** * A Worker entry that replaces the adapter's, as `workerEntryPoint` did in `@astrojs/cloudflare` v12. The path is relative to the project root. The module default-exports the Worker (`{ fetch }`), and calls the adapter's handler, which `astro-adapter-cloudflare-pages/handler` exports, for the requests it does not answer itself. Pages Workers run `fetch` only, so scheduled and queue handlers are not supported. */ workerEntryPoint?: string; /** * Where the build runs. * * - `"pages"`: Cloudflare Pages, with the Worker in `dist/_worker.js/` and `_routes.json`. * - `"workers"`: Workers with static assets. The Worker is `dist/server/index.js` and the static files are in `dist/client/`; point `main` and `assets.directory` of your Wrangler config at them, and name the assets binding `ASSETS`. * @default "pages" */ target?: Target; /** Escape hatches. Everything here is unsupported. */ unsafe?: { /** Lets the build continue on an Astro major that this version of the adapter has not been tested with. */ allowUntestedAstro?: boolean; }; } /** * The Cloudflare Pages runtime that this adapter exposes as `Astro.locals.runtime`, with the same shape as `@astrojs/cloudflare` v12. * * Declare your bindings by passing their type, then augment `App.Locals` in `src/env.d.ts`: * * @example * ```ts * type Runtime = import("astro-adapter-cloudflare-pages").Runtime; * * declare namespace App { * interface Locals extends Runtime {} * } * ``` */ interface Runtime> { runtime: { env: Env; cf: IncomingRequestCfProperties | undefined; caches: CacheStorage; ctx: PagesExecutionContext; }; /** The raw execution context, under the same name as `@astrojs/cloudflare` v13 and later use. */ cfContext: ExecutionContext; } /** * `ExecutionContext` as Pages Functions provide it: `passThroughOnException()` exists but always throws, because Pages does not support it. */ interface PagesExecutionContext { waitUntil(promise: Promise): void; passThroughOnException(): never; props: Record; } /** * Creates the Astro integration that builds on-demand rendered sites for Cloudflare Pages advanced mode (`_worker.js`). * * @example * ```js * // astro.config.mjs * import { defineConfig } from "astro/config"; * import cloudflarePages from "astro-adapter-cloudflare-pages"; * * export default defineConfig({ * adapter: cloudflarePages(), * }); * ``` */ export default function cloudflarePages(options?: Options): AstroIntegration; export type { Options, PagesExecutionContext, RouteRule, Runtime };