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/smart-placement/configuration.md
1# Smart Placement Configuration23## wrangler.jsonc Setup45```jsonc6{7"$schema": "./node_modules/wrangler/config-schema.json",8"placement": {9"mode": "smart"10}11}12```1314## Placement Mode Values1516| Mode | Behavior |17|------|----------|18| `"smart"` | Enable Smart Placement - automatic optimization based on traffic analysis |19| `"off"` | Explicitly disable Smart Placement - always run at edge closest to user |20| Not specified | Default behavior - run at edge closest to user (same as `"off"`) |2122**Note:** Smart Placement vs Explicit Placement are separate features. Smart Placement (`mode: "smart"`) uses automatic analysis. For manual placement control, see explicit placement options (`region`, `host`, `hostname` fields - not covered in this reference).2324## Frontend + Backend Split Configuration2526### Frontend Worker (No Smart Placement)2728```jsonc29// frontend-worker/wrangler.jsonc30{31"name": "frontend",32"main": "frontend-worker.ts",33// No "placement" - runs at edge34"services": [35{36"binding": "BACKEND",37"service": "backend-api"38}39]40}41```4243### Backend Worker (Smart Placement Enabled)4445```jsonc46// backend-api/wrangler.jsonc47{48"name": "backend-api",49"main": "backend-worker.ts",50"placement": {51"mode": "smart"52},53"d1_databases": [54{55"binding": "DATABASE",56"database_id": "xxx"57}58]59}60```6162## Requirements & Limitations6364### Requirements65- **Wrangler version:** 2.20.0+66- **Analysis time:** Up to 15 minutes67- **Traffic requirements:** Consistent multi-location traffic68- **Workers plan:** All plans (Free, Paid, Enterprise)6970### What Smart Placement Affects7172**CRITICAL LIMITATION - Smart Placement ONLY Affects `fetch` Handlers:**7374Smart Placement is fundamentally limited to Workers with default `fetch` handlers. This is a key architectural constraint.7576- ✅ **Affects:** `fetch` event handlers ONLY (the default export's fetch method)77- ❌ **Does NOT affect:**78- RPC methods (Service Bindings with `WorkerEntrypoint` - see example below)79- Named entrypoints (exports other than `default`)80- Workers without `fetch` handlers81- Queue consumers, scheduled handlers, or other event types8283**Example - Smart Placement ONLY affects `fetch`:**84```typescript85// ✅ Smart Placement affects this:86export default {87async fetch(request: Request, env: Env): Promise<Response> {88// This runs close to backend when Smart Placement enabled89const data = await env.DATABASE.prepare('SELECT * FROM users').all();90return Response.json(data);91}92}9394// ❌ Smart Placement DOES NOT affect these:95export class MyRPC extends WorkerEntrypoint {96async myMethod() {97// This ALWAYS runs at edge, Smart Placement has NO EFFECT98const data = await this.env.DATABASE.prepare('SELECT * FROM users').all();99return data;100}101}102103export async function scheduled(event: ScheduledEvent, env: Env) {104// NOT affected by Smart Placement105}106```107108**Consequence:** If your backend logic uses RPC methods (`WorkerEntrypoint`), Smart Placement cannot optimize those calls. You must use fetch-based patterns for Smart Placement to work.109110**Solution:** Convert RPC methods to fetch endpoints, or use a wrapper Worker with `fetch` handler that calls your backend RPC (though this adds latency).111112### Baseline Traffic113Smart Placement automatically routes 1% of requests WITHOUT optimization as baseline for performance comparison.114115### Validation Rules116117**Mutually exclusive fields:**118- `mode` cannot be used with explicit placement fields (`region`, `host`, `hostname`)119- Choose either Smart Placement OR explicit placement, not both120121```jsonc122// ✅ Valid - Smart Placement123{ "placement": { "mode": "smart" } }124125// ✅ Valid - Explicit Placement (different feature)126{ "placement": { "region": "us-east1" } }127128// ❌ Invalid - Cannot combine129{ "placement": { "mode": "smart", "region": "us-east1" } }130```131132## Dashboard Configuration133134**Workers & Pages** → Select Worker → **Settings** → **General** → **Placement: Smart** → Wait 15min → Check **Metrics**135136## TypeScript Types137138```typescript139interface Env {140BACKEND: Fetcher;141DATABASE: D1Database;142}143144export default {145async fetch(request: Request, env: Env): Promise<Response> {146const data = await env.DATABASE.prepare('SELECT * FROM table').all();147return Response.json(data);148}149} satisfies ExportedHandler<Env>;150```151152## Cloudflare Pages/Assets Warning153154**CRITICAL PERFORMANCE ISSUE:** Enabling Smart Placement with `assets.run_worker_first = true` in Pages projects **severely degrades asset serving performance**. This is one of the most common misconfigurations.155156**Why this is bad:**157- Smart Placement routes ALL requests (including static assets) away from edge to remote locations158- Static assets (HTML, CSS, JS, images) should ALWAYS be served from edge closest to user159- Result: 2-5x slower asset loading times, poor user experience160161**Problem:** Smart Placement routes asset requests away from edge, but static assets should always be served from edge closest to user.162163**Solutions (in order of preference):**1641. **Recommended:** Split into separate Workers (frontend at edge + backend with Smart Placement)1652. Set `"mode": "off"` to explicitly disable Smart Placement for Pages/Assets Workers1663. Use `assets.run_worker_first = false` (serves assets first, bypasses Worker for static content)167168```jsonc169// ❌ BAD - Degrades asset performance by 2-5x170{171"name": "pages-app",172"placement": { "mode": "smart" },173"assets": { "run_worker_first": true }174}175176// ✅ GOOD - Frontend at edge, backend optimized177// frontend-worker/wrangler.jsonc178{179"name": "frontend",180"assets": { "run_worker_first": true }181// No placement - runs at edge182}183184// backend-worker/wrangler.jsonc185{186"name": "backend-api",187"placement": { "mode": "smart" },188"d1_databases": [{ "binding": "DB", "database_id": "xxx" }]189}190```191192**Key takeaway:** Never enable Smart Placement on Workers that serve static assets with `run_worker_first = true`.193194## Local Development195196Smart Placement does NOT work in `wrangler dev` (local only). Test by deploying: `wrangler deploy --env staging`197