Open History to see the last commit
1import { Component } from '@angular/core'
2import { injectQuery } from '@tanstack/angular-query'
3import { CLIENT_PERSIST_QUERY_ROOT } from '../query-persist-scope'
4
5/**
6 * Mounted only in the browser (parent uses `afterNextRender`), so `queryFn` is not run during SSR.
7 * Pair with `dehydrateOptions.shouldDehydrateQuery` if you want only `client-persist` keys in storage.
8 */
9@Component({
10 selector: 'client-persist-demo',
11 template: `
12 <div class="panel">
13 <p class="badge">Client-only island</p>
14 <h3>Client-only persisted query</h3>
15 @if (demo.isPending()) {
16 <p>Loading client-only data…</p>
17 } @else if (demo.isError()) {
18 <p>Failed to load.</p>
19 } @else if (demo.data(); as data) {
20 <p class="mono">{{ data.createdAt }}</p>
21 <p class="note">
22 This value is stored under the <code>client-persist</code> query key.
23 Hard-reload the page: it should reappear from persistence while the
24 posts list above is rendered from SSR again.
25 </p>
26 }
27 </div>
28 `,
29 styles: `
30 .panel {
31 margin-top: 8px;
32 padding: 20px;
33 border-radius: 18px;
34 border: 1px dashed #c4b5a0;
35 background: #fffefb;
36 }
37
38 .badge {
39 margin: 0 0 8px;
40 font-size: 11px;
41 font-weight: 700;
42 letter-spacing: 0.1em;
43 text-transform: uppercase;
44 color: #7c5a10;
45 }
46
47 h3 {
48 margin: 0 0 12px;
49 font-size: 1.1rem;
50 }
51
52 .mono {
53 margin: 0 0 12px;
54 font-family: ui-monospace, monospace;
55 font-size: 0.85rem;
56 word-break: break-all;
57 }
58
59 .note {
60 margin: 0;
61 font-size: 0.9rem;
62 line-height: 1.5;
63 color: #4b5563;
64 }
65
66 code {
67 font-size: 0.88em;
68 padding: 0.1em 0.35em;
69 border-radius: 4px;
70 background: #f3f4f6;
71 }
72 `,
73})
74export class ClientPersistDemoComponent {
75 readonly demo = injectQuery(() => ({
76 queryKey: [CLIENT_PERSIST_QUERY_ROOT, 'timestamp-demo'],
77 queryFn: async () => {
78 await new Promise((r) => setTimeout(r, 100))
79 return { createdAt: new Date().toISOString() }
80 },
81 staleTime: Infinity,
82 }))
83}