Repository navigation
Commit c8ef58d
authored
perf(auth): cache auth pages in browsers and at the edge (#160)
Every auth page was `no-store`, so each view, back navigation and repeat
sign-in downloaded the whole page again. The pages also had no `ETag`,
so browsers couldn't revalidate them even when allowed to. Closes #34.
**Build-time validators.** `build.rs` now generates a `match` over the
embedded files. Each arm carries the file's strong `ETag` (base64url of
the first 16 bytes of its SHA-256), content type and cache class. A
request whose `If-None-Match` lists the tag, or `*`, gets a `304` with
no body and the same headers. A `304` to `HEAD` reports the file's real
`Content-Length`. Lookup no longer scans a slice, and nothing is hashed
or parsed at runtime.
**Explicit cache classes.** Handlers mark a response cacheable with
`Cached(CacheClass, T)`. `HttpSecurity::decorate` now always writes
`Cache-Control` from the enforced class, overwriting whatever the
handler set. A response without a class, one that sets a cookie, or any
status other than 200/304 is `no-store`.
| Class | `Cache-Control` | Used by |
| --- | --- | --- |
| `NoStore` | `no-store` | APIs, OAuth, errors, everything else |
| `Immutable` | `public, max-age=31536000, immutable` |
`/_app/immutable/*` (unchanged) |
| `Revalidate` | `private, no-cache` + `ETag` | pages,
`/_app/version.json` |
| `Public` | `public, max-age=3600` | discovery and JWKS (moved from a
hand-set header) |
`Public` responses always get `Access-Control-Allow-Origin: *` with no
credentials. Pages and bundles get no CORS headers, even for an approved
`Origin`. Pages stay out of shared caches: an edge-cached old page could
reference bundles a new binary no longer has. The CSP header and the
other security headers are unchanged. Pages now answer only
`GET`/`HEAD`; other methods get `405`, so a failed precondition never
needs a `412`.
CI's Rust lint job builds without the frontend. `build.rs` sets
`cfg(embedded_frontend)` when it embeds files, so the two cache classes
only assets use are expected to be dead code in that build.
`infra/README.md` documents the policy.
### Measurements
A Playwright/Chromium script on two previews, one from `main` and one
from this branch, counted the requests that reached auth (not served
from the browser cache). Four page visits in one browser:
| Visit | Before: requests · bytes | After: requests · bytes |
| --- | --- | --- |
| 1. `/login` (cold) | 15 · 101,224 | 15 · 101,299 |
| 2. `/login` again | 2 · 2,890 (`200` page) | 2 · 282 (`304` page, 43
B) |
| 3. `/register` | 3 · 3,187 | 3 · 3,229 |
| 4. `/login` again | 2 · 2,867 (`200` page) | 2 · 282 (`304` page, 43
B) |
Repeat page views now cost a 43-byte `304` instead of a full ~2.6 KB
page. Bundles were already cached by browsers. The remaining request per
view is `/config`, an API, so it stays `no-store`. A passkey sign-up,
sign-out and sign-in on the branch preview work end to end.
Not measured here:
- **Cloudflare's hit ratio.** Previews are private routes and bypass the
tunnel.
- **`http.server.request.duration` counts and the
`piquel.auth.assets.not_modified` counter.** These need the metrics
pipeline from #29, which isn't merged yet.
> [!NOTE]
> Written by Claude Opus 5.5 in Claude Code and reviewed by GPT 6.1 Sol
(via T3 Code)
---------1 parent 0881ec1 commit c8ef58d
11 files changed
Lines changed: 402 additions & 93 deletions
File tree
- apps/auth-service
- src
- oidc
- security
- docs/architecture
- infra
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
33 | 37 | | |
34 | 38 | | |
35 | 39 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
2 | 5 | | |
3 | 6 | | |
4 | 7 | | |
5 | 8 | | |
6 | 9 | | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
7 | 14 | | |
8 | 15 | | |
9 | 16 | | |
| |||
35 | 42 | | |
36 | 43 | | |
37 | 44 | | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
38 | 114 | | |
39 | 115 | | |
40 | | - | |
| 116 | + | |
41 | 117 | | |
42 | 118 | | |
43 | 119 | | |
| |||
53 | 129 | | |
54 | 130 | | |
55 | 131 | | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
67 | 136 | | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
| 137 | + | |
76 | 138 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
4 | 7 | | |
5 | | - | |
6 | | - | |
7 | | - | |
| 8 | + | |
| 9 | + | |
8 | 10 | | |
9 | 11 | | |
| 12 | + | |
10 | 13 | | |
11 | 14 | | |
12 | 15 | | |
13 | 16 | | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
18 | 17 | | |
19 | 18 | | |
20 | 19 | | |
| |||
32 | 31 | | |
33 | 32 | | |
34 | 33 | | |
35 | | - | |
36 | | - | |
37 | | - | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
38 | 76 | | |
39 | | - | |
| 77 | + | |
40 | 78 | | |
41 | | - | |
42 | | - | |
| 79 | + | |
| 80 | + | |
43 | 81 | | |
44 | 82 | | |
45 | | - | |
| 83 | + | |
46 | 84 | | |
47 | 85 | | |
48 | 86 | | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
60 | 92 | | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
80 | 140 | | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
81 | 148 | | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | 149 | | |
89 | 150 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
51 | | - | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
52 | 58 | | |
53 | 59 | | |
54 | 60 | | |
| |||
81 | 87 | | |
82 | 88 | | |
83 | 89 | | |
84 | | - | |
| 90 | + | |
85 | 91 | | |
86 | 92 | | |
87 | 93 | | |
| |||
90 | 96 | | |
91 | 97 | | |
92 | 98 | | |
93 | | - | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
94 | 103 | | |
95 | 104 | | |
96 | 105 | | |
| |||
0 commit comments