@flightdev/adapter-cloudflare
v0.0.7
Published
Cloudflare adapter for Flight Framework - deploy to Workers/Pages
Maintainers
Readme
@flightdev/adapter-cloudflare
Cloudflare Workers and Pages deployment adapter for Flight Framework. Deploy to 300+ edge locations worldwide.
Table of Contents
- Installation
- Quick Start
- Configuration
- Cloudflare Pages
- Cloudflare Workers
- Bindings
- Build Output
- Deployment
- API Reference
- License
Installation
npm install @flightdev/adapter-cloudflareQuick Start
// flight.config.ts
import { defineConfig } from '@flightdev/core';
import cloudflare from '@flightdev/adapter-cloudflare';
export default defineConfig({
adapter: cloudflare(),
});Deploy:
npm run build
wrangler pages deploy .cloudflareConfiguration
cloudflare({
// 'pages' or 'workers'
mode: 'pages',
// Routes configuration
routes: {
include: ['/*'],
exclude: ['/static/*', '/assets/*'],
},
// Compatibility settings
compatibility_date: '2024-01-01',
compatibility_flags: ['nodejs_compat'],
// Workers-specific
minify: true,
});Cloudflare Pages
Pages is recommended for most apps. It provides:
- Automatic static asset hosting
- Git-connected deployments
- Preview deployments for PRs
cloudflare({
mode: 'pages',
});Build Output
.cloudflare/
├── _worker.js # Edge worker
├── _routes.json # Route configuration
└── static/ # Static assets
├── assets/
└── index.htmlDeploy
wrangler pages deploy .cloudflareCloudflare Workers
For more control or specific Workers features:
cloudflare({
mode: 'workers',
});wrangler.toml
name = "my-flight-app"
main = ".cloudflare/worker.js"
compatibility_date = "2024-01-01"
compatibility_flags = ["nodejs_compat"]
[site]
bucket = ".cloudflare/static"Deploy
wrangler deployBindings
Access Cloudflare bindings (KV, D1, R2, etc.) in your routes:
KV Namespace
// src/routes/api/cache.get.ts
export async function GET({ platform }) {
const kv = platform.env.MY_KV;
const value = await kv.get('key');
return Response.json({ value });
}
export async function PUT({ platform, request }) {
const kv = platform.env.MY_KV;
const { key, value } = await request.json();
await kv.put(key, value);
return Response.json({ success: true });
}D1 Database
// src/routes/api/users.get.ts
export async function GET({ platform }) {
const db = platform.env.MY_D1;
const { results } = await db.prepare(
'SELECT * FROM users LIMIT 10'
).all();
return Response.json(results);
}R2 Storage
// src/routes/api/files/[key].get.ts
export async function GET({ platform, params }) {
const bucket = platform.env.MY_R2;
const object = await bucket.get(params.key);
if (!object) {
return new Response('Not found', { status: 404 });
}
return new Response(object.body, {
headers: { 'Content-Type': object.httpMetadata.contentType },
});
}Durable Objects
// src/routes/api/counter.ts
export async function GET({ platform, request }) {
const id = platform.env.COUNTER.idFromName('global');
const stub = platform.env.COUNTER.get(id);
return stub.fetch(request);
}Configure Bindings
In wrangler.toml:
[[kv_namespaces]]
binding = "MY_KV"
id = "abc123"
[[d1_databases]]
binding = "MY_D1"
database_name = "my-database"
database_id = "def456"
[[r2_buckets]]
binding = "MY_R2"
bucket_name = "my-bucket"Build Output
Pages Mode
.cloudflare/
├── _worker.js # Edge worker
├── _routes.json # Route config
└── static/Workers Mode
.cloudflare/
├── worker.js # Bundled worker
└── static/ # Static assetsDeployment
Wrangler CLI
# Install Wrangler
npm install -g wrangler
# Login
wrangler login
# Deploy Pages
wrangler pages deploy .cloudflare
# Deploy Workers
wrangler deployGit Integration
Connect your repository in the Cloudflare dashboard for automatic deployments.
API Reference
Adapter Options
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| mode | string | 'pages' | Deployment mode |
| routes.include | string[] | ['/*'] | Included routes |
| routes.exclude | string[] | [] | Excluded routes |
| compatibility_date | string | latest | Compat date |
| compatibility_flags | string[] | [] | Compat flags |
| minify | boolean | true | Minify output |
License
MIT
