Diffinery

homeflare/celldpublic

celld/docs/services/static-assets.md
147 lines · 8.3 KBRaw History
1# Static assets
2
3Static assets serve the files of a directory, with a Worker or without one. An
4application uses them for the front end that a browser downloads: an HTML page,
5a stylesheet, a script bundle, a font, or an image. `celld deploy` uploads the
6directory with the deployment, so the files become part of the deployed version
7and not a separate store. The bytes live in the fleet bucket, and a node copies
8a file to its own disk cache when it first serves that file. Read the
9[Cloudflare Static Assets documentation](https://developers.cloudflare.com/workers/static-assets/)
10for the standard behavior.
11
12## Example
13
14The [Static assets example](../../examples/static-assets) serves an HTML file
15from the `public` directory without a Worker. A `_headers` file adds a response
16header, and a `_redirects` file moves `/home` to `/`.
17
18<!-- celld-example: static-assets -->
19
20## What a deployment uploads
21
22An `assets` block in the Wrangler configuration names the directory. celld
23accepts five keys in that block: `directory`, `binding`, `html_handling`,
24`not_found_handling`, and `run_worker_first`. It refuses any other key by name,
25so an unsupported Cloudflare option stops the deployment instead of changing the
26behavior in silence. A project that declares `assets` and no `main` is an
27asset-only project, and such a project cannot set `run_worker_first` or declare
28a binding, because it runs no code. The
29[binding documentation](https://developers.cloudflare.com/workers/static-assets/binding/)
30describes the same keys for Cloudflare.
31
32`celld deploy` reads each file, takes the SHA-256 digest of the exact bytes, and
33stores the body once under that digest. Two identical files in one directory
34therefore upload one body. A redeploy compares the digest with the stored object
35and skips a body that the fleet already holds, so a build that changes one file
36uploads one file. The command also assigns a content type from a fixed table of
37file extensions. An unknown extension gets no `Content-Type` header at all,
38which is the behavior that Wrangler produces.
39
40The upload produces an index beside the deployment manifest. The index maps each
41path to its digest, its size, and its content type, and it carries the `assets`
42configuration and the complete text of the `_headers` and `_redirects` files.
43The manifest names the index and the checksum of the index. celld writes the
44bodies first, then the index, then the manifest, and the deployment pointer
45last. A node therefore cannot read a pointer whose bodies are still incomplete.
46A node loads only the index at startup and fetches a body when a request asks
47for it, so a large asset directory does not become a large startup cost.
48
49## The routing decision
50
51A request meets the asset layer before it meets the Worker. celld considers only
52a `GET` or a `HEAD` request for an asset, and it sends every other method to the
53Worker. `run_worker_first` reverses the order. It takes `true`, or a list of up
54to 100 route patterns in which a `!` prefix excludes a path, and a match sends
55the request to the Worker with no asset lookup. The exact `/` pattern sends only
56the root path to the Worker. Cloudflare describes the same control in its
57[Worker script routing documentation](https://developers.cloudflare.com/workers/static-assets/routing/worker-script/).
58
59![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)
60
61The `_redirects` rules run before any file lookup, in file order, and the first
62match wins. A rule with status 301, 302, 303, 307, or 308 answers with a
63`Location` header. A rule with status 200 names a local path, and routing
64restarts at that path, so the client sees the destination content under the
65original URL.
66
67`html_handling` then decides which file a path can reach.
68`auto-trailing-slash` is the default, and it maps `/about` to `/about.html` and
69`/about/` to `/about/index.html`. `force-trailing-slash` and
70`drop-trailing-slash` pick one spelling, and `none` matches the exact path only.
71When the request uses a spelling that is not the canonical one, celld answers
72`307` to the canonical path and keeps the query string. The
73[html_handling documentation](https://developers.cloudflare.com/workers/static-assets/routing/advanced/html-handling/)
74lists the same four modes.
75
76A miss has one more step that Cloudflare does not need. celld re-reads the
77deployment pointer, at most once in five seconds, because a rolling restart can
78leave one node on an older index while an upgraded node serves HTML that names a
79newer content-hashed file. A newer index therefore answers the request instead
80of a broken page.
81
82`not_found_handling` fabricates the last answer. `single-page-application`
83returns `/index.html` with status 200, and `404-page` returns the nearest
84`404.html` with status 404, which
85[Cloudflare also does](https://developers.cloudflare.com/workers/static-assets/routing/single-page-application/).
86celld applies this step only when the deployment has no Worker, or when the
87request is a navigation request. A deployment with a Worker therefore keeps a
88non-navigation miss for the Worker, and the Worker answers it. When nothing
89matches and there is no Worker, the node answers `404`.
90
91One failure never reaches the Worker. A body that the fleet bucket cannot supply
92gives `502`, because a silent fall through would run the Worker for a request
93that an asset owns.
94
95## How a node serves a file
96
97A node fetches a body once and writes it to a local disk cache. It verifies the
98digest after the write, and it keeps the cache under a bound of 512 MiB that
99`CELLD_ASSET_CACHE_BYTES` changes. The cache evicts the least recently used file
100when it crosses the bound, so the cache is a copy and never the durable record.
101The fleet bucket holds the durable bytes.
102
103The `ETag` is the strong SHA-256 digest of the body. A conditional `GET` with
104`If-None-Match` therefore gives `304` with no body, and a changed file gives a
105new tag. celld sends no `Last-Modified` header and reads no `If-Modified-Since`
106header, so revalidation uses the tag. A `Range` header selects one byte range
107and gives `206`, an unsatisfiable range gives `416`, and a `HEAD` request keeps
108the real `Content-Length` with an empty body. celld also sends
109`Cache-Control: public, max-age=0, must-revalidate` on a plain request, which
110makes a browser revalidate each time.
111
112celld runs no edge cache and no content delivery network, so this response path
113is the whole of it. Cloudflare caches an asset in the location that serves the
114request and pulls a miss from a nearby cache. A celld fleet has only the
115machines that you run. Latency comes from your own placement, and a `_headers`
116rule that sets a long `Cache-Control` on a content-hashed file is the way to
117keep a browser from asking again.
118
119A `binding` name puts the same serving code behind `env.ASSETS.fetch()`. The
120binding takes a `Request`, a `URL`, or a string, and `fetch()` is its only
121method. It answers `405` for a method other than `GET` or `HEAD`, and it always
122applies `not_found_handling`, so a Worker can render a fallback page itself.
123
124## Differences from Cloudflare
125
126- celld does not compress an asset response. Use a compressing ingress proxy
127 when a client needs gzip or brotli.
128- celld has no edge cache. Each node keeps a 512 MiB disk cache, configured by
129 `CELLD_ASSET_CACHE_BYTES`, and requires browser revalidation.
130- A node checks the deployment pointer after an asset miss at most once in five
131 seconds.
132- celld reads `If-None-Match` and `If-Range`. It sends no `Last-Modified` header
133 and reads no `If-Modified-Since` header.
134- A `_headers` file cannot change `connection`, `content-length`, or
135 `transfer-encoding`.
136- A deployment can contain 20,000 assets, 25 MiB for each asset, and 1 GiB in
137 total. Each `_headers` or `_redirects` file has a 100 KiB limit.
138- `celld deploy` accepts only `directory`, `binding`, `html_handling`,
139 `not_found_handling`, and `run_worker_first` in the `assets` block.
140- `celld deploy` refuses a `.assetsignore` file, and it stops the deployment
141 instead of ignoring the file. It also refuses a symbolic link, a special
142 file, a non-UTF-8 name, an unsafe decoded path, and a `_worker.js` entry.
143- The assets binding has one method, `fetch()`. celld supplies no `unstable_`
144 helper.
145
146The [Cloudflare compatibility](../cloudflare-compat.md#services) page
147lists the runtime APIs and the unsupported services.