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

app.component.ts

125 lines3.1 KB

1import {
2 ChangeDetectionStrategy,
3 Component,
4 afterNextRender,
5 signal,
6} from '@angular/core'
7import { ClientPersistDemoComponent } from './components/client-persist-demo.component'
8import { PostsComponent } from './components/posts.component'
9​
10@Component({
11 changeDetection: ChangeDetectionStrategy.OnPush,
12 selector: 'ssr-persist-example',
13 imports: [ClientPersistDemoComponent, PostsComponent],
14 template: `
15 <main class="page">
16 <header class="hero">
17 <p class="eyebrow">Angular SSR + client-only island + persistence</p>
18 <h1>SSR queries vs client-only persisted queries</h1>
19 <p class="lede">
20 Posts use <code>queryKey: ['posts']</code> and run on the server. Configure the
21 persister with <code>dehydrateOptions.shouldDehydrateQuery</code> if you want only
22 <code>client-persist</code> keys in <code>localStorage</code>.
23 </p>
24 <p class="lede hint">
25 The panel below is mounted only in the browser via
26 <code>afterNextRender</code>, so its query never runs during SSR. After the first
27 visit, hard-reload: the timestamp can be restored from persistence while posts render
28 from SSR again.
29 </p>
30 </header>
31​
32 <section class="region">
33 <h2 class="region-title">Server data</h2>
34 <posts />
35 </section>
36​
37 <section class="region">
38 <h2 class="region-title">Client-only (afterNextRender)</h2>
39 @if (showClientDemo()) {
40 <client-persist-demo />
41 } @else {
42 <p class="placeholder">
43 Server render: this placeholder has no <code>client-persist-demo</code> component
44 yet β€” it is created after the first browser frame via <code>afterNextRender</code>.
45 </p>
46 }
47 </section>
48 </main>
49 `,
50 styles: `
51 .page {
52 max-width: 960px;
53 margin: 0 auto;
54 padding: 48px 20px 72px;
55 }
56​
57 .hero {
58 margin-bottom: 32px;
59 }
60​
61 .eyebrow {
62 margin: 0 0 8px;
63 font-size: 12px;
64 font-weight: 700;
65 letter-spacing: 0.12em;
66 text-transform: uppercase;
67 color: #9b5c00;
68 }
69​
70 h1 {
71 margin: 0 0 12px;
72 font-size: clamp(2.2rem, 5vw, 4rem);
73 line-height: 1;
74 }
75​
76 .lede {
77 max-width: 640px;
78 margin: 0 0 12px;
79 font-size: 1rem;
80 line-height: 1.6;
81 color: #4b5563;
82 }
83​
84 .hint {
85 font-size: 0.95rem;
86 color: #6b7280;
87 }
88​
89 code {
90 font-size: 0.88em;
91 padding: 0.1em 0.35em;
92 border-radius: 4px;
93 background: #f3f4f6;
94 }
95​
96 .region {
97 margin-bottom: 40px;
98 }
99​
100 .region-title {
101 margin: 0 0 16px;
102 font-size: 1.25rem;
103 color: #1f2937;
104 }
105​
106 .placeholder {
107 margin: 0;
108 padding: 16px;
109 border-radius: 12px;
110 background: #f9fafb;
111 color: #6b7280;
112 line-height: 1.5;
113 }
114 `,
115})
116export class SsrPersistExampleComponent {
117 /** When `true`, `ClientPersistDemoComponent` exists only in the browser (not during SSR). */
118 readonly showClientDemo = signal(false)
119​
120 constructor() {
121 afterNextRender(() => {
122 this.showClientDemo.set(true)
123 })
124 }