Commit a51f2e8
fix(web): create the QueryClient per-request on the server (#1575)
* fix(web): create the QueryClient per-request on the server
The QueryClient was created at module scope, so on the server one instance —
rooted for the process lifetime — was shared by every SSR render. Each render
deposits query state into its cache under request-derived keys (repos, file
paths, searches), and queries never become inactive server-side because
unmount/unsubscribe never run during SSR, so gcTime eviction never fires. The
cache anchored entire server render graphs (react-query observers, hook
state, CodeMirror editor state) at ~400 MiB/h of genuine retention, measured
in production by forcing a full GC via the inspector: 2073 -> 1826 MiB freed
only 247 MiB at a 2 GiB heap.
Follow TanStack Query's advanced SSR guidance: a fresh client per server
render (it dies with the request, releasing everything anchored to it) and a
module-scope singleton in the browser, deliberately not stored in React state
so it survives React suspending during the initial render.
This also removes a correctness defect the docs call out for module-scope
clients: one request's cached query data could previously be rendered into
another request's SSR HTML.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
* docs: add changelog entry for query client leak fix
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>1 parent 5e13dbb commit a51f2e8
2 files changed
Lines changed: 43 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
5 | 38 | | |
6 | | - | |
7 | | - | |
8 | 39 | | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
0 commit comments