Setting the file. One moment.
Chapter 04 · Cloudflare Deploy
Subchapter 4.137
references/pages-functions/README.mdMarkdown3 KBView on GitHub
Serverless functions on Cloudflare Pages using Workers runtime. Full-stack dev with file-based routing.
Need to…
| Task | Go to |
|---|---|
| Set up TypeScript types | configuration.md - TypeScript Setup |
| Configure bindings (KV, D1, R2) | configuration.md - wrangler.jsonc |
| Access request/env/params | api.md - EventContext |
| Add middleware or auth | patterns.md - Middleware, Auth |
| Background tasks (waitUntil) | patterns.md - Background Tasks |
| Debug errors or check limits | gotchas.md - Common Errors, Limits |
Need serverless backend?
├─ Yes, for a static site → Pages Functions
├─ Yes, standalone API → Workers
└─ Just static hosting → Pages (no functions)
Have existing Worker?
├─ Complex routing logic → Use _worker.js (Advanced Mode)
└─ Simple routes → Migrate to /functions (File-Based)
Framework-based?
├─ Next.js/SvelteKit/Remix → Uses _worker.js automatically
└─ Vanilla/HTML/React SPA → Use /functions/functions
├── index.js → /
├── api.js → /api
├── users/
│ ├── index.js → /users/
│ ├── [user].js → /users/:user
│ └── [[catchall]].js → /users/*
└── _middleware.js → runs on all routesRules:
index.js → directory rootSingle segment [param] → string:
// /functions/users/[user].js
export function onRequest(context) {
return new Response(`Hello ${context.params.user}`);
}
// Matches: /users/neviMulti-segment [[param]] → array:
// /functions/users/[[catchall]].js
export function onRequest(context) {
return new Response(JSON.stringify(context.params.catchall));
}
// Matches: /users/nevi/foobar → ["nevi", "foobar"]onRequestGet, onRequestPost, etc._middleware.js for cross-cutting concernswrangler types command_worker.js for custom routing logicNew to Pages Functions? Start here:
Quick reference lookup: