skip to content

In Cloudflare Workers, how do routes, Custom Domains and the workers.dev subdomain differ when a Wrangler file attaches a Worker to traffic?

level: middleimportance: should knowfreq 40%

answer

  1. in front of an origin, or the origin
  2. who creates the DNS record
  3. most specific pattern wins
  4. workers_dev default depends on routes

basics

~20 s

A route runs a Worker on matching URL patterns of a proxied hostname, in front of an origin. A Custom Domain makes the Worker the origin of a whole hostname, with Cloudflare creating DNS and certificate. workers.dev is the account's test subdomain.

solid answer

~40 s

All three are set in the Wrangler file. A **route** is a `routes` entry such as `{ "pattern": "example.com/orders/*", "zone_name": "example.com" }`: it needs a proxied DNS record, the most specific pattern wins, and `fetch(request)` goes on to the origin. A **Custom Domain** is a `routes` entry with `"custom_domain": true`: Cloudflare creates the DNS record and certificate, every path on the hostname reaches the Worker, and the Worker is the origin. **`workers.dev`** is controlled by `workers_dev`, which defaults to `true` only when the file has no `route` or `routes`. Cloudflare recommends routes or Custom Domains for production, and turning `workers.dev` off only in the dashboard is undone by the next deploy of a file with no routes.

code

json · 17 lines
json
{
  "$schema": "./node_modules/wrangler/config-schema.json",
  "name": "orders-api",
  "main": "src/index.js",
  "compatibility_date": "2026-10-01",
  "workers_dev": false,
  "routes": [
    {
      "pattern": "api.example.com",
      "custom_domain": true
    },
    {
      "pattern": "example.com/orders/*",
      "zone_name": "example.com"
    }
  ]
}

go deeper

for a junior

Recall the three ways in, and that a Custom Domain is a routes entry with custom_domain set to true.

for a middle

Explain who owns DNS and certificates for each, the most-specific-pattern rule, and when workers_dev defaults to true or false.

for a senior

Show you would pick a Custom Domain for a Worker that is the origin and a route for one that fronts a server, and that you keep production off workers.dev.

for a principal

Set the team rule for exposure: which Workers may have public hostnames, and that internal ones ship with workers_dev false and no routes.

## Three ways in A Cloudflare Worker receives HTTP requests only once it is attached to an endpoint. The Wrangler configuration file offers three: **routes**, **Custom Domains** and the **`workers.dev` subdomain**. Routes and Custom Domains share the `route` / `routes` keys, and `workers.dev` has a key of its own, `workers_dev`. `route` takes a single entry and `routes` an array; a file may use one or the other, not both, and both are inheritable, so an environment that does not set its own gets the top-level hostnames. A Worker that handles only Cron Triggers or service-binding calls needs none of the three. ## Routes: a Worker in front of an origin A **route** maps a URL pattern in a Cloudflare zone to a Worker, for example `example.com/orders/*`. In the file it is a plain string (a simple route) or an object with `pattern` plus `zone_name` or `zone_id`. Routes suit a site that already has an application server behind Cloudflare: the Worker runs first and can pass the request on with `fetch(request)`, which reaches the origin defined in the zone's DNS settings. The pattern rules are Cloudflare's own: - The hostname needs a **proxied DNS record** in the zone, because the route only acts on traffic Cloudflare already handles. - The only operator is `*`, matching zero or more characters. Infix wildcards and query strings are not allowed, so `example.com/*.jpg` is invalid. - When several patterns match, the **most specific** wins: `example.com/hello/*` beats `example.com/*` for `example.com/hello/world`. - The path is case sensitive. For routes created since 15 October 2023, the host is not. - A route cannot be the target of a same-zone `fetch()` from another Worker. ## Custom Domains: the Worker is the origin A **Custom Domain** is written as a route object with `"custom_domain": true`, for example `{ "pattern": "api.example.com", "custom_domain": true }`. Here the Worker *is* the application, not a layer in front of one: - Cloudflare creates the DNS record and issues the certificate itself, so you edit no DNS and manage no certificate. - Every path on that hostname reaches the Worker. Paths and query strings play no part in matching, and wildcards are not supported. - The hostname must belong to a Cloudflare zone on your own account. - Other Workers can call it with same-zone `fetch()`, and a route on the same hostname takes precedence, which lets a logging or auth Worker run in front. ## `workers.dev`: the account subdomain Every account has a `workers.dev` subdomain, and a Worker can be reached at `<name>.<subdomain>.workers.dev`. The `workers_dev` key controls it: 1. If the file sets neither `route` nor `routes`, `workers_dev` defaults to `true`. 2. If the file sets either, it defaults to `false`; write `"workers_dev": true` to keep the subdomain alongside the routes. 3. Disabling the subdomain in the dashboard is undone by the next deploy whenever the file has no routes and no `"workers_dev": false`, because the default then turns it back on. Cloudflare recommends routes or Custom Domains for production; a `workers.dev` hostname is treated as a Free website and is meant for personal or hobby projects. Turning it off does not turn off version or preview URLs, which are a deployment topic. ## Side by side | | Route | Custom Domain | `workers.dev` | |---|---|---|---| | Config | `routes` entry with `zone_name` / `zone_id` | `routes` entry with `custom_domain: true` | `workers_dev: true` | | DNS and certificate | you provide a proxied record | Cloudflare creates both | provided | | Matches | URL patterns, most specific wins | the whole hostname | the Worker's own subdomain | | Origin behind it | yes, via `fetch(request)` | the Worker is the origin | none | | Same-zone `fetch()` target | no | yes | not applicable | ## Choosing in the file - A new API or site with no other server: a Custom Domain. - A Worker that rewrites, authenticates or caches in front of an existing origin: a route. - A private internal Worker reached only through service bindings or a schedule: `"workers_dev": false` and no routes. - A demo or a short-lived test Worker: `workers.dev`. One Worker may combine them: the `routes` array can hold a Custom Domain entry and ordinary route entries side by side, and `"workers_dev": true` can keep the test hostname alongside both. Whatever the mix, the file is what a deploy applies, so a route added only in the dashboard does not survive the next deploy that lists routes.

  • Why can a Worker call a Custom Domain with fetch() but not a route on the same zone?
    A Custom Domain is an origin: its hostname resolves to the Worker, so a same-zone `fetch()` to it invokes that Worker like any application server. A route is a pattern that intercepts traffic on its way to an origin, and Cloudflare does not let it be the target of a same-zone `fetch()`. A route and a Custom Domain on one hostname can be chained, with the route running first.
  • How do you keep a Worker off the public internet entirely?
    Give it no `route` or `routes` and set `"workers_dev": false`, so it has no hostname at all. It can still run from a Cron Trigger or be called by other Workers through a service binding. Leaving `workers_dev` out would not do it, because with no routes it defaults to `true` on the next deploy.

  • A route creates its own DNS record, so no proxied record is needed
  • A Custom Domain can use a wildcard such as *.example.com
  • workers_dev stays true after routes are added unless set to false
  • When two route patterns match, the first one declared wins
  • A route pattern may put a wildcard mid-path, as in example.com/*.jpg