Skip to content

Commit 37107fb

Browse files
committed
docs: document the optional status filter of custom http headers
Describe the optional `status` key of `[[advanced.headers]]` entries, the responses custom headers never reach, the fallback page caveat and how a filtered entry overrides an earlier unfiltered one.
1 parent f0831d9 commit 37107fb

1 file changed

Lines changed: 48 additions & 3 deletions

File tree

‎src/v3/features/custom-http-headers.md‎

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ outline: deep
1010

1111
The Server HTTP response headers should be defined mainly as an [Array of Tables](https://toml.io/en/v1.0.0#array-of-tables).
1212

13-
Each table entry should have two key/value pairs:
13+
Each table entry should have the following key/value pairs:
1414

1515
- One `source` key containing a string _glob pattern_.
1616
- One `headers` key containing a [set or hash table](https://toml.io/en/v1.0.0#table) describing plain HTTP headers to apply.
17+
- An optional `status` key containing an array of HTTP response status codes.
1718

18-
A particular set of HTTP headers can only be applied when a `source` matches against the request URI.
19+
A particular set of HTTP headers can only be applied when a `source` matches against the request URI and, if `status` is defined, the response status code is one of its values.
1920

2021
> [!INFO] Custom HTTP headers take precedence over existing ones
2122
>
@@ -33,9 +34,21 @@ The source is a [Glob pattern](<https://en.wikipedia.org/wiki/Glob_(programming)
3334

3435
A set of valid plain [HTTP headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers) to be applied.
3536

37+
### Status
38+
39+
An optional array of [HTTP response status codes](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status) (numbers) that limits the entry to responses with one of those codes, for example `status = [200, 206, 304]`.
40+
41+
Without `status`, the headers apply regardless of the response status, including error responses like `404 Not Found`. This fits headers such as `Strict-Transport-Security`, but not caching headers: an `immutable` `Cache-Control` for `/assets/**` would also be sent with a `404` for a missing asset.
42+
43+
Custom headers are not added to responses that SWS returns before serving a file, such as [URL redirects](url-redirects.md), `405 Method Not Allowed` or maintenance mode responses, so a `status` of `301` or `503` never matches those.
44+
45+
To limit caching headers to successful file responses, include `304` next to `200` and `206`: a `304 Not Modified` updates the headers of the response stored by a cache ([RFC 9111, section 4.3.4](https://www.rfc-editor.org/rfc/rfc9111#section-4.3.4)).
46+
47+
SWS validates the codes at startup and fails to start on an empty array or a code outside `100` to `999`.
48+
3649
## Examples
3750

38-
Below are some examples of how to customize server HTTP headers in three variants.
51+
Below are some examples of how to customize server HTTP headers.
3952

4053
### One-line version
4154

@@ -69,3 +82,35 @@ Strict-Transport-Security = "max-age=63072000; includeSubDomains; preload"
6982
source = "**/*.{jpg,jpeg,png,ico,gif}"
7083
headers.Strict-Transport-Security = "max-age=63072000; includeSubDomains; preload"
7184
```
85+
86+
### Limit headers to specific response status codes
87+
88+
```toml
89+
[advanced]
90+
91+
[[advanced.headers]]
92+
source = "/assets/**"
93+
status = [200, 206, 304]
94+
headers = { Cache-Control = "public, max-age=31536000, immutable" }
95+
```
96+
97+
`GET /assets/app.css` returns `200 OK` with `cache-control: public, max-age=31536000, immutable`, while `GET /assets/missing.css` returns `404 Not Found` without the `immutable` directive.
98+
99+
> [!INFO] Fallback page
100+
>
101+
> With a [fallback page](error-pages.md#fallback-page-for-use-with-client-routers) configured, a `GET` for a missing file is answered with `200 OK` and the fallback page, so status filtering does not exclude it.
102+
103+
Entries apply in order and a later entry replaces a header set by an earlier one. Below, every `/assets/**` response gets `no-cache`, and the second entry replaces it with `immutable` for `200`, `206` and `304` responses only.
104+
105+
```toml
106+
[advanced]
107+
108+
[[advanced.headers]]
109+
source = "/assets/**"
110+
headers = { Cache-Control = "no-cache" }
111+
112+
[[advanced.headers]]
113+
source = "/assets/**"
114+
status = [200, 206, 304]
115+
headers = { Cache-Control = "public, max-age=31536000, immutable" }
116+
```

0 commit comments

Comments
 (0)