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

app.component.ts

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