Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

entry-server.tsx

98 lines2.8 KB

1/**
2 * SSR entry for the hydration tests. Runs in a plain node subprocess so that
3 * `solid-js` resolves to its server build. Renders the fixture app and prints
4 * a JSON report (HTML with embedded hydration scripts, fetch counts, and the
5 * dehydrated-ish query states) on stdout.
6 */
7import { renderToStream } from '@solidjs/web'
8import { QueryClient } from '@tanstack/solid-query'
9import { App } from './App'
10import type { Query } from '@tanstack/solid-query'
11import type { FetchCounts } from './App'
12​
13interface QuerySnapshot {
14 queryKey: unknown
15 queryHash: string
16 state: unknown
18​
19/**
20 * The provider clears the cache when the render disposes (the SSR
21 * teardown), so query states are recorded live off cache events rather
22 * than read back after completion. States are immutable objects in
23 * query-core; keeping the latest per hash is an accurate snapshot.
24 */
25function trackSnapshots(client: QueryClient): Map<string, QuerySnapshot> {
26 const snapshots = new Map<string, QuerySnapshot>()
27 const record = (query: Query<any, any, any, any>) => {
28 snapshots.set(query.queryHash, {
29 queryKey: query.queryKey,
30 queryHash: query.queryHash,
31 state: query.state,
32 })
33 }
34 client.getQueryCache().subscribe((event) => {
35 if (event.type !== 'removed') record(event.query)
36 })
37 return snapshots
39​
40function renderApp(client: QueryClient, counts: FetchCounts): Promise<string> {
41 return new Promise<string>((resolve) => {
42 let out = ''
43 // Collected through pipe() rather than the thenable form so the fixture
44 // builds against any solid-js 2 beta (renderToStringAsync was removed
45 // after beta.29).
46 renderToStream(() => (
47 <App client={client} source="server" counts={counts} />
48 )).pipe({
49 write(payload: string) {
50 out += payload
51 },
52 end() {
53 resolve(out)
54 },
55 })
56 })
58​
59const client = new QueryClient()
60const counts: FetchCounts = {
61 fresh: 0,
62 stale: 0,
63 placeholder: 0,
64 prefetched: 0,
66const snapshots = trackSnapshots(client)
67const html = await renderApp(client, counts)
68​
69// The dispose-time teardown must have emptied the per-request cache.
70const cacheEmptyAfterDispose = client.getQueryCache().getAll().length === 0
71​
72// Second pass: same app on a client whose standard dehydrate filter
73// excludes the stale query β€” its registry entry must stay off the wire
74// while the others still ship.
75const filteredClient = new QueryClient({
76 defaultOptions: {
77 dehydrate: {
78 shouldDehydrateQuery: (query) => query.queryKey[0] !== 'stale',
79 },
80 },
81})
82const filteredCounts: FetchCounts = {
83 fresh: 0,
84 stale: 0,
85 placeholder: 0,
86 prefetched: 0,
88const filteredHtml = await renderApp(filteredClient, filteredCounts)
89​
90console.log(
91 JSON.stringify({
92 html,
93 counts,
94 queries: [...snapshots.values()],
95 cacheEmptyAfterDispose,
96 filteredHtml,
97 }),