Chapter 04 · Cloudflare Deploy
Subchapter 4.194
references/smart-placement/configuration.mdMarkdown6 KBView on GitHub
{
"$schema": "./node_modules/wrangler/config-schema.json",
"placement": {
"mode": "smart"
}
}| Mode | Behavior |
|---|---|
"smart" | Enable Smart Placement - automatic optimization based on traffic analysis |
"off" | Explicitly disable Smart Placement - always run at edge closest to user |
| Not specified | Default behavior - run at edge closest to user (same as "off") |
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).
// frontend-worker/wrangler.jsonc
{
"name": "frontend",
"main": "frontend-worker.ts",
// No "placement" - runs at edge
"services": [
{
"binding": "BACKEND",
"service": "backend-api"
}
]
}// backend-api/wrangler.jsonc
{
"name": "backend-api",
"main": "backend-worker.ts",
"placement": {
"mode": "smart"
},
"d1_databases": [
{
"binding": "DATABASE",
"database_id": "xxx"
}
]
}CRITICAL LIMITATION - Smart Placement ONLY Affects fetch Handlers:
Smart Placement is fundamentally limited to Workers with default fetch handlers. This is a key architectural constraint.
fetch event handlers ONLY (the default export’s fetch method)WorkerEntrypoint - see example below)default)fetch handlersExample - Smart Placement ONLY affects fetch:
// ✅ Smart Placement affects this:
export default {
async fetch(request: Request, env: Env): Promise<Response> {
// This runs close to backend when Smart Placement enabled
const data = await env.DATABASE.prepare('SELECT * FROM users').all();
return Response.json(data);
}
}
// ❌ Smart Placement DOES NOT affect these:
export class MyRPC extends WorkerEntrypoint {
async myMethod() {
// This ALWAYS runs at edge, Smart Placement has NO EFFECT
const data = await this.env.DATABASE.prepare('SELECT * FROM users').all();
return data;
}
}
export async function scheduled(event: ScheduledEvent, env: Env) {
// NOT affected by Smart Placement
}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.
Solution: Convert RPC methods to fetch endpoints, or use a wrapper Worker with fetch handler that calls your backend RPC (though this adds latency).
Smart Placement automatically routes 1% of requests WITHOUT optimization as baseline for performance comparison.
Mutually exclusive fields:
mode cannot be used with explicit placement fields (region, host, hostname)// ✅ Valid - Smart Placement
{ "placement": { "mode": "smart" } }
// ✅ Valid - Explicit Placement (different feature)
{ "placement": { "region": "us-east1" } }
// ❌ Invalid - Cannot combine
{ "placement": { "mode": "smart", "region": "us-east1" } }Workers & Pages → Select Worker → Settings → General → Placement: Smart → Wait 15min → Check Metrics
interface Env {
BACKEND: Fetcher;
DATABASE: D1Database;
}
export default {
async fetch(request: Request, env: Env): Promise<Response> {
const data = await env.DATABASE.prepare('SELECT * FROM table').all();
return Response.json(data);
}
} satisfies ExportedHandler<Env>;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.
Why this is bad:
Problem: Smart Placement routes asset requests away from edge, but static assets should always be served from edge closest to user.
Solutions (in order of preference):
"mode": "off" to explicitly disable Smart Placement for Pages/Assets Workersassets.run_worker_first = false (serves assets first, bypasses Worker for static content)// ❌ BAD - Degrades asset performance by 2-5x
{
"name": "pages-app",
"placement": { "mode": "smart" },
"assets": { "run_worker_first": true }
}
// ✅ GOOD - Frontend at edge, backend optimized
// frontend-worker/wrangler.jsonc
{
"name": "frontend",
"assets": { "run_worker_first": true }
// No placement - runs at edge
}
// backend-worker/wrangler.jsonc
{
"name": "backend-api",
"placement": { "mode": "smart" },
"d1_databases": [{ "binding": "DB", "database_id": "xxx" }]
}Key takeaway: Never enable Smart Placement on Workers that serve static assets with run_worker_first = true.
Smart Placement does NOT work in wrangler dev (local only). Test by deploying: wrangler deploy --env staging