Bindings
assets
The static files deployed with the code.
json
"bindings": { "ASSETS": "assets" }Files under public/ in a deployment are not modules: the assets binding
serves them as they are, when the service's code passes it a request.
js
return env.ASSETS.fetch(request) // the file of public/ at the request's path, or a 404/x serves public/x, public/x.html or public/x/index.html; a path
ending in / serves its index.html. The request's range and conditions
apply (206, 304), with an ETag and Cache-Control: no-cache. During a
rollout each version serves its own files.
A static site
Every service has code. A static site is one line of it:
js
// index.js, with "bindings": {"ASSETS": "assets"}
export default { fetch: (request, env) => env.ASSETS.fetch(request) }A single-page app
Your code decides the rest: an app with its own routes answers the paths that are no file with its page.
js
export default {
async fetch(request, env) {
const res = await env.ASSETS.fetch(request)
return res.status === 404 && request.method === "GET"
? env.ASSETS.fetch(new URL("/", request.url))
: res
},
}