Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Comprehensive Cloudflare platform skill covering Workers, D1, R2, KV, AI, Durable Objects, and security.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/pages-functions/configuration.md
1# Configuration23## TypeScript Setup45**Generate types from wrangler.jsonc** (replaces deprecated `@cloudflare/workers-types`):67```bash8npx wrangler types9```1011Creates `worker-configuration.d.ts` with typed `Env` interface based on your bindings.1213```typescript14// functions/api.ts15export const onRequest: PagesFunction<Env> = async (ctx) => {16// ctx.env.KV, ctx.env.DB, etc. are fully typed17return Response.json({ ok: true });18};19```2021**Manual types** (if not using wrangler types):2223```typescript24interface Env {25KV: KVNamespace;26DB: D1Database;27API_KEY: string;28}29export const onRequest: PagesFunction<Env> = async (ctx) => { /* ... */ };30```3132## wrangler.jsonc3334```jsonc35{36"$schema": "./node_modules/wrangler/config-schema.json",37"name": "my-pages-app",38"pages_build_output_dir": "./dist",39"compatibility_date": "2025-01-01",40"compatibility_flags": ["nodejs_compat"],4142"vars": { "API_URL": "https://api.example.com" },43"kv_namespaces": [{ "binding": "KV", "id": "abc123" }],44"d1_databases": [{ "binding": "DB", "database_name": "prod-db", "database_id": "xyz789" }],45"r2_buckets": [{ "binding": "BUCKET", "bucket_name": "my-bucket" }],46"durable_objects": { "bindings": [{ "name": "COUNTER", "class_name": "Counter", "script_name": "counter-worker" }] },47"services": [{ "binding": "AUTH", "service": "auth-worker" }],48"ai": { "binding": "AI" },49"vectorize": [{ "binding": "VECTORIZE", "index_name": "my-index" }],50"analytics_engine_datasets": [{ "binding": "ANALYTICS" }]51}52```5354## Environment Overrides5556Top-level → local dev, `env.preview` → preview, `env.production` → production5758```jsonc59{60"vars": { "API_URL": "http://localhost:8787" },61"env": {62"production": { "vars": { "API_URL": "https://api.example.com" } }63}64}65```6667**Note:** If overriding `vars`, `kv_namespaces`, `d1_databases`, etc., ALL must be redefined (non-inheritable)6869## Local Secrets (.dev.vars)7071**Local dev only** - NOT deployed:7273```bash74# .dev.vars (add to .gitignore)75SECRET_KEY="my-secret-value"76```7778Accessed via `ctx.env.SECRET_KEY`. Set production secrets:79```bash80echo "value" | npx wrangler pages secret put SECRET_KEY --project-name=my-app81```8283## Static Config Files8485**_routes.json** - Custom routing:86```json87{ "version": 1, "include": ["/api/*"], "exclude": ["/static/*"] }88```8990**_headers** - Static headers:91```92/static/*93Cache-Control: public, max-age=3153600094```9596**_redirects** - Redirects:97```98/old /new 30199```100101## Local Dev & Deployment102103```bash104# Dev server105npx wrangler pages dev ./dist106107# With bindings108npx wrangler pages dev ./dist --kv=KV --d1=DB=db-id --r2=BUCKET109110# Durable Objects (2 terminals)111cd do-worker && npx wrangler dev112cd pages-project && npx wrangler pages dev ./dist --do COUNTER=Counter@do-worker113114# Deploy115npx wrangler pages deploy ./dist116npx wrangler pages deploy ./dist --branch preview117118# Download config119npx wrangler pages download config my-project120```121122**See also:** [api.md](./api.md) for binding usage examples123