Routing
Assets are served by attempting to match up the incoming request's pathname to a static asset. The structure and organization of files in your static asset directory, along with any routing configuration, determine the routing paths for your application. When a request invokes a Worker with assets:
- If a request is found with a matching path to the current route requested then that asset will always be served.
In this example, request to example.com/blog
serves the blog.html
file.
- If there is no Worker script,
not_found_handling
will be evaluated. By default, a null-body 404-status response will be served.
If a Worker is configured, and there are no assets that match the current route requested, the Worker will be invoked. The Worker can then "fall back" to not_found_handling
asset behavior, by passing the incoming request through to the asset binding.
In this example, request to example.com/api
doesn't match a static asset so the Worker is invoked.
There are two options for asset serving that can be configured in wrangler.toml
:
Forcing or dropping trailing slashes on request paths (for example, example.com/page/
vs. example.com/page
) is often something that developers wish to control for cosmetic reasons. Additionally, it can impact SEO because search engines often treat URLs with and without trailing slashes as different, separate pages. This distinction can lead to duplicate content issues, indexing problems, and overall confusion about the correct canonical version of a page.
html_handling
configuration determines the redirects and rewrites of requests for HTML content. It is used to specify the pattern for canonical URLs, thus where Cloudflare serves HTML content from, and additionally, where Cloudflare redirects non-canonical URLs to.
In the event a request does not match a static asset, and there is no Worker script (or that Worker script calls fetch()
on the assets binding), not_found_handling
determines how Cloudflare will construct a response.
It can be used to serve single-page applications (SPAs), or to serve custom 404 HTML pages.
If creating a SPA, place an /index.html
in your asset directory. When not_found_handling
is configured to "single-page-application"
, this page will be served with a 200 response.
If you have custom 404 HTML pages, and configure not_found_handling
to "404-page"
, Cloudflare will recursively navigate up by pathname segment to serve the nearest 404.html
file. For example, you can have a /404.html
and /blog/404.html
file, and Cloudflare will serve the /blog/404.html
file for requests to /blog/not-found
and the /404.html
file for requests to /foo/bar
.
"auto-trailing-slash"
is the default mode if html_handling
is not explicitly specified.
Take the following directory structure:
Based on the incoming requests, the following assets would be served:
Incoming Request | Response | Asset Served |
---|---|---|
/file | 200 | /file.html served |
/file.html | 307 to /file | - |
/file/ | 307 to /file | - |
/file/index | 307 to /file | - |
/file/index.html | 307 to /file | - |
/folder | 307 to /folder/ | - |
/folder.html | 307 to /folder/ | - |
/folder/ | 200 | /folder/index.html |
/folder/index | 307 /folder/ | - |
/folder/index.html | 307 /folder/ | - |
"none"
is the default mode if not_found_handling
is not explicitly specified.
For all non-matching requests, Cloudflare will return a null-body 404-status response.
Alternate configuration options are outlined on this page and can be specified in your project's wrangler.toml
file. If you're deploying using a framework, these options will be defined by the framework provider.
Example wrangler.toml
configuration:
Take the following directory structure:
html_handling: "auto-trailing-slash"
Based on the incoming requests, the following assets would be served:
Incoming Request | Response | Asset Served |
---|---|---|
/file | 200 | /file.html |
/file.html | 307 to /file | - |
/file/ | 307 to /file | - |
/file/index | 307 to /file | - |
/file/index.html | 307 to /file | - |
/folder | 307 to /folder | - |
/folder.html | 307 to /folder | - |
/folder/ | 200 | /folder/index.html |
/folder/index | 307 /folder | - |
/folder/index.html | 307 /folder | - |
html_handling: "force-trailing-slash"
Based on the incoming requests, the following assets would be served:
Incoming Request | Response | Asset Served |
---|---|---|
/file | 307 to /file/ | - |
/file.html | 307 to /file/ | - |
/file/ | 200 | /file.html |
/file/index | 307 to /file/ | - |
/file/index.html | 307 to /file/ | - |
/folder | 307 to /folder/ | - |
/folder.html | 307 to /folder/ | - |
/folder/ | 200 | /folder/index.html |
/folder/index | 307 /folder/ | - |
/folder/index.html | 307 /folder/ | - |
html_handling: "drop-trailing-slash"
Based on the incoming requests, the following assets would be served:
Incoming Request | Response | Asset Served |
---|---|---|
/file | 200 | /file.html |
/file.html | 307 to /file | - |
/file/ | 307 to /file | - |
/file/index | 307 to /file | - |
/file/index.html | 307 to /file | - |
/folder | 200 | /folder/index.html |
/folder.html | 307 to /folder | - |
/folder/ | 307 to /folder | - |
/folder/index | 307 /folder | - |
/folder/index.html | 307 /folder | - |
html_handling: "none"
Based on the incoming requests, the following assets would be served:
Incoming Request | Response | Asset Served |
---|---|---|
/file | Depends on not_found_handling | Depends on not_found_handling |
/file.html | 200 | /file.html |
/file/ | Depends on not_found_handling | Depends on not_found_handling |
/file/index | Depends on not_found_handling | Depends on not_found_handling |
/file/index.html | Depends on not_found_handling | Depends on not_found_handling |
/folder | Depends on not_found_handling | Depends on not_found_handling |
/folder.html | Depends on not_found_handling | Depends on not_found_handling |
/folder/ | Depends on not_found_handling | Depends on not_found_handling |
/folder/index | Depends on not_found_handling | Depends on not_found_handling |
/folder/index.html | 200 | /folder/index.html |
Take the following directory structure:
not_found_handling: "none"
not_found_handling: "404-page"
not_found_handling: "single-page-application"