Single Page Application (SPA)
Single Page Applications (SPAs) are web applications which are client-side rendered (CSR). They are often built with a framework such as React, Vue or Svelte. The build process of these frameworks will produce a single /index.html
file and accompanying client-side resources (e.g. JavaScript bundles, CSS stylesheets, images, fonts, etc.). Typically, data is fetched by the client from an API with client-side requests.
In order to deploy a Single Page Application to Workers, you must configure the assets.directory
and assets.not_found_handling
options in your Wrangler configuration file:
{ "name": "my-worker", "compatibility_date": "2025-06-06", "assets": { "directory": "./dist/", "not_found_handling": "single-page-application" }}
name = "my-worker"compatibility_date = "2025-06-06"
[assets]directory = "./dist/"not_found_handling = "single-page-application"
Configuring assets.not_found_handling
to single-page-application
overrides the default serving behavior of Workers for static assets. When an incoming request does not match a file in the assets.directory
, Workers will serve the contents of the /index.html
file with a 200 OK
status.
If you have a Worker script (main
), have configured assets.not_found_handling
, and use the assets_navigation_prefers_asset_serving
compatibility flag (or set a compatibility date of 2025-04-01
or greater), navigation requests will not invoke the Worker script. A navigation request is a request made with the Sec-Fetch-Mode: navigate
header, which browsers automatically attach when navigating to a page. This reduces billable invocations of your Worker script, and is particularly useful for client-heavy applications which would otherwise invoke your Worker script very frequently and unnecessarily.
In some cases, you might need to pass a value from a navigation request to your Worker script. For example, if you are acting as an OAuth callback, you might expect to see requests made to some route such as /oauth/callback?code=...
. With the assets_navigation_prefers_asset_serving
flag, your HTML assets will be server, rather than your Worker script. In this case, we recommend, either as part of your client application for this appropriate route, or with a slimmed-down endpoint-specific HTML file, passing the value to the server with client-side JavaScript.
<!DOCTYPE html><html> <head> <title>OAuth callback</title> </head> <body> <p>Loading...</p> <script> (async () => { const response = await fetch("/api/oauth/callback" + window.location.search); if (response.ok) { window.location.href = '/'; } else { document.querySelector('p').textContent = 'Error: ' + (await response.json()).error; } })(); </script> </body></html>