| 1 | # Static assets |
| 2 | |
| 3 | Static assets serve the files of a directory, with a Worker or without one. An |
| 4 | application uses them for the front end that a browser downloads: an HTML page, |
| 5 | a stylesheet, a script bundle, a font, or an image. `celld deploy` uploads the |
| 6 | directory with the deployment, so the files become part of the deployed version |
| 7 | and not a separate store. The bytes live in the fleet bucket, and a node copies |
| 8 | a 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/) |
| 10 | for the standard behavior. |
| 11 | |
| 12 | ## Example |
| 13 | |
| 14 | The [Static assets example](../../examples/static-assets) serves an HTML file |
| 15 | from the `public` directory without a Worker. A `_headers` file adds a response |
| 16 | header, and a `_redirects` file moves `/home` to `/`. |
| 17 | |
| 18 | <!-- celld-example: static-assets --> |
| 19 | |
| 20 | ## What a deployment uploads |
| 21 | |
| 22 | An `assets` block in the Wrangler configuration names the directory. celld |
| 23 | accepts five keys in that block: `directory`, `binding`, `html_handling`, |
| 24 | `not_found_handling`, and `run_worker_first`. It refuses any other key by name, |
| 25 | so an unsupported Cloudflare option stops the deployment instead of changing the |
| 26 | behavior in silence. A project that declares `assets` and no `main` is an |
| 27 | asset-only project, and such a project cannot set `run_worker_first` or declare |
| 28 | a binding, because it runs no code. The |
| 29 | [binding documentation](https://developers.cloudflare.com/workers/static-assets/binding/) |
| 30 | describes the same keys for Cloudflare. |
| 31 | |
| 32 | `celld deploy` reads each file, takes the SHA-256 digest of the exact bytes, and |
| 33 | stores the body once under that digest. Two identical files in one directory |
| 34 | therefore upload one body. A redeploy compares the digest with the stored object |
| 35 | and skips a body that the fleet already holds, so a build that changes one file |
| 36 | uploads one file. The command also assigns a content type from a fixed table of |
| 37 | file extensions. An unknown extension gets no `Content-Type` header at all, |
| 38 | which is the behavior that Wrangler produces. |
| 39 | |
| 40 | The upload produces an index beside the deployment manifest. The index maps each |
| 41 | path to its digest, its size, and its content type, and it carries the `assets` |
| 42 | configuration and the complete text of the `_headers` and `_redirects` files. |
| 43 | The manifest names the index and the checksum of the index. celld writes the |
| 44 | bodies first, then the index, then the manifest, and the deployment pointer |
| 45 | last. A node therefore cannot read a pointer whose bodies are still incomplete. |
| 46 | A node loads only the index at startup and fetches a body when a request asks |
| 47 | for it, so a large asset directory does not become a large startup cost. |
| 48 | |
| 49 | ## The routing decision |
| 50 | |
| 51 | A request meets the asset layer before it meets the Worker. celld considers only |
| 52 | a `GET` or a `HEAD` request for an asset, and it sends every other method to the |
| 53 | Worker. `run_worker_first` reverses the order. It takes `true`, or a list of up |
| 54 | to 100 route patterns in which a `!` prefix excludes a path, and a match sends |
| 55 | the request to the Worker with no asset lookup. The exact `/` pattern sends only |
| 56 | the 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 |  |
| 60 | |
| 61 | The `_redirects` rules run before any file lookup, in file order, and the first |
| 62 | match 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 |
| 64 | restarts at that path, so the client sees the destination content under the |
| 65 | original 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. |
| 71 | When 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/) |
| 74 | lists the same four modes. |
| 75 | |
| 76 | A miss has one more step that Cloudflare does not need. celld re-reads the |
| 77 | deployment pointer, at most once in five seconds, because a rolling restart can |
| 78 | leave one node on an older index while an upgraded node serves HTML that names a |
| 79 | newer content-hashed file. A newer index therefore answers the request instead |
| 80 | of a broken page. |
| 81 | |
| 82 | `not_found_handling` fabricates the last answer. `single-page-application` |
| 83 | returns `/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/). |
| 86 | celld applies this step only when the deployment has no Worker, or when the |
| 87 | request is a navigation request. A deployment with a Worker therefore keeps a |
| 88 | non-navigation miss for the Worker, and the Worker answers it. When nothing |
| 89 | matches and there is no Worker, the node answers `404`. |
| 90 | |
| 91 | One failure never reaches the Worker. A body that the fleet bucket cannot supply |
| 92 | gives `502`, because a silent fall through would run the Worker for a request |
| 93 | that an asset owns. |
| 94 | |
| 95 | ## How a node serves a file |
| 96 | |
| 97 | A node fetches a body once and writes it to a local disk cache. It verifies the |
| 98 | digest 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 |
| 100 | when it crosses the bound, so the cache is a copy and never the durable record. |
| 101 | The fleet bucket holds the durable bytes. |
| 102 | |
| 103 | The `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 |
| 105 | new tag. celld sends no `Last-Modified` header and reads no `If-Modified-Since` |
| 106 | header, so revalidation uses the tag. A `Range` header selects one byte range |
| 107 | and gives `206`, an unsatisfiable range gives `416`, and a `HEAD` request keeps |
| 108 | the real `Content-Length` with an empty body. celld also sends |
| 109 | `Cache-Control: public, max-age=0, must-revalidate` on a plain request, which |
| 110 | makes a browser revalidate each time. |
| 111 | |
| 112 | celld runs no edge cache and no content delivery network, so this response path |
| 113 | is the whole of it. Cloudflare caches an asset in the location that serves the |
| 114 | request and pulls a miss from a nearby cache. A celld fleet has only the |
| 115 | machines that you run. Latency comes from your own placement, and a `_headers` |
| 116 | rule that sets a long `Cache-Control` on a content-hashed file is the way to |
| 117 | keep a browser from asking again. |
| 118 | |
| 119 | A `binding` name puts the same serving code behind `env.ASSETS.fetch()`. The |
| 120 | binding takes a `Request`, a `URL`, or a string, and `fetch()` is its only |
| 121 | method. It answers `405` for a method other than `GET` or `HEAD`, and it always |
| 122 | applies `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 | |
| 146 | The [Cloudflare compatibility](../cloudflare-compat.md#services) page |
| 147 | lists the runtime APIs and the unsupported services. |