skip to content

In Cloudflare Workers static assets, how does not_found_handling serve a single-page app, and how do its three values differ?

level: middleimportance: must knowfreq 45%

answer

  1. only matters on a miss
  2. the default does nothing special
  3. 200 shell versus 404 page
  4. nearest 404.html up the path
  5. navigations skip the script

basics

~20 s

In Workers static assets, not_found_handling sets the reply when no file matches: single-page-application serves /index.html with 200, 404-page the nearest 404.html with 404, and none, the default, an empty 404. With a Worker script and no route patterns, non-navigation misses reach the script first.

solid answer

~40 s

`assets.not_found_handling` only applies when a request matches no file. `single-page-application` rewrites the miss to `/index.html` and answers `200 OK`, so a deep link like `/settings/billing` loads the app shell and the client router takes over. `404-page` serves the nearest `404.html`, walking up from the requested path, with `404 Not Found`. `none`, the default, returns a null-body 404. When the Worker also has a `main` script and a compatibility date from 2025-04-01, only **navigation requests** (`Sec-Fetch-Mode: navigate`) get the fallback without running the Worker; a `fetch()` from the page that misses invokes the Worker, which can call `env.ASSETS.fetch(request)` to get the fallback, unless `run_worker_first` lists route patterns. Unlike Pages, Workers never infers the mode from the files present: you set it.

code

json · 9 lines
json
{
  "name": "ssg-docs",
  "compatibility_date": "2026-10-10",
  "assets": {
    "directory": "./dist",
    "not_found_handling": "404-page",
    "html_handling": "auto-trailing-slash"
  }
}

go deeper

for a junior

Recall the three values and their status codes: single-page-application gives index.html with 200, 404-page gives a 404.html with 404, and none is the default.

for a middle

Explain that the setting only applies to misses after html_handling, how 404-page picks the nearest file, and how a Worker script changes the order with the navigation-request rule.

for a senior

Diagnose the classic results: a deep link 404ing after a move from Pages, a broken image answered with HTML and 200, and a browser navigation to an API path returning the shell.

for a principal

Weigh SPA fallback against crawlability and cost: navigations served without code are free, but a static site needs real 404s, so the mode belongs in the team's site template.

## The problem it solves A **single-page application (SPA)** ships one HTML file, `index.html`, plus JavaScript that draws every screen in the browser. The URL `/settings/billing` exists only in the client-side router; there is no `settings/billing.html` on disk. If a user reloads that page or opens a shared link, the server receives a request for a path with no file behind it. Something has to answer with the app shell, or the user sees a 404. In a Worker with static assets, the key `assets.not_found_handling` in the Wrangler configuration decides that answer. It only applies to a **miss**, a request that matched no file after `html_handling` (default `auto-trailing-slash`) has tried `about.html` for `/about` and `index.html` for a folder. ## The three values | Value | On a miss | Status | Typical site | | --- | --- | --- | --- | | `none` (default) | null-body response, or the Worker runs if one exists | `404` | API Worker with a few files | | `single-page-application` | `/index.html` | `200 OK` | client-rendered app | | `404-page` | nearest `404.html` | `404 Not Found` | statically generated site | Details that interviewers probe: - **`single-page-application`** returns the shell with `200`, not a redirect, so the URL in the address bar stays `/settings/billing` and the router can read it. If `index.html` does not exist either, the reply is a null-body 404. - **`404-page`** looks for the *nearest* `404.html`: a miss under `/blog/` serves `blog/404.html` if it exists and falls back to the root `404.html` otherwise. With no `404.html` anywhere, it returns a null-body 404. - **`none`** keeps the plain behaviour. Pick it when the Worker script should decide every miss. ## What changes when the Worker has a script With no `main` script, the fallback above is the whole story. With a script, the platform has two candidates for a miss: the fallback page and your code. For compatibility dates from 2025-04-01 the `assets_navigation_prefers_asset_serving` flag is on by default and the rule is: 1. A request matching a file is served as the file. 2. A miss that is a **navigation request**, carrying the browser's `Sec-Fetch-Mode: navigate` header, gets the `not_found_handling` fallback without invoking the Worker. 3. Any other miss, such as `fetch("/api/me")` from the page or an image tag pointing at a missing file, invokes the Worker. 4. The Worker can return its own response or forward with `env.ASSETS.fetch(request)`; the binding applies `not_found_handling`, so a forwarded miss gets `index.html`. The point of rule 2 is cost: a client-heavy app reloads and deep-links constantly, and none of those navigations needs billable code. The consequence is that a browser *navigating* to an API path receives the HTML shell, while the same path fetched by script reaches the Worker. Setting `run_worker_first` to an array of route patterns replaces this header test with explicit routes, and then every unmatched miss outside those routes gets the fallback. ## Moving from Pages Pages looked at the deployed files and guessed: a `404.html` meant custom 404 pages, no `404.html` and an `index.html` meant SPA routing. Workers does not guess, to avoid accidental misconfiguration. A project moved from Pages with only `assets.directory` set therefore answers deep links with a 404 until someone adds: ```json { "name": "my-spa", "compatibility_date": "2026-10-10", "assets": { "directory": "./dist/client/", "not_found_handling": "single-page-application" } } ``` ## Pitfalls - In SPA mode with no script, a broken link to `/img/logo.png` returns `index.html` with `200`, which the browser then fails to decode as an image. The status code does not reveal the broken path, so check the response body or type when debugging. - Search engines can index the shell under every unknown URL; a static site should prefer `404-page` so real 404s exist. - The fallback is chosen per Worker: one `not_found_handling` value covers the whole assets directory. ## Debugging which branch answered When a URL returns something unexpected, establish which of the three branches produced it before changing config: 1. Check whether the path, after `html_handling`, names a real file in the build output. If it does, no fallback or script was involved. 2. Repeat the request with and without the `Sec-Fetch-Mode: navigate` header under `wrangler dev`. A different answer means the navigation rule is deciding. 3. Look for an invocation in the Worker's logs. No invocation and an HTML body means the fallback served it; an invocation means your code, or its call to `env.ASSETS.fetch()`, did.

  • Why can a missing image return HTML with status 200 in SPA mode?
    `single-page-application` rewrites every miss that reaches the asset layer to `/index.html`. With no Worker script, or with `run_worker_first` set to route patterns, that includes requests for missing images and scripts, so the browser gets the shell with `200` and fails to use it. The bug shows up as a broken image or a script parse error, not as a 404 in the network tab.
  • How does html_handling interact with not_found_handling?
    `html_handling` runs first and decides whether a path names an HTML file: the default `auto-trailing-slash` serves `/file` from `file.html` and `/folder/` from `folder/index.html`, redirecting other spellings with `307`. Only paths it cannot map are misses for `not_found_handling`. With `html_handling` set to `none`, only exact filenames such as `/file.html` match, so far more requests fall through to the fallback.
  • A Worker in SPA mode needs every miss to reach its code, as under the old behaviour. What are the options?
    Add the `assets_navigation_has_no_effect` compatibility flag, which turns off the navigation preference so navigations that miss also invoke the Worker, or use `run_worker_first: true`. Both make every such request a billed invocation, so they are worth it only when the code must see navigations, for example to render HTML on the server.

  • single-page-application redirects unknown routes to / with a 302
  • Workers detects an SPA from index.html automatically, as Pages did
  • 404-page always serves the root 404.html, whatever the path
  • The default value of not_found_handling is single-page-application
  • With a Worker script and default routing, every missed fetch() from the page gets index.html directly

Assumes a compatibility date from 2025-04-01, when assets_navigation_prefers_asset_serving became the default; before it every miss invoked the Worker script when one was present.