-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
282 lines (272 loc) · 12.9 KB
/
index.html
File metadata and controls
282 lines (272 loc) · 12.9 KB
1
2
3
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
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
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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!-- Primary Meta Tags -->
<title>
React Query Demo – Interactive Cache, Stale Time & Garbage Collection
Visualizer
</title>
<meta
name="title"
content="React Query Demo – Interactive Cache, Stale Time & Garbage Collection Visualizer"
/>
<meta
name="description"
content="Explore React Query (TanStack Query) caching in action. This interactive demo visualizes stale time, cache time (gcTime), garbage collection, background refetching, and data synchronization — helping you master React Query's data-fetching lifecycle."
/>
<meta
name="keywords"
content="React Query, TanStack Query, React Query demo, React Query cache, React Query stale time, React Query gcTime, React Query garbage collection, useQuery, data fetching React, React Query tutorial, React Query example, React Query visualization, cache invalidation, background refetching, React server state, React data sync"
/>
<meta name="author" content="Hamed Panjeh" />
<meta name="robots" content="index, follow" />
<link rel="canonical" href="https://react-query.dev/" />
<!-- Open Graph / Facebook -->
<meta property="og:type" content="website" />
<meta property="og:url" content="https://react-query.dev/" />
<meta
property="og:title"
content="React Query Demo – Interactive Cache & Data-Fetching Visualizer"
/>
<meta
property="og:description"
content="See how React Query (TanStack Query) manages caching, stale time, gcTime, garbage collection, and background refetching in a live interactive demo."
/>
<meta property="og:image" content="https://react-query.dev/og-image.png" />
<meta property="og:site_name" content="React Query Demo" />
<meta property="og:locale" content="en_US" />
<!-- Twitter Card -->
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:url" content="https://react-query.dev/" />
<meta
name="twitter:title"
content="React Query Demo – Interactive Cache & Data-Fetching Visualizer"
/>
<meta
name="twitter:description"
content="See how React Query (TanStack Query) manages caching, stale time, gcTime, garbage collection, and background refetching in a live interactive demo."
/>
<meta name="twitter:image" content="https://react-query.dev/og-image.png" />
<!-- Favicon -->
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<link rel="apple-touch-icon" href="/vite.svg" />
<!-- Web App Manifest -->
<link rel="manifest" href="/manifest.json" />
<meta name="theme-color" content="#7c3aed" />
<!-- Structured Data: WebApplication -->
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "WebApplication",
"name": "React Query Demo",
"url": "https://react-query.dev/",
"description": "An interactive visualization that makes React Query's caching, stale time, garbage collection, observer tracking, query lifecycle, network activity, and data-sync behavior easy to grasp at a glance.",
"applicationCategory": "DeveloperApplication",
"operatingSystem": "Any",
"author": {
"@type": "Person",
"name": "Hamed Panjeh",
"url": "https://www.linkedin.com/in/hpwebdeveloper/"
},
"offers": {
"@type": "Offer",
"price": "0",
"priceCurrency": "USD"
},
"keywords": "React Query, TanStack Query, cache demo, data fetching, useQuery, stale time, gcTime, garbage collection, observer count, query lifecycle, network activity log, cache hit, background refetch"
}
</script>
<!-- Structured Data: FAQPage (helps win rich snippets) -->
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": [
{
"@type": "Question",
"name": "What is React Query stale time?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Stale time (staleTime) is the duration data is considered fresh after being fetched. During this window React Query won't refetch the data automatically. Once stale time expires, the data is marked stale and may be refetched on the next trigger (window focus, component mount, etc.)."
}
},
{
"@type": "Question",
"name": "What is React Query gcTime (cache time)?",
"acceptedAnswer": {
"@type": "Answer",
"text": "gcTime (formerly cacheTime) controls how long inactive (unused) query data stays in the cache before being garbage collected. Even after a component unmounts, the cached data remains available for this duration so remounting is instant."
}
},
{
"@type": "Question",
"name": "How does React Query garbage collection work?",
"acceptedAnswer": {
"@type": "Answer",
"text": "When a query has no active observers (no mounted components using it), React Query starts a garbage collection timer equal to gcTime. If the query isn't used again before the timer expires, the cached data is permanently removed from memory."
}
},
{
"@type": "Question",
"name": "What is the difference between isLoading and isFetching in React Query?",
"acceptedAnswer": {
"@type": "Answer",
"text": "isLoading is true only on the very first fetch when there is no cached data. isFetching is true whenever a network request is in flight, including background refetches. This means isFetching can be true while isLoading is false when stale data is being refreshed behind the scenes."
}
}
]
}
</script>
<!-- Structured Data: HowTo (step-by-step guide for rich snippets) -->
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "HowTo",
"name": "How to Use the React Query Cache Demo",
"description": "A 10-step interactive guide to understanding React Query's caching lifecycle — from first fetch through stale time, cache sharing, mount/unmount behavior, and garbage collection.",
"totalTime": "PT5M",
"tool": [
{
"@type": "HowToTool",
"name": "Modern web browser (Chrome, Firefox, Safari, or Edge)"
},
{ "@type": "HowToTool", "name": "Browser DevTools console (F12)" }
],
"step": [
{
"@type": "HowToStep",
"position": 1,
"name": "Mount Your First User",
"text": "Click the green Mount button on any user card (e.g. User 1). React Query fetches data from the server for the first time. The status shows Loading, then the user's name, email, status, and server timestamp appear. A 30-second stale-time countdown begins."
},
{
"@type": "HowToStep",
"position": 2,
"name": "Mount a Second User",
"text": "While User 1 is still mounted, click Mount on User 2. A new API call is made for User 2. Both users are mounted simultaneously with independent data, statuses, and countdowns."
},
{
"@type": "HowToStep",
"position": 3,
"name": "Unmount and Re-mount While Fresh",
"text": "Click Unmount on User 1, then immediately click Mount again within 30 seconds. Data appears instantly from cache with no network request — the console confirms no API call was made."
},
{
"@type": "HowToStep",
"position": 4,
"name": "Watch Data Become Stale",
"text": "Keep a user mounted and watch the blue freshness countdown reach 0. The label turns orange and reads STALE. The cached data is still displayed but is now considered outdated by React Query."
},
{
"@type": "HowToStep",
"position": 5,
"name": "Show the Child Component (Cache Sharing)",
"text": "Click the purple Show Child Component button inside any mounted user's card. A child component appears using the same cached data with the same queryKey — no duplicate network request is made. The observer count increases to 2."
},
{
"@type": "HowToStep",
"position": 6,
"name": "Hide the Child Component",
"text": "Click the button again to hide the child. The observer count drops back to 1 but the parent still observes the query, so no garbage-collection countdown starts."
},
{
"@type": "HowToStep",
"position": 7,
"name": "Unmount a User",
"text": "Click the Unmount button on a specific user's card. That user's data disappears and the 60-second garbage-collection countdown begins. Other mounted users are not affected."
},
{
"@type": "HowToStep",
"position": 8,
"name": "Re-mount Before GC Expires",
"text": "Click Mount on the same user before the GC countdown reaches 0. The data reappears instantly from cache with no loading spinner, demonstrating gcTime's grace period."
},
{
"@type": "HowToStep",
"position": 9,
"name": "Let Garbage Collection Remove the Cache",
"text": "Unmount a user and wait the full 60 seconds. The Garbage Collection Events panel logs a removal event, confirming the cache entry has been permanently deleted."
},
{
"@type": "HowToStep",
"position": 10,
"name": "Mount After Garbage Collection",
"text": "Click Mount on the user whose cache was collected. You see the Loading state again and a fresh API call — the full fetch-cache-stale-GC lifecycle is complete."
}
]
}
</script>
</head>
<body>
<div id="root"></div>
<!-- Noscript fallback for search-engine crawlers that don't execute JS -->
<noscript>
<h1>React Query Demo – Interactive Cache Visualizer</h1>
<p>
This interactive demo by Hamed Panjeh visualizes React Query (TanStack
Query) caching, stale time, gcTime, garbage collection, and background
refetching to help developers understand data-fetching lifecycle at a
glance.
</p>
<p>
Features: live cache countdown timers, mount/unmount simulation, user
data fetching, stale vs fresh indicators, and garbage collection
monitoring.
</p>
<h2>How to Use This React Query Demo</h2>
<ol>
<li>
<strong>Mount Your First User</strong> — Click the green "Mount"
button on any user's card. React Query makes a real API call and
starts a 30-second stale-time countdown.
</li>
<li>
<strong>Mount a Second User</strong> — Mount another user while the
first is still active. Both run independently with their own data and
countdowns.
</li>
<li>
<strong>Unmount and Re-mount While Fresh</strong> — Unmount a user,
then re-mount within 30 seconds. Data loads instantly from cache—no
network request.
</li>
<li>
<strong>Watch Data Become Stale</strong> — After 30 seconds the data
becomes stale (orange). React Query will refetch on the next trigger.
</li>
<li>
<strong>Show Child Component</strong> — Click "Show Child Component"
inside a mounted user's card to see cache sharing. Parent and child
use the same cached data—observer count increases.
</li>
<li>
<strong>Hide Child Component</strong> — Hiding the child reduces the
observer count but the cache remains active.
</li>
<li>
<strong>Unmount a User</strong> — Click "Unmount" on a user's card.
The 60-second garbage-collection countdown begins for that user only.
</li>
<li>
<strong>Re-mount Before GC</strong> — Mount the user again before 60
seconds. Cached data appears instantly.
</li>
<li>
<strong>Wait for Garbage Collection</strong> — Let the GC timer
expire. The cache is permanently removed.
</li>
<li>
<strong>Mount After GC</strong> — Mount the user again after GC. A
fresh API call is made—completing the full lifecycle.
</li>
</ol>
<p>
Visit <a href="https://react-query.dev/">react-query.dev</a> with
JavaScript enabled to explore the full interactive experience.
</p>
</noscript>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>