create-huenei-template 0.2.3 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-component/SKILL.md +288 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-component/references/component-patterns.md +358 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-di/SKILL.md +387 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-di/references/di-patterns.md +519 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-directives/SKILL.md +437 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-directives/references/directive-patterns.md +570 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-forms/SKILL.md +434 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-forms/references/form-patterns.md +398 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-http/SKILL.md +366 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-http/references/http-patterns.md +448 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-routing/SKILL.md +395 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-routing/references/routing-patterns.md +472 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-signals/SKILL.md +302 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-signals/references/signal-patterns.md +404 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-ssr/SKILL.md +435 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-ssr/references/ssr-patterns.md +497 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-testing/SKILL.md +660 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-testing/references/testing-patterns.md +707 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-tooling/SKILL.md +352 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-tooling/references/tooling-patterns.md +448 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-accessibility/SKILL.md +780 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-components/SKILL.md +718 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-design-systems/SKILL.md +612 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-layouts/SKILL.md +594 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-animations/SKILL.md +432 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-animations/references/animation-patterns.md +427 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-debugging/SKILL.md +354 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-debugging/references/troubleshooting-guide.md +306 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-framework-integration/SKILL.md +578 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-framework-integration/references/react-patterns.md +353 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/SKILL.md +608 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/references/advanced-theme-patterns.md +215 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/references/custom-utilities-guide.md +281 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-mobile-first/SKILL.md +768 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-performance/SKILL.md +374 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-performance/references/bundle-optimization.md +244 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-plugins/SKILL.md +505 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-responsive-darkmode/SKILL.md +601 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-responsive-darkmode/references/advanced-responsive-patterns.md +485 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/web-design-guidelines/SKILL.md +39 -0
- package/dist/templates/frontend/angular-base/package.json +0 -1
- package/dist/templates/frontend/angular-base/pnpm-lock.yaml +8183 -0
- package/package.json +1 -1
- package/templates/frontend/angular-base/.cursor/skills/angular-component/SKILL.md +288 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-component/references/component-patterns.md +358 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-di/SKILL.md +387 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-di/references/di-patterns.md +519 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-directives/SKILL.md +437 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-directives/references/directive-patterns.md +570 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-forms/SKILL.md +434 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-forms/references/form-patterns.md +398 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-http/SKILL.md +366 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-http/references/http-patterns.md +448 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-routing/SKILL.md +395 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-routing/references/routing-patterns.md +472 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-signals/SKILL.md +302 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-signals/references/signal-patterns.md +404 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-ssr/SKILL.md +435 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-ssr/references/ssr-patterns.md +497 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-testing/SKILL.md +660 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-testing/references/testing-patterns.md +707 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-tooling/SKILL.md +352 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-tooling/references/tooling-patterns.md +448 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-accessibility/SKILL.md +780 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-components/SKILL.md +718 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-design-systems/SKILL.md +612 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-layouts/SKILL.md +594 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-animations/SKILL.md +432 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-animations/references/animation-patterns.md +427 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-debugging/SKILL.md +354 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-debugging/references/troubleshooting-guide.md +306 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-framework-integration/SKILL.md +578 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-framework-integration/references/react-patterns.md +353 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/SKILL.md +608 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/references/advanced-theme-patterns.md +215 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/references/custom-utilities-guide.md +281 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-mobile-first/SKILL.md +768 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-performance/SKILL.md +374 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-performance/references/bundle-optimization.md +244 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-plugins/SKILL.md +505 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-responsive-darkmode/SKILL.md +601 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-responsive-darkmode/references/advanced-responsive-patterns.md +485 -0
- package/templates/frontend/angular-base/.cursor/skills/web-design-guidelines/SKILL.md +39 -0
- package/templates/frontend/angular-base/package.json +0 -1
- package/templates/frontend/angular-base/pnpm-lock.yaml +8183 -0
- package/templates/frontend/angular-base/package-lock.json +0 -13321
|
@@ -0,0 +1,435 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: angular-ssr
|
|
3
|
+
description: Implement server-side rendering and hydration in Angular v20+ using @angular/ssr. Use for SSR setup, hydration strategies, prerendering static pages, and handling browser-only APIs. Triggers on SSR configuration, fixing hydration mismatches, prerendering routes, or making code SSR-compatible.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Angular SSR
|
|
7
|
+
|
|
8
|
+
Implement server-side rendering, hydration, and prerendering in Angular v20+.
|
|
9
|
+
|
|
10
|
+
## Setup
|
|
11
|
+
|
|
12
|
+
### Add SSR to Existing Project
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
ng add @angular/ssr
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
This adds:
|
|
19
|
+
- `@angular/ssr` package
|
|
20
|
+
- `server.ts` - Express server
|
|
21
|
+
- `src/main.server.ts` - Server bootstrap
|
|
22
|
+
- `src/app/app.config.server.ts` - Server providers
|
|
23
|
+
- Updates `angular.json` with SSR configuration
|
|
24
|
+
|
|
25
|
+
### Project Structure
|
|
26
|
+
|
|
27
|
+
```
|
|
28
|
+
src/
|
|
29
|
+
├── app/
|
|
30
|
+
│ ├── app.config.ts # Browser config
|
|
31
|
+
│ ├── app.config.server.ts # Server config
|
|
32
|
+
│ └── app.routes.ts
|
|
33
|
+
├── main.ts # Browser bootstrap
|
|
34
|
+
├── main.server.ts # Server bootstrap
|
|
35
|
+
server.ts # Express server
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Configuration
|
|
39
|
+
|
|
40
|
+
### app.config.server.ts
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
import { ApplicationConfig, mergeApplicationConfig } from '@angular/core';
|
|
44
|
+
import { provideServerRendering } from '@angular/platform-server';
|
|
45
|
+
import { provideServerRoutesConfig } from '@angular/ssr';
|
|
46
|
+
import { appConfig } from './app.config';
|
|
47
|
+
import { serverRoutes } from './app.routes.server';
|
|
48
|
+
|
|
49
|
+
const serverConfig: ApplicationConfig = {
|
|
50
|
+
providers: [
|
|
51
|
+
provideServerRendering(),
|
|
52
|
+
provideServerRoutesConfig(serverRoutes),
|
|
53
|
+
],
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export const config = mergeApplicationConfig(appConfig, serverConfig);
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Server Routes Configuration
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
// app.routes.server.ts
|
|
63
|
+
import { RenderMode, ServerRoute } from '@angular/ssr';
|
|
64
|
+
|
|
65
|
+
export const serverRoutes: ServerRoute[] = [
|
|
66
|
+
{
|
|
67
|
+
path: '',
|
|
68
|
+
renderMode: RenderMode.Prerender, // Static at build time
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
path: 'products',
|
|
72
|
+
renderMode: RenderMode.Prerender,
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
path: 'products/:id',
|
|
76
|
+
renderMode: RenderMode.Server, // Dynamic SSR
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
path: 'dashboard',
|
|
80
|
+
renderMode: RenderMode.Client, // Client-only (SPA)
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
path: '**',
|
|
84
|
+
renderMode: RenderMode.Server,
|
|
85
|
+
},
|
|
86
|
+
];
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Render Modes
|
|
90
|
+
|
|
91
|
+
| Mode | Description | Use Case |
|
|
92
|
+
|------|-------------|----------|
|
|
93
|
+
| `RenderMode.Prerender` | Static HTML at build time | Marketing pages, blogs |
|
|
94
|
+
| `RenderMode.Server` | Dynamic SSR per request | User-specific content |
|
|
95
|
+
| `RenderMode.Client` | Client-side only (SPA) | Authenticated dashboards |
|
|
96
|
+
|
|
97
|
+
## Hydration
|
|
98
|
+
|
|
99
|
+
### Default Hydration
|
|
100
|
+
|
|
101
|
+
Hydration is enabled by default with `provideClientHydration()`:
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
// app.config.ts
|
|
105
|
+
import { provideClientHydration } from '@angular/platform-browser';
|
|
106
|
+
|
|
107
|
+
export const appConfig: ApplicationConfig = {
|
|
108
|
+
providers: [
|
|
109
|
+
provideClientHydration(),
|
|
110
|
+
// ...
|
|
111
|
+
],
|
|
112
|
+
};
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Incremental Hydration
|
|
116
|
+
|
|
117
|
+
Defer hydration of specific components:
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
@Component({
|
|
121
|
+
template: `
|
|
122
|
+
<!-- Hydrate when visible -->
|
|
123
|
+
@defer (hydrate on viewport) {
|
|
124
|
+
<app-comments [postId]="postId" />
|
|
125
|
+
} @placeholder {
|
|
126
|
+
<div class="comments-placeholder">Loading comments...</div>
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
<!-- Hydrate on interaction -->
|
|
130
|
+
@defer (hydrate on interaction) {
|
|
131
|
+
<app-interactive-chart [data]="chartData" />
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
<!-- Hydrate on idle -->
|
|
135
|
+
@defer (hydrate on idle) {
|
|
136
|
+
<app-recommendations />
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
<!-- Never hydrate (static only) -->
|
|
140
|
+
@defer (hydrate never) {
|
|
141
|
+
<app-static-footer />
|
|
142
|
+
}
|
|
143
|
+
`,
|
|
144
|
+
})
|
|
145
|
+
export class Post {
|
|
146
|
+
postId = input.required<string>();
|
|
147
|
+
chartData = input.required<ChartData>();
|
|
148
|
+
}
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### Hydration Triggers
|
|
152
|
+
|
|
153
|
+
| Trigger | Description |
|
|
154
|
+
|---------|-------------|
|
|
155
|
+
| `hydrate on viewport` | When element enters viewport |
|
|
156
|
+
| `hydrate on interaction` | On click, focus, or input |
|
|
157
|
+
| `hydrate on idle` | When browser is idle |
|
|
158
|
+
| `hydrate on immediate` | Immediately after load |
|
|
159
|
+
| `hydrate on timer(ms)` | After specified delay |
|
|
160
|
+
| `hydrate when condition` | When expression is true |
|
|
161
|
+
| `hydrate never` | Never hydrate (static) |
|
|
162
|
+
|
|
163
|
+
### Event Replay
|
|
164
|
+
|
|
165
|
+
Capture user events before hydration completes:
|
|
166
|
+
|
|
167
|
+
```typescript
|
|
168
|
+
import { provideClientHydration, withEventReplay } from '@angular/platform-browser';
|
|
169
|
+
|
|
170
|
+
export const appConfig: ApplicationConfig = {
|
|
171
|
+
providers: [
|
|
172
|
+
provideClientHydration(withEventReplay()),
|
|
173
|
+
],
|
|
174
|
+
};
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
## Browser-Only Code
|
|
178
|
+
|
|
179
|
+
### Platform Detection
|
|
180
|
+
|
|
181
|
+
```typescript
|
|
182
|
+
import { PLATFORM_ID, inject } from '@angular/core';
|
|
183
|
+
import { isPlatformBrowser, isPlatformServer } from '@angular/common';
|
|
184
|
+
|
|
185
|
+
@Component({...})
|
|
186
|
+
export class My {
|
|
187
|
+
private platformId = inject(PLATFORM_ID);
|
|
188
|
+
|
|
189
|
+
ngOnInit() {
|
|
190
|
+
if (isPlatformBrowser(this.platformId)) {
|
|
191
|
+
// Browser-only code
|
|
192
|
+
window.addEventListener('scroll', this.onScroll);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### afterNextRender / afterRender
|
|
199
|
+
|
|
200
|
+
Run code only in browser after rendering:
|
|
201
|
+
|
|
202
|
+
```typescript
|
|
203
|
+
import { afterNextRender, afterRender } from '@angular/core';
|
|
204
|
+
|
|
205
|
+
@Component({...})
|
|
206
|
+
export class Chart {
|
|
207
|
+
constructor() {
|
|
208
|
+
// Runs once after first render (browser only)
|
|
209
|
+
afterNextRender(() => {
|
|
210
|
+
this.initChart();
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
// Runs after every render (browser only)
|
|
214
|
+
afterRender(() => {
|
|
215
|
+
this.updateChart();
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
private initChart() {
|
|
220
|
+
// Safe to use DOM APIs here
|
|
221
|
+
const canvas = document.getElementById('chart');
|
|
222
|
+
new Chart(canvas, this.config);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
### Inject Browser APIs Safely
|
|
228
|
+
|
|
229
|
+
```typescript
|
|
230
|
+
// tokens.ts
|
|
231
|
+
import { InjectionToken, PLATFORM_ID, inject } from '@angular/core';
|
|
232
|
+
import { isPlatformBrowser } from '@angular/common';
|
|
233
|
+
|
|
234
|
+
export const WINDOW = new InjectionToken<Window | null>('Window', {
|
|
235
|
+
providedIn: 'root',
|
|
236
|
+
factory: () => {
|
|
237
|
+
const platformId = inject(PLATFORM_ID);
|
|
238
|
+
return isPlatformBrowser(platformId) ? window : null;
|
|
239
|
+
},
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
export const LOCAL_STORAGE = new InjectionToken<Storage | null>('LocalStorage', {
|
|
243
|
+
providedIn: 'root',
|
|
244
|
+
factory: () => {
|
|
245
|
+
const platformId = inject(PLATFORM_ID);
|
|
246
|
+
return isPlatformBrowser(platformId) ? localStorage : null;
|
|
247
|
+
},
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
// Usage
|
|
251
|
+
@Injectable({ providedIn: 'root' })
|
|
252
|
+
export class Storage {
|
|
253
|
+
private storage = inject(LOCAL_STORAGE);
|
|
254
|
+
|
|
255
|
+
get(key: string): string | null {
|
|
256
|
+
return this.storage?.getItem(key) ?? null;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
set(key: string, value: string): void {
|
|
260
|
+
this.storage?.setItem(key, value);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
## Prerendering
|
|
266
|
+
|
|
267
|
+
### Static Routes
|
|
268
|
+
|
|
269
|
+
```typescript
|
|
270
|
+
// app.routes.server.ts
|
|
271
|
+
export const serverRoutes: ServerRoute[] = [
|
|
272
|
+
{ path: '', renderMode: RenderMode.Prerender },
|
|
273
|
+
{ path: 'about', renderMode: RenderMode.Prerender },
|
|
274
|
+
{ path: 'contact', renderMode: RenderMode.Prerender },
|
|
275
|
+
{ path: 'blog', renderMode: RenderMode.Prerender },
|
|
276
|
+
];
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
### Dynamic Routes with getPrerenderParams
|
|
280
|
+
|
|
281
|
+
```typescript
|
|
282
|
+
// app.routes.server.ts
|
|
283
|
+
import { RenderMode, ServerRoute, PrerenderFallback } from '@angular/ssr';
|
|
284
|
+
|
|
285
|
+
export const serverRoutes: ServerRoute[] = [
|
|
286
|
+
{
|
|
287
|
+
path: 'products/:id',
|
|
288
|
+
renderMode: RenderMode.Prerender,
|
|
289
|
+
async getPrerenderParams() {
|
|
290
|
+
// Fetch product IDs to prerender
|
|
291
|
+
const response = await fetch('https://api.example.com/products');
|
|
292
|
+
const products = await response.json();
|
|
293
|
+
return products.map((p: Product) => ({ id: p.id }));
|
|
294
|
+
},
|
|
295
|
+
fallback: PrerenderFallback.Server, // SSR for non-prerendered
|
|
296
|
+
},
|
|
297
|
+
{
|
|
298
|
+
path: 'blog/:slug',
|
|
299
|
+
renderMode: RenderMode.Prerender,
|
|
300
|
+
async getPrerenderParams() {
|
|
301
|
+
const posts = await fetchBlogPosts();
|
|
302
|
+
return posts.map(post => ({ slug: post.slug }));
|
|
303
|
+
},
|
|
304
|
+
fallback: PrerenderFallback.Client, // SPA for non-prerendered
|
|
305
|
+
},
|
|
306
|
+
];
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
### Prerender Fallback Options
|
|
310
|
+
|
|
311
|
+
| Fallback | Description |
|
|
312
|
+
|----------|-------------|
|
|
313
|
+
| `PrerenderFallback.Server` | SSR for non-prerendered routes |
|
|
314
|
+
| `PrerenderFallback.Client` | Client-side rendering |
|
|
315
|
+
| `PrerenderFallback.None` | 404 for non-prerendered routes |
|
|
316
|
+
|
|
317
|
+
## HTTP Caching
|
|
318
|
+
|
|
319
|
+
### TransferState
|
|
320
|
+
|
|
321
|
+
Automatically transfer HTTP responses from server to client:
|
|
322
|
+
|
|
323
|
+
```typescript
|
|
324
|
+
import { provideClientHydration, withHttpTransferCacheOptions } from '@angular/platform-browser';
|
|
325
|
+
|
|
326
|
+
export const appConfig: ApplicationConfig = {
|
|
327
|
+
providers: [
|
|
328
|
+
provideClientHydration(
|
|
329
|
+
withHttpTransferCacheOptions({
|
|
330
|
+
includePostRequests: true,
|
|
331
|
+
includeRequestsWithAuthHeaders: false,
|
|
332
|
+
filter: (req) => !req.url.includes('/api/realtime'),
|
|
333
|
+
})
|
|
334
|
+
),
|
|
335
|
+
],
|
|
336
|
+
};
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
### Manual TransferState
|
|
340
|
+
|
|
341
|
+
```typescript
|
|
342
|
+
import { TransferState, makeStateKey } from '@angular/core';
|
|
343
|
+
|
|
344
|
+
const PRODUCTS_KEY = makeStateKey<Product[]>('products');
|
|
345
|
+
|
|
346
|
+
@Injectable({ providedIn: 'root' })
|
|
347
|
+
export class Product {
|
|
348
|
+
private http = inject(HttpClient);
|
|
349
|
+
private transferState = inject(TransferState);
|
|
350
|
+
private platformId = inject(PLATFORM_ID);
|
|
351
|
+
|
|
352
|
+
getProducts(): Observable<Product[]> {
|
|
353
|
+
// Check if data was transferred from server
|
|
354
|
+
if (this.transferState.hasKey(PRODUCTS_KEY)) {
|
|
355
|
+
const products = this.transferState.get(PRODUCTS_KEY, []);
|
|
356
|
+
this.transferState.remove(PRODUCTS_KEY);
|
|
357
|
+
return of(products);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
return this.http.get<Product[]>('/api/products').pipe(
|
|
361
|
+
tap(products => {
|
|
362
|
+
// Store for transfer on server
|
|
363
|
+
if (isPlatformServer(this.platformId)) {
|
|
364
|
+
this.transferState.set(PRODUCTS_KEY, products);
|
|
365
|
+
}
|
|
366
|
+
})
|
|
367
|
+
);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
## Build and Deploy
|
|
373
|
+
|
|
374
|
+
### Build Commands
|
|
375
|
+
|
|
376
|
+
```bash
|
|
377
|
+
# Build with SSR
|
|
378
|
+
ng build
|
|
379
|
+
|
|
380
|
+
# Output structure
|
|
381
|
+
dist/
|
|
382
|
+
├── my-app/
|
|
383
|
+
│ ├── browser/ # Client assets
|
|
384
|
+
│ └── server/ # Server bundle
|
|
385
|
+
```
|
|
386
|
+
|
|
387
|
+
### Run SSR Server
|
|
388
|
+
|
|
389
|
+
```bash
|
|
390
|
+
# Development
|
|
391
|
+
npm run serve:ssr:my-app
|
|
392
|
+
|
|
393
|
+
# Production
|
|
394
|
+
node dist/my-app/server/server.mjs
|
|
395
|
+
```
|
|
396
|
+
|
|
397
|
+
### Deploy to Node.js Host
|
|
398
|
+
|
|
399
|
+
```javascript
|
|
400
|
+
// server.ts (generated)
|
|
401
|
+
import { APP_BASE_HREF } from '@angular/common';
|
|
402
|
+
import { CommonEngine } from '@angular/ssr/node';
|
|
403
|
+
import express from 'express';
|
|
404
|
+
import { dirname, join, resolve } from 'node:path';
|
|
405
|
+
import { fileURLToPath } from 'node:url';
|
|
406
|
+
import bootstrap from './src/main.server';
|
|
407
|
+
|
|
408
|
+
const serverDistFolder = dirname(fileURLToPath(import.meta.url));
|
|
409
|
+
const browserDistFolder = resolve(serverDistFolder, '../browser');
|
|
410
|
+
const indexHtml = join(serverDistFolder, 'index.server.html');
|
|
411
|
+
|
|
412
|
+
const app = express();
|
|
413
|
+
const commonEngine = new CommonEngine();
|
|
414
|
+
|
|
415
|
+
app.get('*', express.static(browserDistFolder, { maxAge: '1y', index: false }));
|
|
416
|
+
|
|
417
|
+
app.get('*', (req, res, next) => {
|
|
418
|
+
commonEngine
|
|
419
|
+
.render({
|
|
420
|
+
bootstrap,
|
|
421
|
+
documentFilePath: indexHtml,
|
|
422
|
+
url: req.originalUrl,
|
|
423
|
+
publicPath: browserDistFolder,
|
|
424
|
+
providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }],
|
|
425
|
+
})
|
|
426
|
+
.then((html) => res.send(html))
|
|
427
|
+
.catch((err) => next(err));
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
app.listen(4000, () => {
|
|
431
|
+
console.log('Server listening on http://localhost:4000');
|
|
432
|
+
});
|
|
433
|
+
```
|
|
434
|
+
|
|
435
|
+
For advanced patterns, see [references/ssr-patterns.md](references/ssr-patterns.md).
|