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/README.md
1# Cloudflare Pages Functions23Serverless functions on Cloudflare Pages using Workers runtime. Full-stack dev with file-based routing.45## Quick Navigation67**Need to...**8| Task | Go to |9|------|-------|10| Set up TypeScript types | [configuration.md](./configuration.md) - TypeScript Setup |11| Configure bindings (KV, D1, R2) | [configuration.md](./configuration.md) - wrangler.jsonc |12| Access request/env/params | [api.md](./api.md) - EventContext |13| Add middleware or auth | [patterns.md](./patterns.md) - Middleware, Auth |14| Background tasks (waitUntil) | [patterns.md](./patterns.md) - Background Tasks |15| Debug errors or check limits | [gotchas.md](./gotchas.md) - Common Errors, Limits |1617## Decision Tree: Is This Pages Functions?1819```20Need serverless backend?21├─ Yes, for a static site → Pages Functions22├─ Yes, standalone API → Workers23└─ Just static hosting → Pages (no functions)2425Have existing Worker?26├─ Complex routing logic → Use _worker.js (Advanced Mode)27└─ Simple routes → Migrate to /functions (File-Based)2829Framework-based?30├─ Next.js/SvelteKit/Remix → Uses _worker.js automatically31└─ Vanilla/HTML/React SPA → Use /functions32```3334## File-Based Routing3536```37/functions38├── index.js → /39├── api.js → /api40├── users/41│ ├── index.js → /users/42│ ├── [user].js → /users/:user43│ └── [[catchall]].js → /users/*44└── _middleware.js → runs on all routes45```4647**Rules:**48- `index.js` → directory root49- Trailing slash optional50- Specific routes precede catch-alls51- Falls back to static if no match5253## Dynamic Routes5455**Single segment** `[param]` → string:56```js57// /functions/users/[user].js58export function onRequest(context) {59return new Response(`Hello ${context.params.user}`);60}61// Matches: /users/nevi62```6364**Multi-segment** `[[param]]` → array:65```js66// /functions/users/[[catchall]].js67export function onRequest(context) {68return new Response(JSON.stringify(context.params.catchall));69}70// Matches: /users/nevi/foobar → ["nevi", "foobar"]71```7273## Key Features7475- **Method handlers:** `onRequestGet`, `onRequestPost`, etc.76- **Middleware:** `_middleware.js` for cross-cutting concerns77- **Bindings:** KV, D1, R2, Durable Objects, Workers AI, Service bindings78- **TypeScript:** Full type support via `wrangler types` command79- **Advanced mode:** Use `_worker.js` for custom routing logic8081## Reading Order8283**New to Pages Functions?** Start here:841. [README.md](./README.md) - Overview, routing, decision tree (you are here)852. [configuration.md](./configuration.md) - TypeScript setup, wrangler.jsonc, bindings863. [api.md](./api.md) - EventContext, handlers, bindings reference874. [patterns.md](./patterns.md) - Middleware, auth, CORS, rate limiting, caching885. [gotchas.md](./gotchas.md) - Common errors, debugging, limits8990**Quick reference lookup:**91- Bindings table → [api.md](./api.md)92- Error diagnosis → [gotchas.md](./gotchas.md)93- TypeScript setup → [configuration.md](./configuration.md)9495## See Also96- [pages](../pages/) - Pages platform overview and static site deployment97- [workers](../workers/) - Workers runtime API reference98- [d1](../d1/) - D1 database integration with Pages Functions99