docs: document Static FilesUnverified
772ff4d parent: 41ffd77 modified
README.md +66 -0 | @@ -18,6 +18,7 @@ Vocabulary (Invocation, Event, Warm Process...) is defined in [GLOSSARY.md](GLOS | ||
| 18 | 18 | - [Installation](#installation) |
| 19 | 19 | - [Configuration](#configuration) |
| 20 | 20 | - [Supported Applications](#supported-applications) |
| 21 | +- [Static Files](#static-files) | |
| 21 | 22 | - [CRON triggers and Command Invocations](#cron-triggers-and-command-invocations) |
| 22 | 23 | - [Packaging](#packaging) |
| 23 | 24 | - [Deployment](#deployment) |
| @@ -283,6 +284,71 @@ Other keys are dropped, and any other return value fails the Invocation with a 5 | ||
| 283 | 284 | Command Invocations are not routed: the raw callable gets their Event like any other. |
| 284 | 285 | An invokable object is not taken for a raw callable; return `$object(...)` to make it one. |
| 285 | 286 | |
| 287 | +## Static Files | |
| 288 | + | |
| 289 | +The runtime serves the files of `public/` itself, before building the Application: stylesheets, JavaScript, images, fonts, `favicon.ico`, `robots.txt`, and what AssetMapper or Webpack Encore compile there. | |
| 290 | +Such an Invocation pays no Kernel boot, and is answered even when the Application fails to build ([ADR 0009](docs/adr/0009-serve-static-files-from-the-function.md)). | |
| 291 | + | |
| 292 | +A file is served when: | |
| 293 | + | |
| 294 | +- the request is a `GET` or a `HEAD`; | |
| 295 | +- the path names a regular file under `<project_dir>/public`, a symbolic link leading out of it excluded; | |
| 296 | +- no segment of the path starts with a dot (`..`, `.env`, `.htaccess`), but for a leading `/.well-known/`; | |
| 297 | +- its extension is not `.php`. | |
| 298 | + | |
| 299 | +Every other request goes to the Application, directories included: there is no index file nor directory listing. | |
| 300 | + | |
| 301 | +> [!IMPORTANT] | |
| 302 | +> A file wins over a route of the same path. | |
| 303 | +> An application answering `/robots.txt` from a controller while shipping `public/robots.txt` gets the file: delete the file to keep the route. | |
| 304 | + | |
| 305 | +### Responses | |
| 306 | + | |
| 307 | +- The `Content-Type` comes from the extension, `application/octet-stream` for an unknown one, without charset. | |
| 308 | +- Each response carries `X-Content-Type-Options: nosniff`, and an `ETag` of the bytes it sends: a matching `If-None-Match` gets a `304` without body. | |
| 309 | +- `Cache-Control` is `public, max-age=0, must-revalidate`: browsers keep the file but check it on each use. | |
| 310 | +- It is `public, max-age=31536000, immutable` for a file whose name changes with its content: one that a `manifest.json` in its directory, or in one above, maps from another name. | |
| 311 | + AssetMapper's `public/assets/manifest.json` and Webpack Encore's `public/build/manifest.json` (with versioning) are such manifests. | |
| 312 | + Names alone are not trusted, as `font-awesome.css` would look as digested as `app-mH7q4W_.css`. | |
| 313 | +- `Range` requests get the whole file with a `200`. | |
| 314 | + | |
| 315 | +No CDN stands in front of a function: every request for a Static File is an Invocation, billed and possibly paying a cold start. | |
| 316 | +Long-lived caching of digested files is what spares most of them. | |
| 317 | + | |
| 318 | +### Compression | |
| 319 | + | |
| 320 | +A file is sent compressed when the client accepts it, in this order: | |
| 321 | + | |
| 322 | +1. a precompressed variant shipped next to it, `app.css.br` then `app.css.gz`, written by any build step; | |
| 323 | +2. otherwise, when the `static_compression` runtime option is set, the file compressed by the runtime. | |
| 324 | + | |
| 325 | +```json | |
| 326 | +{ | |
| 327 | + "extra": { | |
| 328 | + "runtime": { | |
| 329 | + "static_compression": "gzip" | |
| 330 | + } | |
| 331 | + } | |
| 332 | +} | |
| 333 | +``` | |
| 334 | + | |
| 335 | +`static_compression` is `false` by default, `"gzip"`, or `"br"`, which needs the `brotli` extension: any other value, or `"br"` without the extension, answers every Invocation with a 500 until fixed. | |
| 336 | +The Scaleway `php85` runtime does not load `brotli`, so use `"gzip"` there, and ship `.br` variants built beforehand for brotli. | |
| 337 | +The runtime only compresses textual files (`text/*`, JavaScript, JSON, XML, SVG, WebAssembly) of at least 1 KiB, and keeps the results in memory for the Warm Process, up to 32 MiB. | |
| 338 | + | |
| 339 | +The order of preference is the runtime's: only a `q=0` in `Accept-Encoding` refuses an encoding. | |
| 340 | +Compressed responses carry `Content-Encoding`, an `ETag` of their own and `Vary: Accept-Encoding`. | |
| 341 | +A request for `app.css.gz` itself gets that file, as any other. | |
| 342 | + | |
| 343 | +### Limits | |
| 344 | + | |
| 345 | +- A Static File must fit in one Function Response, base64-encoded when it is binary, which takes a third more. | |
| 346 | + Scaleway enforces no limit, but responses of 10 MiB and more intermittently fail with a `504` after 60 seconds ([measured](docs/probe-results.md#size-and-encodings)). | |
| 347 | + The runtime therefore refuses Static Files over 6 MiB in a Function Response: it answers a 500 and logs the file, and `scaleway-package` warns about such files. | |
| 348 | +- A file written at runtime under `public/`, such as an upload, is only served by the instance that wrote it, each instance having its own disk. | |
| 349 | +- Store uploads, large files and busy sites' assets on Object Storage, behind Edge Services or not, and point `framework.assets.base_urls` to it. | |
| 350 | +- `assets:install` boots the Kernel: at deploy time, run it from a `post-install-cmd` with an explicit env, `@php bin/console assets:install public --env=prod`, for the same reasons as [`cache:warmup`](#cache-and-writable-directories). | |
| 351 | + | |
| 286 | 352 | ## CRON triggers and Command Invocations |
| 287 | 353 | |
| 288 | 354 | ### CRON triggers |
| @@ -18,6 +18,7 @@ Vocabulary (Invocation, Event, Warm Process...) is defined in [GLOSSARY.md](GLOS | |||
| 18 | - [Installation](#installation) | 18 | - [Installation](#installation) |
| 19 | - [Configuration](#configuration) | 19 | - [Configuration](#configuration) |
| 20 | - [Supported Applications](#supported-applications) | 20 | - [Supported Applications](#supported-applications) |
| 21 | +- [Static Files](#static-files) | ||
| 21 | - [CRON triggers and Command Invocations](#cron-triggers-and-command-invocations) | 22 | - [CRON triggers and Command Invocations](#cron-triggers-and-command-invocations) |
| 22 | - [Packaging](#packaging) | 23 | - [Packaging](#packaging) |
| 23 | - [Deployment](#deployment) | 24 | - [Deployment](#deployment) |
| @@ -283,6 +284,71 @@ Other keys are dropped, and any other return value fails the Invocation with a 5 | |||
| 283 | Command Invocations are not routed: the raw callable gets their Event like any other. | 284 | Command Invocations are not routed: the raw callable gets their Event like any other. |
| 284 | An invokable object is not taken for a raw callable; return `$object(...)` to make it one. | 285 | An invokable object is not taken for a raw callable; return `$object(...)` to make it one. |
| 285 | 286 | ||
| 287 | +## Static Files | ||
| 288 | + | ||
| 289 | +The runtime serves the files of `public/` itself, before building the Application: stylesheets, JavaScript, images, fonts, `favicon.ico`, `robots.txt`, and what AssetMapper or Webpack Encore compile there. | ||
| 290 | +Such an Invocation pays no Kernel boot, and is answered even when the Application fails to build ([ADR 0009](docs/adr/0009-serve-static-files-from-the-function.md)). | ||
| 291 | + | ||
| 292 | +A file is served when: | ||
| 293 | + | ||
| 294 | +- the request is a `GET` or a `HEAD`; | ||
| 295 | +- the path names a regular file under `<project_dir>/public`, a symbolic link leading out of it excluded; | ||
| 296 | +- no segment of the path starts with a dot (`..`, `.env`, `.htaccess`), but for a leading `/.well-known/`; | ||
| 297 | +- its extension is not `.php`. | ||
| 298 | + | ||
| 299 | +Every other request goes to the Application, directories included: there is no index file nor directory listing. | ||
| 300 | + | ||
| 301 | +> [!IMPORTANT] | ||
| 302 | +> A file wins over a route of the same path. | ||
| 303 | +> An application answering `/robots.txt` from a controller while shipping `public/robots.txt` gets the file: delete the file to keep the route. | ||
| 304 | + | ||
| 305 | +### Responses | ||
| 306 | + | ||
| 307 | +- The `Content-Type` comes from the extension, `application/octet-stream` for an unknown one, without charset. | ||
| 308 | +- Each response carries `X-Content-Type-Options: nosniff`, and an `ETag` of the bytes it sends: a matching `If-None-Match` gets a `304` without body. | ||
| 309 | +- `Cache-Control` is `public, max-age=0, must-revalidate`: browsers keep the file but check it on each use. | ||
| 310 | +- It is `public, max-age=31536000, immutable` for a file whose name changes with its content: one that a `manifest.json` in its directory, or in one above, maps from another name. | ||
| 311 | + AssetMapper's `public/assets/manifest.json` and Webpack Encore's `public/build/manifest.json` (with versioning) are such manifests. | ||
| 312 | + Names alone are not trusted, as `font-awesome.css` would look as digested as `app-mH7q4W_.css`. | ||
| 313 | +- `Range` requests get the whole file with a `200`. | ||
| 314 | + | ||
| 315 | +No CDN stands in front of a function: every request for a Static File is an Invocation, billed and possibly paying a cold start. | ||
| 316 | +Long-lived caching of digested files is what spares most of them. | ||
| 317 | + | ||
| 318 | +### Compression | ||
| 319 | + | ||
| 320 | +A file is sent compressed when the client accepts it, in this order: | ||
| 321 | + | ||
| 322 | +1. a precompressed variant shipped next to it, `app.css.br` then `app.css.gz`, written by any build step; | ||
| 323 | +2. otherwise, when the `static_compression` runtime option is set, the file compressed by the runtime. | ||
| 324 | + | ||
| 325 | +```json | ||
| 326 | +{ | ||
| 327 | + "extra": { | ||
| 328 | + "runtime": { | ||
| 329 | + "static_compression": "gzip" | ||
| 330 | + } | ||
| 331 | + } | ||
| 332 | +} | ||
| 333 | +``` | ||
| 334 | + | ||
| 335 | +`static_compression` is `false` by default, `"gzip"`, or `"br"`, which needs the `brotli` extension: any other value, or `"br"` without the extension, answers every Invocation with a 500 until fixed. | ||
| 336 | +The Scaleway `php85` runtime does not load `brotli`, so use `"gzip"` there, and ship `.br` variants built beforehand for brotli. | ||
| 337 | +The runtime only compresses textual files (`text/*`, JavaScript, JSON, XML, SVG, WebAssembly) of at least 1 KiB, and keeps the results in memory for the Warm Process, up to 32 MiB. | ||
| 338 | + | ||
| 339 | +The order of preference is the runtime's: only a `q=0` in `Accept-Encoding` refuses an encoding. | ||
| 340 | +Compressed responses carry `Content-Encoding`, an `ETag` of their own and `Vary: Accept-Encoding`. | ||
| 341 | +A request for `app.css.gz` itself gets that file, as any other. | ||
| 342 | + | ||
| 343 | +### Limits | ||
| 344 | + | ||
| 345 | +- A Static File must fit in one Function Response, base64-encoded when it is binary, which takes a third more. | ||
| 346 | + Scaleway enforces no limit, but responses of 10 MiB and more intermittently fail with a `504` after 60 seconds ([measured](docs/probe-results.md#size-and-encodings)). | ||
| 347 | + The runtime therefore refuses Static Files over 6 MiB in a Function Response: it answers a 500 and logs the file, and `scaleway-package` warns about such files. | ||
| 348 | +- A file written at runtime under `public/`, such as an upload, is only served by the instance that wrote it, each instance having its own disk. | ||
| 349 | +- Store uploads, large files and busy sites' assets on Object Storage, behind Edge Services or not, and point `framework.assets.base_urls` to it. | ||
| 350 | +- `assets:install` boots the Kernel: at deploy time, run it from a `post-install-cmd` with an explicit env, `@php bin/console assets:install public --env=prod`, for the same reasons as [`cache:warmup`](#cache-and-writable-directories). | ||
| 351 | + | ||
| 286 | ## CRON triggers and Command Invocations | 352 | ## CRON triggers and Command Invocations |
| 287 | 353 | ||
| 288 | ### CRON triggers | 354 | ### CRON triggers |
modified
example/README.md +9 -0 | @@ -14,6 +14,15 @@ A minimal Symfony application running on Scaleway Serverless Functions through ` | ||
| 14 | 14 | | `GET /redirect` | A redirect to an absolute route URL | |
| 15 | 15 | | `POST /` | Where CRON Invocations land, with the trigger's arguments as JSON body | |
| 16 | 16 | |
| 17 | +The files of `public/` are [Static Files](../README.md#static-files), served by the runtime without building the Application: | |
| 18 | + | |
| 19 | +| File | What it shows | | |
| 20 | +| --- | --- | | |
| 21 | +| `/css/app.css` | A stylesheet, revalidated with its `ETag`; with `Accept-Encoding: gzip`, its precompressed `app.css.gz` | | |
| 22 | +| `/js/greeting.js` | A JavaScript module, as `text/javascript` | | |
| 23 | +| `/images/logo.png` | A binary file, base64-encoded in the Function Response | | |
| 24 | +| `/assets/app-Ex4mPl3.css` | A digested file as AssetMapper writes one, listed in `assets/manifest.json`, so cached for good | | |
| 25 | + | |
| 17 | 26 | Its `post-install-cmd` warms the prod cache when Scaleway installs the dependencies at deploy time, as the warmup needs no secret; without it, the first Invocation of each Warm Process builds the cache. |
| 18 | 27 | |
| 19 | 28 | It logs from the `info` level to stderr, which reaches Cockpit, through Symfony's fallback logger lowered in `config/services.yaml`, without Monolog. |
| @@ -14,6 +14,15 @@ A minimal Symfony application running on Scaleway Serverless Functions through ` | |||
| 14 | | `GET /redirect` | A redirect to an absolute route URL | | 14 | | `GET /redirect` | A redirect to an absolute route URL | |
| 15 | | `POST /` | Where CRON Invocations land, with the trigger's arguments as JSON body | | 15 | | `POST /` | Where CRON Invocations land, with the trigger's arguments as JSON body | |
| 16 | 16 | ||
| 17 | +The files of `public/` are [Static Files](../README.md#static-files), served by the runtime without building the Application: | ||
| 18 | + | ||
| 19 | +| File | What it shows | | ||
| 20 | +| --- | --- | | ||
| 21 | +| `/css/app.css` | A stylesheet, revalidated with its `ETag`; with `Accept-Encoding: gzip`, its precompressed `app.css.gz` | | ||
| 22 | +| `/js/greeting.js` | A JavaScript module, as `text/javascript` | | ||
| 23 | +| `/images/logo.png` | A binary file, base64-encoded in the Function Response | | ||
| 24 | +| `/assets/app-Ex4mPl3.css` | A digested file as AssetMapper writes one, listed in `assets/manifest.json`, so cached for good | | ||
| 25 | + | ||
| 17 | Its `post-install-cmd` warms the prod cache when Scaleway installs the dependencies at deploy time, as the warmup needs no secret; without it, the first Invocation of each Warm Process builds the cache. | 26 | Its `post-install-cmd` warms the prod cache when Scaleway installs the dependencies at deploy time, as the warmup needs no secret; without it, the first Invocation of each Warm Process builds the cache. |
| 18 | 27 | ||
| 19 | It logs from the `info` level to stderr, which reaches Cockpit, through Symfony's fallback logger lowered in `config/services.yaml`, without Monolog. | 28 | It logs from the `info` level to stderr, which reaches Cockpit, through Symfony's fallback logger lowered in `config/services.yaml`, without Monolog. |