Diffinery

homeflare/celldpublic

celld/docs/services/static-assets.md
113 lines · 5.4 KBRaw History
1# Static assets
2
3Static assets serve the files of a directory, with a Worker or without one.
4`celld deploy` uploads the directory to the fleet bucket with the deployment,
5and a node caches a file on its own disk when it first serves that file. Read
6the
7[Cloudflare Static Assets documentation](https://developers.cloudflare.com/workers/static-assets/)
8for the standard behavior.
9
10## Example
11
12The [Static assets example](../../examples/static-assets) serves an HTML file
13from the `public` directory without a Worker. A `_headers` file adds a response
14header, and a `_redirects` file moves `/home` to `/`.
15
16<!-- celld-example: static-assets -->
17
18## API
19
20The `assets` block configures the directory and the routing:
21
22- `directory` names the directory to upload.
23- `binding` exposes `env.ASSETS.fetch()` to a Worker.
24- `html_handling` maps a path to an HTML file.
25- `not_found_handling` selects `single-page-application` or `404-page`.
26- `run_worker_first` sends a matching request to the Worker first.
27- A `_headers` file and a `_redirects` file in the directory set headers and
28 redirects.
29
30## Deployment
31
32The `assets` block accepts `directory`, `binding`, `html_handling`,
33`not_found_handling`, and `run_worker_first`. Any other key stops the
34deployment. A project with `assets` and no `main` cannot set `run_worker_first`
35or declare a binding.
36
37`celld deploy` stores each file body once under its SHA-256 digest, and a
38redeploy uploads only the changed bodies. A file with an unknown extension gets
39no `Content-Type` header, as with Wrangler.
40
41## Routing
42
43celld looks up an asset only for a `GET` or a `HEAD` request. Every other
44method goes to the Worker. `run_worker_first` takes `true`, or a list of up to
45100 route patterns in which a `!` prefix excludes a path. A match sends the
46request to the Worker first. The exact `/` pattern matches only the root path.
47
48![The routing decision for one request: the method test, run_worker_first, the _redirects rules, the html_handling lookup, the pointer re-read after a miss, not_found_handling, and the fall through to the Worker](static-assets-flow.svg)
49
50The `_redirects` rules run before any file lookup, and the first match wins. A
51rule with status 301, 302, 303, 307, or 308 answers with a `Location` header. A
52rule with status 200 restarts routing at the local path and keeps the original
53URL.
54
55`html_handling` is `auto-trailing-slash` by default, which maps `/about` to
56`/about.html` and `/about/` to `/about/index.html`. The other modes are
57`force-trailing-slash`, `drop-trailing-slash`, and `none`. A non-canonical
58spelling gets a `307` to the canonical path with the query string.
59
60After a miss, a node re-reads the deployment pointer at most once in five
61seconds. During a rolling restart, a newer index can then serve a file that an
62upgraded node references.
63
64`not_found_handling` set to `single-page-application` returns `/index.html`
65with status 200, and `404-page` returns the nearest `404.html` with status 404.
66With a Worker, celld applies it only to a navigation request, and the Worker
67answers any other miss. With no Worker and no match, the node answers `404`. A
68body that the fleet bucket cannot supply gives `502` and never reaches the
69Worker.
70
71## Responses
72
73The disk cache holds 512 MiB by default, and `CELLD_ASSET_CACHE_BYTES` changes
74it. The cache evicts the least recently used file.
75
76The `ETag` is the strong SHA-256 digest of the body, so `If-None-Match` gives
77`304`. A `Range` header gives `206`, an unsatisfiable range gives `416`, and a
78`HEAD` response keeps the real `Content-Length`. A plain response carries
79`Cache-Control: public, max-age=0, must-revalidate`. Use a `_headers` rule to
80set a long `Cache-Control` on a content-hashed file.
81
82A `binding` exposes `env.ASSETS.fetch()`, which takes a `Request`, a `URL`, or a
83string. It answers `405` for a method other than `GET` or `HEAD`, and it always
84applies `not_found_handling`.
85
86## Differences from Cloudflare
87
88- celld does not compress an asset response. Use a compressing ingress proxy
89 when a client needs gzip or brotli.
90- celld has no edge cache. Each node keeps a 512 MiB disk cache, configured by
91 `CELLD_ASSET_CACHE_BYTES`, and requires browser revalidation.
92- A node checks the deployment pointer after an asset miss at most once in five
93 seconds.
94- celld reads `If-None-Match` and `If-Range`. It sends no `Last-Modified` header
95 and reads no `If-Modified-Since` header.
96- A `_headers` file cannot change `connection`, `content-length`, or
97 `transfer-encoding`.
98- A deployment can contain 20,000 assets and 1 GiB in total. A file can be at
99 most 25 MiB. `CELLD_MAX_ASSET_FILE_BYTES` changes the per-file limit, and it
100 must have the same value on the deployment builder, the managed deployment
101 agent, and the serving nodes. A process reads it once, so a new value needs
102 a restart. An error for a file that is too large reports its path, its size,
103 and the limit. Each `_headers` or `_redirects` file has a 100 KiB limit.
104- `celld deploy` accepts only `directory`, `binding`, `html_handling`,
105 `not_found_handling`, and `run_worker_first` in the `assets` block.
106- `celld deploy` refuses a `.assetsignore` file, and it stops the deployment
107 instead of ignoring the file. It also refuses a symbolic link, a special
108 file, a non-UTF-8 name, an unsafe decoded path, and a `_worker.js` entry.
109- The assets binding has one method, `fetch()`. celld supplies no `unstable_`
110 helper.
111
112The [Cloudflare compatibility](../cloudflare-compat.md#services) page
113lists the runtime APIs and the unsupported services.