pi-cloudflare 0.10.2 → 0.11.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +78 -180
- package/package.json +7 -42
- package/plugin.json +3 -3
- package/skills/agents-sdk/SKILL.md +17 -36
- package/skills/agents-sdk/references/client-sdk.md +7 -106
- package/skills/agents-sdk/references/human-in-the-loop.md +6 -63
- package/skills/agents-sdk/references/mcp.md +9 -184
- package/skills/agents-sdk/references/queue-retries.md +12 -73
- package/skills/agents-sdk/references/server-driven-messages.md +4 -60
- package/skills/agents-sdk/references/state-scheduling.md +10 -166
- package/skills/agents-sdk/references/streaming-chat.md +11 -193
- package/skills/agents-sdk/references/webhooks-push.md +2 -2
- package/skills/agents-sdk/references/workflows.md +14 -125
- package/skills/cloudflare/SKILL.md +135 -248
- package/skills/cloudflare/references/ai-gateway/README.md +17 -165
- package/skills/cloudflare/references/ai-gateway/configuration.md +14 -107
- package/skills/cloudflare/references/ai-gateway/dynamic-routing.md +12 -79
- package/skills/cloudflare/references/ai-gateway/features.md +22 -96
- package/skills/cloudflare/references/ai-gateway/sdk-integration.md +17 -112
- package/skills/cloudflare/references/ai-gateway/troubleshooting.md +14 -86
- package/skills/cloudflare/references/ai-search/README.md +17 -130
- package/skills/cloudflare/references/ai-search/api.md +10 -84
- package/skills/cloudflare/references/ai-search/configuration.md +16 -88
- package/skills/cloudflare/references/ai-search/gotchas.md +13 -81
- package/skills/cloudflare/references/ai-search/patterns.md +12 -82
- package/skills/cloudflare/references/analytics-engine/api.md +1 -1
- package/skills/cloudflare/references/api/README.md +2 -2
- package/skills/cloudflare/references/api/configuration.md +1 -1
- package/skills/cloudflare/references/bindings/README.md +11 -117
- package/skills/cloudflare/references/bindings/api.md +17 -201
- package/skills/cloudflare/references/bindings/configuration.md +20 -188
- package/skills/cloudflare/references/bindings/gotchas.md +16 -206
- package/skills/cloudflare/references/bindings/patterns.md +15 -195
- package/skills/cloudflare/references/bot-management/README.md +1 -1
- package/skills/cloudflare/references/browser-rendering/README.md +13 -73
- package/skills/cloudflare/references/browser-rendering/api.md +9 -105
- package/skills/cloudflare/references/browser-rendering/configuration.md +9 -75
- package/skills/cloudflare/references/browser-rendering/gotchas.md +12 -85
- package/skills/cloudflare/references/browser-rendering/patterns.md +9 -88
- package/skills/cloudflare/references/c3/README.md +6 -5
- package/skills/cloudflare/references/c3/api.md +3 -4
- package/skills/cloudflare/references/c3/patterns.md +4 -6
- package/skills/cloudflare/references/cache-reserve/README.md +1 -1
- package/skills/cloudflare/references/containers/README.md +15 -77
- package/skills/cloudflare/references/containers/api.md +13 -187
- package/skills/cloudflare/references/containers/configuration.md +15 -188
- package/skills/cloudflare/references/containers/gotchas.md +16 -178
- package/skills/cloudflare/references/containers/patterns.md +14 -198
- package/skills/cloudflare/references/cron-triggers/README.md +13 -92
- package/skills/cloudflare/references/cron-triggers/api.md +11 -191
- package/skills/cloudflare/references/cron-triggers/configuration.md +11 -175
- package/skills/cloudflare/references/cron-triggers/gotchas.md +13 -195
- package/skills/cloudflare/references/cron-triggers/patterns.md +12 -184
- package/skills/cloudflare/references/d1/README.md +9 -127
- package/skills/cloudflare/references/d1/api.md +16 -194
- package/skills/cloudflare/references/d1/configuration.md +16 -189
- package/skills/cloudflare/references/d1/gotchas.md +16 -96
- package/skills/cloudflare/references/d1/patterns.md +14 -183
- package/skills/cloudflare/references/do-storage/README.md +11 -70
- package/skills/cloudflare/references/do-storage/api.md +11 -99
- package/skills/cloudflare/references/do-storage/configuration.md +11 -107
- package/skills/cloudflare/references/do-storage/gotchas.md +12 -147
- package/skills/cloudflare/references/do-storage/patterns.md +11 -190
- package/skills/cloudflare/references/do-storage/testing.md +8 -178
- package/skills/cloudflare/references/email-routing/README.md +14 -84
- package/skills/cloudflare/references/email-routing/api.md +10 -192
- package/skills/cloudflare/references/email-routing/configuration.md +13 -186
- package/skills/cloudflare/references/email-routing/gotchas.md +12 -193
- package/skills/cloudflare/references/email-routing/patterns.md +10 -226
- package/skills/cloudflare/references/email-workers/README.md +13 -146
- package/skills/cloudflare/references/email-workers/api.md +12 -234
- package/skills/cloudflare/references/email-workers/configuration.md +14 -110
- package/skills/cloudflare/references/email-workers/gotchas.md +15 -125
- package/skills/cloudflare/references/email-workers/patterns.md +14 -100
- package/skills/cloudflare/references/flagship/README.md +2 -2
- package/skills/cloudflare/references/hyperdrive/README.md +17 -72
- package/skills/cloudflare/references/hyperdrive/api.md +19 -136
- package/skills/cloudflare/references/hyperdrive/configuration.md +27 -159
- package/skills/cloudflare/references/hyperdrive/gotchas.md +15 -72
- package/skills/cloudflare/references/hyperdrive/patterns.md +12 -187
- package/skills/cloudflare/references/images/README.md +9 -58
- package/skills/cloudflare/references/images/api.md +13 -96
- package/skills/cloudflare/references/images/configuration.md +17 -211
- package/skills/cloudflare/references/images/gotchas.md +16 -99
- package/skills/cloudflare/references/images/patterns.md +16 -115
- package/skills/cloudflare/references/kv/README.md +9 -83
- package/skills/cloudflare/references/kv/api.md +12 -154
- package/skills/cloudflare/references/kv/configuration.md +11 -139
- package/skills/cloudflare/references/kv/gotchas.md +15 -129
- package/skills/cloudflare/references/kv/patterns.md +15 -190
- package/skills/cloudflare/references/miniflare/README.md +15 -98
- package/skills/cloudflare/references/miniflare/api.md +16 -187
- package/skills/cloudflare/references/miniflare/configuration.md +12 -169
- package/skills/cloudflare/references/miniflare/gotchas.md +15 -160
- package/skills/cloudflare/references/miniflare/patterns.md +15 -177
- package/skills/cloudflare/references/network-interconnect/README.md +1 -1
- package/skills/cloudflare/references/network-interconnect/gotchas.md +1 -1
- package/skills/cloudflare/references/network-interconnect/patterns.md +1 -1
- package/skills/cloudflare/references/observability/README.md +20 -81
- package/skills/cloudflare/references/observability/api.md +20 -164
- package/skills/cloudflare/references/observability/configuration.md +18 -164
- package/skills/cloudflare/references/observability/gotchas.md +19 -109
- package/skills/cloudflare/references/observability/patterns.md +12 -103
- package/skills/cloudflare/references/pages/README.md +13 -81
- package/skills/cloudflare/references/pages/api.md +10 -201
- package/skills/cloudflare/references/pages/configuration.md +16 -201
- package/skills/cloudflare/references/pages/gotchas.md +13 -197
- package/skills/cloudflare/references/pages/patterns.md +11 -200
- package/skills/cloudflare/references/pages-functions/README.md +13 -91
- package/skills/cloudflare/references/pages-functions/api.md +10 -140
- package/skills/cloudflare/references/pages-functions/configuration.md +11 -119
- package/skills/cloudflare/references/pages-functions/gotchas.md +17 -94
- package/skills/cloudflare/references/pages-functions/patterns.md +12 -133
- package/skills/cloudflare/references/pipelines/README.md +1 -1
- package/skills/cloudflare/references/pulumi/README.md +2 -2
- package/skills/cloudflare/references/queues/README.md +15 -87
- package/skills/cloudflare/references/queues/api.md +10 -200
- package/skills/cloudflare/references/queues/configuration.md +18 -142
- package/skills/cloudflare/references/queues/gotchas.md +16 -204
- package/skills/cloudflare/references/queues/patterns.md +19 -218
- package/skills/cloudflare/references/r2/README.md +17 -86
- package/skills/cloudflare/references/r2/api.md +14 -198
- package/skills/cloudflare/references/r2/configuration.md +17 -163
- package/skills/cloudflare/references/r2/gotchas.md +15 -188
- package/skills/cloudflare/references/r2/patterns.md +16 -191
- package/skills/cloudflare/references/r2-data-catalog/README.md +11 -70
- package/skills/cloudflare/references/r2-data-catalog/api.md +14 -89
- package/skills/cloudflare/references/r2-data-catalog/configuration.md +15 -96
- package/skills/cloudflare/references/r2-data-catalog/gotchas.md +14 -51
- package/skills/cloudflare/references/r2-data-catalog/patterns.md +11 -116
- package/skills/cloudflare/references/realtime-sfu/README.md +1 -1
- package/skills/cloudflare/references/realtimekit/README.md +1 -1
- package/skills/cloudflare/references/secrets-store/README.md +14 -66
- package/skills/cloudflare/references/secrets-store/api.md +14 -193
- package/skills/cloudflare/references/secrets-store/configuration.md +16 -176
- package/skills/cloudflare/references/secrets-store/gotchas.md +14 -92
- package/skills/cloudflare/references/secrets-store/patterns.md +14 -194
- package/skills/cloudflare/references/smart-placement/README.md +2 -2
- package/skills/cloudflare/references/static-assets/README.md +15 -59
- package/skills/cloudflare/references/static-assets/api.md +12 -195
- package/skills/cloudflare/references/static-assets/configuration.md +16 -186
- package/skills/cloudflare/references/static-assets/gotchas.md +18 -162
- package/skills/cloudflare/references/static-assets/patterns.md +13 -185
- package/skills/cloudflare/references/stream/README.md +15 -107
- package/skills/cloudflare/references/stream/api-live.md +16 -193
- package/skills/cloudflare/references/stream/api.md +19 -197
- package/skills/cloudflare/references/stream/configuration.md +16 -139
- package/skills/cloudflare/references/stream/gotchas.md +16 -128
- package/skills/cloudflare/references/stream/patterns.md +20 -182
- package/skills/cloudflare/references/tail-workers/README.md +14 -82
- package/skills/cloudflare/references/tail-workers/api.md +10 -197
- package/skills/cloudflare/references/tail-workers/configuration.md +13 -174
- package/skills/cloudflare/references/tail-workers/gotchas.md +12 -188
- package/skills/cloudflare/references/tail-workers/patterns.md +11 -176
- package/skills/cloudflare/references/terraform/README.md +2 -2
- package/skills/cloudflare/references/tunnel/README.md +13 -122
- package/skills/cloudflare/references/tunnel/api.md +11 -190
- package/skills/cloudflare/references/tunnel/configuration.md +12 -155
- package/skills/cloudflare/references/tunnel/gotchas.md +12 -144
- package/skills/cloudflare/references/tunnel/networking.md +12 -166
- package/skills/cloudflare/references/tunnel/patterns.md +12 -189
- package/skills/cloudflare/references/vectorize/README.md +15 -126
- package/skills/cloudflare/references/vectorize/api.md +14 -84
- package/skills/cloudflare/references/vectorize/configuration.md +14 -83
- package/skills/cloudflare/references/vectorize/gotchas.md +12 -73
- package/skills/cloudflare/references/vectorize/patterns.md +17 -83
- package/skills/cloudflare/references/waf/README.md +15 -107
- package/skills/cloudflare/references/waf/api.md +17 -202
- package/skills/cloudflare/references/waf/configuration.md +14 -199
- package/skills/cloudflare/references/waf/gotchas.md +15 -200
- package/skills/cloudflare/references/waf/patterns.md +13 -193
- package/skills/cloudflare/references/workerd/README.md +2 -2
- package/skills/cloudflare/references/workers-ai/README.md +14 -187
- package/skills/cloudflare/references/workers-ai/api.md +10 -109
- package/skills/cloudflare/references/workers-ai/configuration.md +11 -92
- package/skills/cloudflare/references/workers-ai/gotchas.md +12 -111
- package/skills/cloudflare/references/workers-ai/patterns.md +9 -115
- package/skills/cloudflare/references/workers-for-platforms/README.md +3 -3
- package/skills/cloudflare/references/workers-vpc/README.md +1 -1
- package/skills/cloudflare/references/workflows/README.md +15 -69
- package/skills/cloudflare/references/workflows/api.md +12 -213
- package/skills/cloudflare/references/workflows/configuration.md +16 -150
- package/skills/cloudflare/references/workflows/gotchas.md +17 -88
- package/skills/cloudflare/references/workflows/patterns.md +19 -164
- package/skills/cloudflare-api-token/SKILL.md +1 -1
- package/skills/cloudflare-cf-cli/SKILL.md +44 -13
- package/skills/cloudflare-email-service/SKILL.md +6 -36
- package/skills/cloudflare-email-service/references/rest-api.md +12 -179
- package/skills/cloudflare-email-service/references/sending.md +17 -250
- package/skills/cloudflare-one/SKILL.md +4 -2
- package/skills/cloudflare-one-migrations/SKILL.md +1 -1
- package/skills/cloudflare-token-roll/SKILL.md +2 -5
- package/skills/durable-objects/SKILL.md +10 -16
- package/skills/durable-objects/references/rules.md +17 -293
- package/skills/durable-objects/references/testing.md +16 -257
- package/skills/nextjs-on-cloudflare/SKILL.md +33 -0
- package/skills/sandbox-migrate-to-next/SKILL.md +2 -2
- package/skills/sandbox-next/SKILL.md +3 -2
- package/skills/sandbox-next/references/api-quick-ref.md +15 -90
- package/skills/sandbox-stable/SKILL.md +1 -1
- package/skills/turnstile-spin/SKILL.md +14 -8
- package/skills/workers-best-practices/SKILL.md +36 -103
- package/skills/workers-best-practices/references/configuration.md +139 -0
- package/skills/workers-best-practices/references/platform-apis.md +51 -0
- package/skills/workers-best-practices/references/{rules.md → runtime-patterns.md} +9 -134
- package/skills/wrangler/SKILL.md +48 -901
- package/bin/setup.mjs +0 -119
- package/dist/api-token.d.ts +0 -12
- package/dist/api-token.d.ts.map +0 -1
- package/dist/api-token.js +0 -23
- package/dist/api-token.js.map +0 -1
- package/dist/auth-flow.d.ts +0 -15
- package/dist/auth-flow.d.ts.map +0 -1
- package/dist/auth-flow.js +0 -90
- package/dist/auth-flow.js.map +0 -1
- package/dist/client.d.ts +0 -18
- package/dist/client.d.ts.map +0 -1
- package/dist/client.js +0 -47
- package/dist/client.js.map +0 -1
- package/dist/config.d.ts +0 -20
- package/dist/config.d.ts.map +0 -1
- package/dist/config.js +0 -10
- package/dist/config.js.map +0 -1
- package/dist/index.d.ts +0 -22
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -65
- package/dist/index.js.map +0 -1
- package/dist/mcp-server.d.ts +0 -2
- package/dist/mcp-server.d.ts.map +0 -1
- package/dist/mcp-server.js +0 -106
- package/dist/mcp-server.js.map +0 -1
- package/dist/oauth.d.ts +0 -44
- package/dist/oauth.d.ts.map +0 -1
- package/dist/oauth.js +0 -135
- package/dist/oauth.js.map +0 -1
- package/dist/proxy.d.ts +0 -30
- package/dist/proxy.d.ts.map +0 -1
- package/dist/proxy.js +0 -41
- package/dist/proxy.js.map +0 -1
- package/dist/result-caps.d.ts +0 -23
- package/dist/result-caps.d.ts.map +0 -1
- package/dist/result-caps.js +0 -75
- package/dist/result-caps.js.map +0 -1
- package/dist/runtime.d.ts +0 -72
- package/dist/runtime.d.ts.map +0 -1
- package/dist/runtime.js +0 -249
- package/dist/runtime.js.map +0 -1
- package/dist/servers.d.ts +0 -19
- package/dist/servers.d.ts.map +0 -1
- package/dist/servers.js +0 -40
- package/dist/servers.js.map +0 -1
- package/dist/token-store.d.ts +0 -32
- package/dist/token-store.d.ts.map +0 -1
- package/dist/token-store.js +0 -61
- package/dist/token-store.js.map +0 -1
- package/dist/tool-cache.d.ts +0 -25
- package/dist/tool-cache.d.ts.map +0 -1
- package/dist/tool-cache.js +0 -43
- package/dist/tool-cache.js.map +0 -1
- package/mcp.json +0 -15
- package/skills/cloudflare/references/agents-sdk/README.md +0 -89
- package/skills/cloudflare/references/agents-sdk/api.md +0 -190
- package/skills/cloudflare/references/agents-sdk/configuration.md +0 -182
- package/skills/cloudflare/references/agents-sdk/gotchas.md +0 -158
- package/skills/cloudflare/references/agents-sdk/patterns.md +0 -192
- package/skills/cloudflare/references/durable-objects/README.md +0 -185
- package/skills/cloudflare/references/durable-objects/api.md +0 -187
- package/skills/cloudflare/references/durable-objects/configuration.md +0 -160
- package/skills/cloudflare/references/durable-objects/gotchas.md +0 -197
- package/skills/cloudflare/references/durable-objects/patterns.md +0 -201
- package/skills/cloudflare/references/sandbox/README.md +0 -96
- package/skills/cloudflare/references/sandbox/api.md +0 -198
- package/skills/cloudflare/references/sandbox/configuration.md +0 -143
- package/skills/cloudflare/references/sandbox/gotchas.md +0 -194
- package/skills/cloudflare/references/sandbox/patterns.md +0 -201
- package/skills/cloudflare/references/turnstile/README.md +0 -99
- package/skills/cloudflare/references/turnstile/api.md +0 -240
- package/skills/cloudflare/references/turnstile/configuration.md +0 -222
- package/skills/cloudflare/references/turnstile/gotchas.md +0 -218
- package/skills/cloudflare/references/turnstile/patterns.md +0 -193
- package/skills/cloudflare/references/workers/README.md +0 -108
- package/skills/cloudflare/references/workers/api.md +0 -195
- package/skills/cloudflare/references/workers/configuration.md +0 -185
- package/skills/cloudflare/references/workers/frameworks.md +0 -197
- package/skills/cloudflare/references/workers/gotchas.md +0 -137
- package/skills/cloudflare/references/workers/patterns.md +0 -198
- package/skills/cloudflare/references/wrangler/README.md +0 -135
- package/skills/cloudflare/references/wrangler/api.md +0 -188
- package/skills/cloudflare/references/wrangler/configuration.md +0 -197
- package/skills/cloudflare/references/wrangler/gotchas.md +0 -197
- package/skills/cloudflare/references/wrangler/patterns.md +0 -209
- package/skills/cloudflare-auth-setup/SKILL.md +0 -59
- package/skills/workers-best-practices/references/review.md +0 -174
|
@@ -1,143 +1,13 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Pages Functions APIs
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Fetch the API reference before writing a handler; keep runtime types and examples in their authoritative documentation.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
env: Env; // Bindings, vars, secrets
|
|
13
|
-
params: Record<string, string | string[]>; // Route params ([user] or [[catchall]])
|
|
14
|
-
data: any; // Middleware shared state
|
|
15
|
-
}
|
|
16
|
-
```
|
|
5
|
+
| Task | Documentation |
|
|
6
|
+
| --- | --- |
|
|
7
|
+
| Choose method handlers and access EventContext | [API reference](https://developers.cloudflare.com/pages/functions/api-reference/) |
|
|
8
|
+
| Read parameters and resolve dynamic routes | [Routing](https://developers.cloudflare.com/pages/functions/routing/) |
|
|
9
|
+
| Compose middleware and continue a request | [Middleware](https://developers.cloudflare.com/pages/functions/middleware/) |
|
|
10
|
+
| Use a supported resource binding | [Bindings](https://developers.cloudflare.com/pages/functions/bindings/) |
|
|
11
|
+
| Handle requests through generated Worker output | [Advanced mode](https://developers.cloudflare.com/pages/functions/advanced-mode/) |
|
|
17
12
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
## Handlers
|
|
21
|
-
|
|
22
|
-
```typescript
|
|
23
|
-
// Generic (fallback for any method)
|
|
24
|
-
export async function onRequest(ctx: EventContext): Promise<Response> {
|
|
25
|
-
return new Response('Any method');
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
// Method-specific (takes precedence over generic)
|
|
29
|
-
export async function onRequestGet(ctx: EventContext): Promise<Response> {
|
|
30
|
-
return Response.json({ message: 'GET' });
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export async function onRequestPost(ctx: EventContext): Promise<Response> {
|
|
34
|
-
const body = await ctx.request.json();
|
|
35
|
-
return Response.json({ received: body });
|
|
36
|
-
}
|
|
37
|
-
// Also: onRequestPut, onRequestPatch, onRequestDelete, onRequestHead, onRequestOptions
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
## Bindings Reference
|
|
41
|
-
|
|
42
|
-
| Binding Type | Interface | Config Key | Use Case |
|
|
43
|
-
|--------------|-----------|------------|----------|
|
|
44
|
-
| KV | `KVNamespace` | `kv_namespaces` | Key-value cache, sessions, config |
|
|
45
|
-
| D1 | `D1Database` | `d1_databases` | Relational data, SQL queries |
|
|
46
|
-
| R2 | `R2Bucket` | `r2_buckets` | Large files, user uploads, assets |
|
|
47
|
-
| Durable Objects | `DurableObjectNamespace` | `durable_objects.bindings` | Stateful coordination, websockets |
|
|
48
|
-
| Workers AI | `Ai` | `ai.binding` | LLM inference, embeddings |
|
|
49
|
-
| Vectorize | `VectorizeIndex` | `vectorize` | Vector search, embeddings |
|
|
50
|
-
| Service Binding | `Fetcher` | `services` | Worker-to-worker RPC |
|
|
51
|
-
| Analytics Engine | `AnalyticsEngineDataset` | `analytics_engine_datasets` | Event logging, metrics |
|
|
52
|
-
| Environment Vars | `string` | `vars` | Non-sensitive config |
|
|
53
|
-
|
|
54
|
-
See [configuration.md](./configuration.md) for wrangler.jsonc examples.
|
|
55
|
-
|
|
56
|
-
## Bindings
|
|
57
|
-
|
|
58
|
-
### KV
|
|
59
|
-
|
|
60
|
-
```typescript
|
|
61
|
-
interface Env { KV: KVNamespace; }
|
|
62
|
-
export const onRequest: PagesFunction<Env> = async (ctx) => {
|
|
63
|
-
await ctx.env.KV.put('key', 'value', { expirationTtl: 3600 });
|
|
64
|
-
const val = await ctx.env.KV.get('key', { type: 'json' });
|
|
65
|
-
const keys = await ctx.env.KV.list({ prefix: 'user:' });
|
|
66
|
-
return Response.json({ val });
|
|
67
|
-
};
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
### D1
|
|
71
|
-
|
|
72
|
-
```typescript
|
|
73
|
-
interface Env { DB: D1Database; }
|
|
74
|
-
export const onRequest: PagesFunction<Env> = async (ctx) => {
|
|
75
|
-
const user = await ctx.env.DB.prepare('SELECT * FROM users WHERE id = ?').bind(123).first();
|
|
76
|
-
return Response.json(user);
|
|
77
|
-
};
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
### R2
|
|
81
|
-
|
|
82
|
-
```typescript
|
|
83
|
-
interface Env { BUCKET: R2Bucket; }
|
|
84
|
-
export const onRequest: PagesFunction<Env> = async (ctx) => {
|
|
85
|
-
const obj = await ctx.env.BUCKET.get('file.txt');
|
|
86
|
-
if (!obj) return new Response('Not found', { status: 404 });
|
|
87
|
-
await ctx.env.BUCKET.put('file.txt', ctx.request.body);
|
|
88
|
-
return new Response(obj.body);
|
|
89
|
-
};
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
### Durable Objects
|
|
93
|
-
|
|
94
|
-
```typescript
|
|
95
|
-
interface Env { COUNTER: DurableObjectNamespace; }
|
|
96
|
-
export const onRequest: PagesFunction<Env> = async (ctx) => {
|
|
97
|
-
const stub = ctx.env.COUNTER.get(ctx.env.COUNTER.idFromName('global'));
|
|
98
|
-
return stub.fetch(ctx.request);
|
|
99
|
-
};
|
|
100
|
-
```
|
|
101
|
-
|
|
102
|
-
### Workers AI
|
|
103
|
-
|
|
104
|
-
```typescript
|
|
105
|
-
interface Env { AI: Ai; }
|
|
106
|
-
export const onRequest: PagesFunction<Env> = async (ctx) => {
|
|
107
|
-
const resp = await ctx.env.AI.run('@cf/meta/llama-3.1-8b-instruct', { prompt: 'Hello' });
|
|
108
|
-
return Response.json(resp);
|
|
109
|
-
};
|
|
110
|
-
```
|
|
111
|
-
|
|
112
|
-
### Service Bindings & Env Vars
|
|
113
|
-
|
|
114
|
-
```typescript
|
|
115
|
-
interface Env { AUTH: Fetcher; API_KEY: string; }
|
|
116
|
-
export const onRequest: PagesFunction<Env> = async (ctx) => {
|
|
117
|
-
// Service binding: forward to another Worker
|
|
118
|
-
return ctx.env.AUTH.fetch(ctx.request);
|
|
119
|
-
|
|
120
|
-
// Environment variable
|
|
121
|
-
return Response.json({ key: ctx.env.API_KEY });
|
|
122
|
-
};
|
|
123
|
-
```
|
|
124
|
-
|
|
125
|
-
## Advanced Mode (env.ASSETS)
|
|
126
|
-
|
|
127
|
-
When using `_worker.js`, access static assets via `env.ASSETS.fetch()`:
|
|
128
|
-
|
|
129
|
-
```typescript
|
|
130
|
-
interface Env { ASSETS: Fetcher; KV: KVNamespace; }
|
|
131
|
-
|
|
132
|
-
export default {
|
|
133
|
-
async fetch(request: Request, env: Env): Promise<Response> {
|
|
134
|
-
const url = new URL(request.url);
|
|
135
|
-
if (url.pathname.startsWith('/api/')) {
|
|
136
|
-
return Response.json({ data: await env.KV.get('key') });
|
|
137
|
-
}
|
|
138
|
-
return env.ASSETS.fetch(request); // Fallback to static
|
|
139
|
-
}
|
|
140
|
-
} satisfies ExportedHandler<Env>;
|
|
141
|
-
```
|
|
142
|
-
|
|
143
|
-
**See also:** [configuration.md](./configuration.md) for TypeScript setup and wrangler.jsonc | [patterns.md](./patterns.md) for middleware and auth patterns
|
|
13
|
+
Decide which handler owns the response, where shared state is established, and which paths should fall through to assets. The API reference also covers asynchronous work and asset fetching. See [configuration.md](./configuration.md) for binding setup and [patterns.md](./patterns.md) for request design.
|
|
@@ -1,122 +1,14 @@
|
|
|
1
|
-
# Configuration
|
|
1
|
+
# Pages Functions Configuration
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Read Pages-specific configuration before reusing settings from a Worker project.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
| Task | Documentation |
|
|
6
|
+
| --- | --- |
|
|
7
|
+
| Manage Wrangler settings and environment overrides | [Functions configuration](https://developers.cloudflare.com/pages/functions/wrangler-configuration/) |
|
|
8
|
+
| Configure supported bindings, variables, and secrets | [Bindings](https://developers.cloudflare.com/pages/functions/bindings/) |
|
|
9
|
+
| Generate and configure runtime and environment types | [TypeScript](https://developers.cloudflare.com/pages/functions/typescript/) |
|
|
10
|
+
| Run assets and Functions locally | [Local development](https://developers.cloudflare.com/pages/functions/local-development/) |
|
|
11
|
+
| Set Function invocation routes | [Routing](https://developers.cloudflare.com/pages/functions/routing/) |
|
|
12
|
+
| Evaluate and enable placement | [Smart Placement](https://developers.cloudflare.com/pages/functions/smart-placement/) |
|
|
6
13
|
|
|
7
|
-
|
|
8
|
-
npx wrangler types
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
Creates `worker-configuration.d.ts` with typed `Env` interface based on your bindings.
|
|
12
|
-
|
|
13
|
-
```typescript
|
|
14
|
-
// functions/api.ts
|
|
15
|
-
export const onRequest: PagesFunction<Env> = async (ctx) => {
|
|
16
|
-
// ctx.env.KV, ctx.env.DB, etc. are fully typed
|
|
17
|
-
return Response.json({ ok: true });
|
|
18
|
-
};
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
**Manual types** (if not using wrangler types):
|
|
22
|
-
|
|
23
|
-
```typescript
|
|
24
|
-
interface Env {
|
|
25
|
-
KV: KVNamespace;
|
|
26
|
-
DB: D1Database;
|
|
27
|
-
API_KEY: string;
|
|
28
|
-
}
|
|
29
|
-
export const onRequest: PagesFunction<Env> = async (ctx) => { /* ... */ };
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
## wrangler.jsonc
|
|
33
|
-
|
|
34
|
-
```jsonc
|
|
35
|
-
{
|
|
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"],
|
|
41
|
-
|
|
42
|
-
"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
|
-
```
|
|
53
|
-
|
|
54
|
-
## Environment Overrides
|
|
55
|
-
|
|
56
|
-
Top-level → local dev, `env.preview` → preview, `env.production` → production
|
|
57
|
-
|
|
58
|
-
```jsonc
|
|
59
|
-
{
|
|
60
|
-
"vars": { "API_URL": "http://localhost:8787" },
|
|
61
|
-
"env": {
|
|
62
|
-
"production": { "vars": { "API_URL": "https://api.example.com" } }
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
**Note:** If overriding `vars`, `kv_namespaces`, `d1_databases`, etc., ALL must be redefined (non-inheritable)
|
|
68
|
-
|
|
69
|
-
## Local Secrets (.dev.vars)
|
|
70
|
-
|
|
71
|
-
**Local dev only** - NOT deployed:
|
|
72
|
-
|
|
73
|
-
```bash
|
|
74
|
-
# .dev.vars (add to .gitignore)
|
|
75
|
-
SECRET_KEY="my-secret-value"
|
|
76
|
-
```
|
|
77
|
-
|
|
78
|
-
Accessed via `ctx.env.SECRET_KEY`. Set production secrets:
|
|
79
|
-
```bash
|
|
80
|
-
echo "value" | npx wrangler pages secret put SECRET_KEY --project-name=my-app
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
## Static Config Files
|
|
84
|
-
|
|
85
|
-
**_routes.json** - Custom routing:
|
|
86
|
-
```json
|
|
87
|
-
{ "version": 1, "include": ["/api/*"], "exclude": ["/static/*"] }
|
|
88
|
-
```
|
|
89
|
-
|
|
90
|
-
**_headers** - Static headers:
|
|
91
|
-
```
|
|
92
|
-
/static/*
|
|
93
|
-
Cache-Control: public, max-age=31536000
|
|
94
|
-
```
|
|
95
|
-
|
|
96
|
-
**_redirects** - Redirects:
|
|
97
|
-
```
|
|
98
|
-
/old /new 301
|
|
99
|
-
```
|
|
100
|
-
|
|
101
|
-
## Local Dev & Deployment
|
|
102
|
-
|
|
103
|
-
```bash
|
|
104
|
-
# Dev server
|
|
105
|
-
npx wrangler pages dev ./dist
|
|
106
|
-
|
|
107
|
-
# With bindings
|
|
108
|
-
npx wrangler pages dev ./dist --kv=KV --d1=DB=db-id --r2=BUCKET
|
|
109
|
-
|
|
110
|
-
# Durable Objects (2 terminals)
|
|
111
|
-
cd do-worker && npx wrangler dev
|
|
112
|
-
cd pages-project && npx wrangler pages dev ./dist --do COUNTER=Counter@do-worker
|
|
113
|
-
|
|
114
|
-
# Deploy
|
|
115
|
-
npx wrangler pages deploy ./dist
|
|
116
|
-
npx wrangler pages deploy ./dist --branch preview
|
|
117
|
-
|
|
118
|
-
# Download config
|
|
119
|
-
npx wrangler pages download config my-project
|
|
120
|
-
```
|
|
121
|
-
|
|
122
|
-
**See also:** [api.md](./api.md) for binding usage examples
|
|
14
|
+
Identify the target deployment environment and which configuration source controls it. Verify Pages support for each binding and the documented local-development behavior before accessing remote resources. See [Pages configuration](../pages/configuration.md) for build output, headers, and redirects.
|
|
@@ -1,94 +1,17 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
|
6
|
-
|
|
7
|
-
|
|
|
8
|
-
|
|
|
9
|
-
|
|
|
10
|
-
|
|
|
11
|
-
|
|
|
12
|
-
|
|
|
13
|
-
|
|
|
14
|
-
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
### TypeScript type errors
|
|
19
|
-
|
|
20
|
-
**Problem:** `ctx.env.MY_BINDING` shows type error
|
|
21
|
-
**Cause:** No type definition for `Env`
|
|
22
|
-
**Solution:** Run `npx wrangler types` or manually define:
|
|
23
|
-
```typescript
|
|
24
|
-
interface Env { MY_BINDING: KVNamespace; }
|
|
25
|
-
export const onRequest: PagesFunction<Env> = async (ctx) => { /* ... */ };
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
### Secrets not available in production
|
|
29
|
-
|
|
30
|
-
**Problem:** `ctx.env.SECRET_KEY` is undefined in production
|
|
31
|
-
**Cause:** `.dev.vars` is local-only, not deployed
|
|
32
|
-
**Solution:** Set production secrets:
|
|
33
|
-
```bash
|
|
34
|
-
echo "value" | npx wrangler pages secret put SECRET_KEY --project-name=my-app
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
## Debugging
|
|
38
|
-
|
|
39
|
-
```typescript
|
|
40
|
-
// Console logging
|
|
41
|
-
export async function onRequest(ctx) {
|
|
42
|
-
console.log('Request:', ctx.request.method, ctx.request.url);
|
|
43
|
-
const res = await ctx.next();
|
|
44
|
-
console.log('Status:', res.status);
|
|
45
|
-
return res;
|
|
46
|
-
}
|
|
47
|
-
```
|
|
48
|
-
|
|
49
|
-
```bash
|
|
50
|
-
# Stream real-time logs
|
|
51
|
-
npx wrangler pages deployment tail
|
|
52
|
-
npx wrangler pages deployment tail --status error
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
```jsonc
|
|
56
|
-
// Source maps (wrangler.jsonc)
|
|
57
|
-
{ "upload_source_maps": true }
|
|
58
|
-
```
|
|
59
|
-
|
|
60
|
-
## Limits
|
|
61
|
-
|
|
62
|
-
| Resource | Free | Paid |
|
|
63
|
-
|----------|------|------|
|
|
64
|
-
| CPU time | 10ms | 30s (default), 5min (max) |
|
|
65
|
-
| Memory | 128 MB | 128 MB |
|
|
66
|
-
| Script size | 10 MB compressed | 10 MB compressed |
|
|
67
|
-
| Env vars | 5 KB per var, 64 max | 5 KB per var, 64 max |
|
|
68
|
-
| Requests | 100k/day | Unlimited ($0.50/million) |
|
|
69
|
-
|
|
70
|
-
## Best Practices
|
|
71
|
-
|
|
72
|
-
**Performance:** Minimize deps (cold start), use KV for cache/D1 for relational/R2 for large files, set `Cache-Control` headers, batch DB ops, handle errors gracefully
|
|
73
|
-
|
|
74
|
-
**Security:** Never commit secrets (use `.dev.vars` + gitignore), validate input, sanitize before DB, implement auth middleware, set CORS headers, rate limit per-IP
|
|
75
|
-
|
|
76
|
-
## Migration
|
|
77
|
-
|
|
78
|
-
**Workers → Pages Functions:**
|
|
79
|
-
- `export default { fetch(req, env) {} }` → `export function onRequest(ctx) { const { request, env } = ctx; }`
|
|
80
|
-
- Use `_worker.js` for complex routing: `env.ASSETS.fetch(request)` for static files
|
|
81
|
-
|
|
82
|
-
**Other platforms → Pages:**
|
|
83
|
-
- File-based routing: `/functions/api/users.js` → `/api/users`
|
|
84
|
-
- Dynamic routes: `[param]` not `:param`
|
|
85
|
-
- Replace Node.js deps with Workers APIs or add `nodejs_compat` flag
|
|
86
|
-
|
|
87
|
-
## Resources
|
|
88
|
-
|
|
89
|
-
- [Official Docs](https://developers.cloudflare.com/pages/functions/)
|
|
90
|
-
- [Workers APIs](https://developers.cloudflare.com/workers/runtime-apis/)
|
|
91
|
-
- [Examples](https://github.com/cloudflare/pages-example-projects)
|
|
92
|
-
- [Discord](https://discord.gg/cloudflaredev)
|
|
93
|
-
|
|
94
|
-
**See also:** [configuration.md](./configuration.md) for TypeScript setup | [patterns.md](./patterns.md) for middleware/auth | [api.md](./api.md) for bindings
|
|
1
|
+
# Pages Functions Troubleshooting
|
|
2
|
+
|
|
3
|
+
Start with the request path, deployment environment, and generated output that actually handled the request.
|
|
4
|
+
|
|
5
|
+
| Task | Documentation |
|
|
6
|
+
| --- | --- |
|
|
7
|
+
| A Function does not run or receives unexpected parameters | [Routing](https://developers.cloudflare.com/pages/functions/routing/) |
|
|
8
|
+
| Middleware is skipped or static fallback fails | [Advanced mode](https://developers.cloudflare.com/pages/functions/advanced-mode/) |
|
|
9
|
+
| Middleware order or scope is incorrect | [Middleware](https://developers.cloudflare.com/pages/functions/middleware/) |
|
|
10
|
+
| Bindings or secrets differ between environments | [Bindings](https://developers.cloudflare.com/pages/functions/bindings/) |
|
|
11
|
+
| Runtime or environment types do not match | [TypeScript](https://developers.cloudflare.com/pages/functions/typescript/) |
|
|
12
|
+
| A local request behaves differently | [Local development](https://developers.cloudflare.com/pages/functions/local-development/) |
|
|
13
|
+
| Inspect exceptions and deployment logs | [Debugging and logging](https://developers.cloudflare.com/pages/functions/debugging-and-logging/) |
|
|
14
|
+
| Check runtime quotas | [Workers limits](https://developers.cloudflare.com/workers/platform/limits/) |
|
|
15
|
+
| Check request costs | [Functions pricing](https://developers.cloudflare.com/pages/functions/pricing/) |
|
|
16
|
+
|
|
17
|
+
Reproduce a failing path through the actual application rather than only calling a handler with a hand-built context. Check the deployed configuration and generated output before changing application code. See [Pages troubleshooting](../pages/gotchas.md) for build, asset, and framework issues.
|
|
@@ -1,137 +1,16 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Pages Functions Request Design
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Choose where behavior belongs before adapting an example.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
| Task | Documentation |
|
|
6
|
+
| --- | --- |
|
|
7
|
+
| Share authentication, logging, and error handling | [Middleware](https://developers.cloudflare.com/pages/functions/middleware/) |
|
|
8
|
+
| Choose request handlers and asynchronous completion behavior | [API reference](https://developers.cloudflare.com/pages/functions/api-reference/) |
|
|
9
|
+
| Keep asset requests outside Function invocation where appropriate | [Routing](https://developers.cloudflare.com/pages/functions/routing/) |
|
|
10
|
+
| Use custom or framework-generated routing | [Advanced mode](https://developers.cloudflare.com/pages/functions/advanced-mode/) |
|
|
11
|
+
| Integrate storage or another service | [Bindings](https://developers.cloudflare.com/pages/functions/bindings/) |
|
|
12
|
+
| Exercise the assembled application locally | [Local development](https://developers.cloudflare.com/pages/functions/local-development/) |
|
|
6
13
|
|
|
7
|
-
|
|
8
|
-
export async function onRequest(ctx: EventContext<Env>) {
|
|
9
|
-
const res = Response.json({ success: true });
|
|
10
|
-
|
|
11
|
-
ctx.waitUntil(ctx.env.KV.put('last-visit', new Date().toISOString()));
|
|
12
|
-
ctx.waitUntil(Promise.all([
|
|
13
|
-
ctx.env.ANALYTICS.writeDataPoint({ event: 'view' }),
|
|
14
|
-
fetch('https://webhook.site/...', { method: 'POST' })
|
|
15
|
-
]));
|
|
16
|
-
|
|
17
|
-
return res; // Returned immediately
|
|
18
|
-
}
|
|
19
|
-
```
|
|
14
|
+
Define the response owner and middleware scope before adding authentication or response transformations. Test protected routes, rejected requests, and static fallbacks together. Choose consistency and concurrency requirements before using storage for session state or rate limiting; a generic read-modify-write example is not a complete policy.
|
|
20
15
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
```typescript
|
|
24
|
-
// functions/_middleware.js (global) or functions/users/_middleware.js (scoped)
|
|
25
|
-
export async function onRequest(ctx) {
|
|
26
|
-
try { return await ctx.next(); }
|
|
27
|
-
catch (err) { return new Response(err.message, { status: 500 }); }
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// Chained: export const onRequest = [errorHandler, auth, logger];
|
|
31
|
-
|
|
32
|
-
// Auth
|
|
33
|
-
async function auth(ctx: EventContext<Env>) {
|
|
34
|
-
const token = ctx.request.headers.get('authorization')?.replace('Bearer ', '');
|
|
35
|
-
if (!token) return new Response('Unauthorized', { status: 401 });
|
|
36
|
-
const session = await ctx.env.KV.get(`session:${token}`);
|
|
37
|
-
if (!session) return new Response('Invalid', { status: 401 });
|
|
38
|
-
ctx.data.user = JSON.parse(session);
|
|
39
|
-
return ctx.next();
|
|
40
|
-
}
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
## CORS & Rate Limiting
|
|
44
|
-
|
|
45
|
-
```typescript
|
|
46
|
-
// CORS middleware
|
|
47
|
-
const cors = { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET, POST' };
|
|
48
|
-
export async function onRequestOptions() { return new Response(null, { headers: cors }); }
|
|
49
|
-
export async function onRequest(ctx) {
|
|
50
|
-
const res = await ctx.next();
|
|
51
|
-
Object.entries(cors).forEach(([k, v]) => res.headers.set(k, v));
|
|
52
|
-
return res;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// Rate limiting (KV-based)
|
|
56
|
-
async function rateLimit(ctx: EventContext<Env>) {
|
|
57
|
-
const ip = ctx.request.headers.get('CF-Connecting-IP') || 'unknown';
|
|
58
|
-
const count = parseInt(await ctx.env.KV.get(`rate:${ip}`) || '0');
|
|
59
|
-
if (count >= 100) return new Response('Rate limited', { status: 429 });
|
|
60
|
-
await ctx.env.KV.put(`rate:${ip}`, (count + 1).toString(), { expirationTtl: 3600 });
|
|
61
|
-
return ctx.next();
|
|
62
|
-
}
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
## Forms, Caching, Redirects
|
|
66
|
-
|
|
67
|
-
```typescript
|
|
68
|
-
// JSON & file upload
|
|
69
|
-
export async function onRequestPost(ctx) {
|
|
70
|
-
const ct = ctx.request.headers.get('content-type') || '';
|
|
71
|
-
if (ct.includes('application/json')) return Response.json(await ctx.request.json());
|
|
72
|
-
if (ct.includes('multipart/form-data')) {
|
|
73
|
-
const file = (await ctx.request.formData()).get('file') as File;
|
|
74
|
-
await ctx.env.BUCKET.put(file.name, file.stream());
|
|
75
|
-
return Response.json({ uploaded: file.name });
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
// Cache API
|
|
80
|
-
export async function onRequest(ctx) {
|
|
81
|
-
let res = await caches.default.match(ctx.request);
|
|
82
|
-
if (!res) {
|
|
83
|
-
res = new Response('Data');
|
|
84
|
-
res.headers.set('Cache-Control', 'public, max-age=3600');
|
|
85
|
-
ctx.waitUntil(caches.default.put(ctx.request, res.clone()));
|
|
86
|
-
}
|
|
87
|
-
return res;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// Redirects
|
|
91
|
-
export async function onRequest(ctx) {
|
|
92
|
-
if (new URL(ctx.request.url).pathname === '/old') {
|
|
93
|
-
return Response.redirect(new URL('/new', ctx.request.url), 301);
|
|
94
|
-
}
|
|
95
|
-
return ctx.next();
|
|
96
|
-
}
|
|
97
|
-
```
|
|
98
|
-
|
|
99
|
-
## Testing
|
|
100
|
-
|
|
101
|
-
**Unit tests** (Vitest + cloudflare:test):
|
|
102
|
-
```typescript
|
|
103
|
-
import { env } from 'cloudflare:test';
|
|
104
|
-
import { it, expect } from 'vitest';
|
|
105
|
-
import { onRequest } from '../functions/api';
|
|
106
|
-
|
|
107
|
-
it('returns JSON', async () => {
|
|
108
|
-
const req = new Request('http://localhost/api');
|
|
109
|
-
const ctx = { request: req, env, params: {}, data: {} } as EventContext;
|
|
110
|
-
const res = await onRequest(ctx);
|
|
111
|
-
expect(res.status).toBe(200);
|
|
112
|
-
});
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
**Integration:** `wrangler pages dev` + Playwright/Cypress
|
|
116
|
-
|
|
117
|
-
## Advanced Mode (_worker.js)
|
|
118
|
-
|
|
119
|
-
Use `_worker.js` for complex routing (replaces `/functions`):
|
|
120
|
-
|
|
121
|
-
```typescript
|
|
122
|
-
interface Env { ASSETS: Fetcher; KV: KVNamespace; }
|
|
123
|
-
|
|
124
|
-
export default {
|
|
125
|
-
async fetch(request: Request, env: Env): Promise<Response> {
|
|
126
|
-
const url = new URL(request.url);
|
|
127
|
-
if (url.pathname.startsWith('/api/')) {
|
|
128
|
-
return Response.json({ data: await env.KV.get('key') });
|
|
129
|
-
}
|
|
130
|
-
return env.ASSETS.fetch(request); // Static files
|
|
131
|
-
}
|
|
132
|
-
} satisfies ExportedHandler<Env>;
|
|
133
|
-
```
|
|
134
|
-
|
|
135
|
-
**When:** Existing Worker, framework-generated (Next.js/SvelteKit), custom routing logic
|
|
136
|
-
|
|
137
|
-
**See also:** [api.md](./api.md) for `env.ASSETS.fetch()` | [gotchas.md](./gotchas.md) for debugging
|
|
16
|
+
See [Pages project decisions](../pages/patterns.md) for framework and migration work.
|
|
@@ -87,4 +87,4 @@ These are non-obvious and prevent most failures — see [gotchas.md](gotchas.md)
|
|
|
87
87
|
|
|
88
88
|
- [r2-data-catalog](../r2-data-catalog/) — Iceberg sink destination
|
|
89
89
|
- [r2-sql](../r2-sql/) — query the ingested data
|
|
90
|
-
- [r2](../r2/) · [queues](../queues/) · [workers](
|
|
90
|
+
- [r2](../r2/) · [queues](../queues/) · [workers](https://developers.cloudflare.com/workers/)
|
|
@@ -96,5 +96,5 @@ const accountId = new pulumi.Config("cloudflare").require("accountId");
|
|
|
96
96
|
|
|
97
97
|
## See Also
|
|
98
98
|
- [terraform](../terraform/) - Alternative IaC for Cloudflare
|
|
99
|
-
- [wrangler](
|
|
100
|
-
- [workers](
|
|
99
|
+
- [wrangler](https://developers.cloudflare.com/workers/wrangler/) - CLI deployment alternative
|
|
100
|
+
- [workers](https://developers.cloudflare.com/workers/) - Worker runtime documentation
|