Open History to see the last commit
1/**
2 * Shared fixture app for the SSR β hydration tests.
3 *
4 * This module is compiled twice by the test harness (see
5 * `src/__tests__/hydration.test.tsx`): once with the Solid SSR transform
6 * (consumed by `entry-server.tsx`) and once with the hydratable DOM
7 * transform (consumed by `entry-client.tsx`).
8 */
9import { Loading, Show } from 'solid-js'
10import {
11 QueryClientProvider,
12 useIsFetching,
13 useQuery,
14} from '@tanstack/solid-query'
15import type { QueryClient } from '@tanstack/solid-query'
16β
17const isServer = typeof window === 'undefined'
18β
19export interface FetchCounts {
20 fresh: number
21 stale: number
22 placeholder: number
23 prefetched: number
24}
25β
26export interface AppProps {
27 client: QueryClient
28 /** Marker baked into the query data so tests can tell where it was fetched. */
29 source: 'server' | 'client'
30 counts: FetchCounts
31 /** Toggled by tests after hydration to mount a subtree that was never
32 * rendered on the server β its query must adopt the server's prefetched
33 * payload from the registry instead of refetching. */
34 lateMount?: () => boolean
35}
36β
37const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
38β
39function Queries(props: AppProps) {
40 // Fresh for a minute: after hydration this must NOT refetch on mount.
41 const fresh = useQuery(() => ({
42 queryKey: ['fresh'],
43 queryFn: async () => {
44 props.counts.fresh++
45 await sleep(5)
46 return `fresh-${props.source}`
47 },
48 staleTime: 60_000,
49 }))
50β
51 // Cross-cache aggregate: must serialize the hydration-time truth (0 β
52 // the hydrating client's fetches are held and primed entries are
53 // settled) and stay latched there through the hydration window.
54 const fetching = useIsFetching()
55β
56 // Immediately stale: normal staleness rules mean this refetches on mount.
57 const stale = useQuery(() => ({
58 queryKey: ['stale'],
59 queryFn: async () => {
60 props.counts.stale++
61 await sleep(5)
62 return `stale-${props.source}`
63 },
64 staleTime: 0,
65 }))
66β
67 // Placeholder short-circuit: the data compute serves the placeholder
68 // before the fetch-pull branch, so this must NOT fetch during SSR β the
69 // placeholder itself is the serialized output (no boundary hold, raw
70 // meta stays 'pending' which is its settled SSR truth), and the client
71 // fetches for real only after its hydration window closes.
72 const placeholder = useQuery(() => ({
73 queryKey: ['placeholder'],
74 queryFn: async () => {
75 props.counts.placeholder++
76 await sleep(5)
77 return `placeholder-resolved-${props.source}`
78 },
79 placeholderData: 'placeholder-value',
80 staleTime: 60_000,
81 }))
82β
83 return (
84 <div>
85 <span id="fresh">{fresh.data}</span>
86 <span id="stale">{stale.data}</span>
87 {/* Meta guards: boundaries serialize settled state only, so these
88 must show settled values in the server HTML β a transient
89 ('pending', fetching) here is a hydration mismatch in waiting. */}
90 <span id="meta">
91 {fresh.status}|{String(fresh.isFetching)}|{String(fresh.isSuccess)}|
92 {String(fresh.isFetchedAfterMount)}
93 </span>
94 <span id="global">{fetching()}</span>
95 <span id="ph">
96 {placeholder.data}|{String(placeholder.isPlaceholderData)}|
97 {placeholder.status}
98 </span>
99 </div>
100 )
101}
102β
103/** Never rendered on the server β mounted by tests after hydration. Its
104 * query was prefetched (and only prefetched) during SSR; the hash-keyed
105 * registry entry must satisfy it with zero client fetches. */
106function LateConsumer(props: AppProps) {
107 const late = useQuery(() => ({
108 queryKey: ['prefetched'],
109 queryFn: async () => {
110 props.counts.prefetched++
111 await sleep(5)
112 return `prefetched-${props.source}`
113 },
114 staleTime: 60_000,
115 }))
116 return <span id="late">{late.data}</span>
117}
118β
119export function App(props: AppProps) {
120 // Cache coverage beyond the rendered tree: prefetch a query no component
121 // reads during this render. Fired synchronously during setup, so the
122 // provider's serializer catches the fetch dispatch while the request's
123 // serialization context is live.
124 if (isServer) {
125 void props.client.prefetchQuery({
126 queryKey: ['prefetched'],
127 queryFn: async () => {
128 props.counts.prefetched++
129 await sleep(5)
130 return `prefetched-${props.source}`
131 },
132 })
133 }
134 return (
135 <QueryClientProvider client={props.client}>
136 <Loading fallback={<div>loading</div>}>
137 <Queries {...props} />
138 <Show when={props.lateMount?.()}>
139 <LateConsumer {...props} />
140 </Show>
141 </Loading>
142 </QueryClientProvider>
143 )
144}