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

app.component.ts

121 lines3.0 KB

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