create-huenei-template 0.2.2 → 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/config/templates.js +3 -3
- 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
package/dist/templates/frontend/angular-base/.cursor/skills/angular-ssr/references/ssr-patterns.md
ADDED
|
@@ -0,0 +1,497 @@
|
|
|
1
|
+
# Angular SSR Patterns
|
|
2
|
+
|
|
3
|
+
## Table of Contents
|
|
4
|
+
- [Hydration Debugging](#hydration-debugging)
|
|
5
|
+
- [SEO Optimization](#seo-optimization)
|
|
6
|
+
- [Authentication with SSR](#authentication-with-ssr)
|
|
7
|
+
- [Caching Strategies](#caching-strategies)
|
|
8
|
+
- [Error Handling](#error-handling)
|
|
9
|
+
- [Performance Optimization](#performance-optimization)
|
|
10
|
+
|
|
11
|
+
## Hydration Debugging
|
|
12
|
+
|
|
13
|
+
### Common Hydration Mismatches
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
// Problem: Different content on server vs client
|
|
17
|
+
@Component({
|
|
18
|
+
template: `<p>Current time: {{ currentTime }}</p>`,
|
|
19
|
+
})
|
|
20
|
+
export class Time {
|
|
21
|
+
// BAD: Different value on server and client
|
|
22
|
+
currentTime = new Date().toLocaleTimeString();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Solution: Use afterNextRender or skip SSR
|
|
26
|
+
@Component({
|
|
27
|
+
template: `<p>Current time: {{ currentTime() }}</p>`,
|
|
28
|
+
})
|
|
29
|
+
export class Time {
|
|
30
|
+
currentTime = signal('');
|
|
31
|
+
|
|
32
|
+
constructor() {
|
|
33
|
+
afterNextRender(() => {
|
|
34
|
+
this.currentTime.set(new Date().toLocaleTimeString());
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Skip Hydration for Dynamic Content
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
@Component({
|
|
44
|
+
template: `
|
|
45
|
+
<!-- Skip hydration for this subtree -->
|
|
46
|
+
<div ngSkipHydration>
|
|
47
|
+
<app-dynamic-widget />
|
|
48
|
+
</div>
|
|
49
|
+
`,
|
|
50
|
+
})
|
|
51
|
+
export class Page {}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Debug Hydration Issues
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
// Enable hydration debugging in development
|
|
58
|
+
import { provideClientHydration, withNoDomReuse } from '@angular/platform-browser';
|
|
59
|
+
|
|
60
|
+
export const appConfig: ApplicationConfig = {
|
|
61
|
+
providers: [
|
|
62
|
+
provideClientHydration(
|
|
63
|
+
// Disable DOM reuse to see hydration errors clearly
|
|
64
|
+
...(isDevMode() ? [withNoDomReuse()] : [])
|
|
65
|
+
),
|
|
66
|
+
],
|
|
67
|
+
};
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## SEO Optimization
|
|
71
|
+
|
|
72
|
+
### Meta Tags Service
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
import { Injectable, inject } from '@angular/core';
|
|
76
|
+
import { Meta, Title } from '@angular/platform-browser';
|
|
77
|
+
import { DOCUMENT } from '@angular/common';
|
|
78
|
+
|
|
79
|
+
@Injectable({ providedIn: 'root' })
|
|
80
|
+
export class Seo {
|
|
81
|
+
private meta = inject(Meta);
|
|
82
|
+
private title = inject(Title);
|
|
83
|
+
private document = inject(DOCUMENT);
|
|
84
|
+
|
|
85
|
+
updateMetaTags(config: {
|
|
86
|
+
title: string;
|
|
87
|
+
description: string;
|
|
88
|
+
image?: string;
|
|
89
|
+
url?: string;
|
|
90
|
+
type?: string;
|
|
91
|
+
}) {
|
|
92
|
+
// Basic meta
|
|
93
|
+
this.title.setTitle(config.title);
|
|
94
|
+
this.meta.updateTag({ name: 'description', content: config.description });
|
|
95
|
+
|
|
96
|
+
// Open Graph
|
|
97
|
+
this.meta.updateTag({ property: 'og:title', content: config.title });
|
|
98
|
+
this.meta.updateTag({ property: 'og:description', content: config.description });
|
|
99
|
+
this.meta.updateTag({ property: 'og:type', content: config.type || 'website' });
|
|
100
|
+
|
|
101
|
+
if (config.image) {
|
|
102
|
+
this.meta.updateTag({ property: 'og:image', content: config.image });
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (config.url) {
|
|
106
|
+
this.meta.updateTag({ property: 'og:url', content: config.url });
|
|
107
|
+
this.updateCanonicalUrl(config.url);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Twitter Card
|
|
111
|
+
this.meta.updateTag({ name: 'twitter:card', content: 'summary_large_image' });
|
|
112
|
+
this.meta.updateTag({ name: 'twitter:title', content: config.title });
|
|
113
|
+
this.meta.updateTag({ name: 'twitter:description', content: config.description });
|
|
114
|
+
|
|
115
|
+
if (config.image) {
|
|
116
|
+
this.meta.updateTag({ name: 'twitter:image', content: config.image });
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
private updateCanonicalUrl(url: string) {
|
|
121
|
+
let link: HTMLLinkElement | null = this.document.querySelector('link[rel="canonical"]');
|
|
122
|
+
|
|
123
|
+
if (!link) {
|
|
124
|
+
link = this.document.createElement('link');
|
|
125
|
+
link.setAttribute('rel', 'canonical');
|
|
126
|
+
this.document.head.appendChild(link);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
link.setAttribute('href', url);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
setJsonLd(data: object) {
|
|
133
|
+
let script: HTMLScriptElement | null = this.document.querySelector('script[type="application/ld+json"]');
|
|
134
|
+
|
|
135
|
+
if (!script) {
|
|
136
|
+
script = this.document.createElement('script');
|
|
137
|
+
script.type = 'application/ld+json';
|
|
138
|
+
this.document.head.appendChild(script);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
script.textContent = JSON.stringify(data);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// Usage in component
|
|
146
|
+
@Component({...})
|
|
147
|
+
export class Product {
|
|
148
|
+
private seo = inject(Seo);
|
|
149
|
+
product = input.required<Product>();
|
|
150
|
+
|
|
151
|
+
constructor() {
|
|
152
|
+
effect(() => {
|
|
153
|
+
const product = this.product();
|
|
154
|
+
this.seo.updateMetaTags({
|
|
155
|
+
title: `${product.name} | My Store`,
|
|
156
|
+
description: product.description,
|
|
157
|
+
image: product.imageUrl,
|
|
158
|
+
url: `https://mystore.com/products/${product.id}`,
|
|
159
|
+
type: 'product',
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
this.seo.setJsonLd({
|
|
163
|
+
'@context': 'https://schema.org',
|
|
164
|
+
'@type': 'Product',
|
|
165
|
+
name: product.name,
|
|
166
|
+
description: product.description,
|
|
167
|
+
image: product.imageUrl,
|
|
168
|
+
offers: {
|
|
169
|
+
'@type': 'Offer',
|
|
170
|
+
price: product.price,
|
|
171
|
+
priceCurrency: 'USD',
|
|
172
|
+
},
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### Route-Based SEO with Resolvers
|
|
180
|
+
|
|
181
|
+
```typescript
|
|
182
|
+
// seo.resolver.ts
|
|
183
|
+
export const seoResolver: ResolveFn<SeoData> = async (route) => {
|
|
184
|
+
const productId = route.paramMap.get('id')!;
|
|
185
|
+
const productService = inject(Product);
|
|
186
|
+
const product = await productService.getById(productId);
|
|
187
|
+
|
|
188
|
+
return {
|
|
189
|
+
title: `${product.name} | My Store`,
|
|
190
|
+
description: product.description,
|
|
191
|
+
image: product.imageUrl,
|
|
192
|
+
};
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
// Routes
|
|
196
|
+
{
|
|
197
|
+
path: 'products/:id',
|
|
198
|
+
component: Product,
|
|
199
|
+
resolve: { seo: seoResolver },
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// Component
|
|
203
|
+
@Component({...})
|
|
204
|
+
export class Product {
|
|
205
|
+
private seo = inject(Seo);
|
|
206
|
+
seoData = input.required<SeoData>(); // From resolver
|
|
207
|
+
|
|
208
|
+
constructor() {
|
|
209
|
+
effect(() => {
|
|
210
|
+
this.seo.updateMetaTags(this.seoData());
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
## Authentication with SSR
|
|
217
|
+
|
|
218
|
+
### Cookie-Based Auth
|
|
219
|
+
|
|
220
|
+
```typescript
|
|
221
|
+
// Server-side cookie reading
|
|
222
|
+
import { REQUEST } from '@angular/ssr/tokens';
|
|
223
|
+
|
|
224
|
+
@Injectable({ providedIn: 'root' })
|
|
225
|
+
export class Auth {
|
|
226
|
+
private request = inject(REQUEST, { optional: true });
|
|
227
|
+
private platformId = inject(PLATFORM_ID);
|
|
228
|
+
|
|
229
|
+
getToken(): string | null {
|
|
230
|
+
if (isPlatformServer(this.platformId) && this.request) {
|
|
231
|
+
// Read from request cookies on server
|
|
232
|
+
const cookies = this.request.headers.cookie || '';
|
|
233
|
+
const match = cookies.match(/auth_token=([^;]+)/);
|
|
234
|
+
return match ? match[1] : null;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
if (isPlatformBrowser(this.platformId)) {
|
|
238
|
+
// Read from document cookies on client
|
|
239
|
+
const match = document.cookie.match(/auth_token=([^;]+)/);
|
|
240
|
+
return match ? match[1] : null;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
return null;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
### Skip SSR for Authenticated Routes
|
|
249
|
+
|
|
250
|
+
```typescript
|
|
251
|
+
// app.routes.server.ts
|
|
252
|
+
export const serverRoutes: ServerRoute[] = [
|
|
253
|
+
// Public routes - prerender
|
|
254
|
+
{ path: '', renderMode: RenderMode.Prerender },
|
|
255
|
+
{ path: 'products', renderMode: RenderMode.Prerender },
|
|
256
|
+
|
|
257
|
+
// Authenticated routes - client only
|
|
258
|
+
{ path: 'dashboard', renderMode: RenderMode.Client },
|
|
259
|
+
{ path: 'profile', renderMode: RenderMode.Client },
|
|
260
|
+
{ path: 'settings', renderMode: RenderMode.Client },
|
|
261
|
+
];
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
## Caching Strategies
|
|
265
|
+
|
|
266
|
+
### HTTP Cache Headers
|
|
267
|
+
|
|
268
|
+
```typescript
|
|
269
|
+
// server.ts
|
|
270
|
+
import { REQUEST, RESPONSE_INIT } from '@angular/ssr/tokens';
|
|
271
|
+
|
|
272
|
+
// In route configuration or component
|
|
273
|
+
@Component({...})
|
|
274
|
+
export class ProductList {
|
|
275
|
+
private responseInit = inject(RESPONSE_INIT, { optional: true });
|
|
276
|
+
|
|
277
|
+
constructor() {
|
|
278
|
+
// Set cache headers for SSR response
|
|
279
|
+
if (this.responseInit) {
|
|
280
|
+
this.responseInit.headers = {
|
|
281
|
+
...this.responseInit.headers,
|
|
282
|
+
'Cache-Control': 'public, max-age=3600, s-maxage=86400',
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
### CDN Caching with Vary Headers
|
|
290
|
+
|
|
291
|
+
```typescript
|
|
292
|
+
// server.ts - Express middleware
|
|
293
|
+
app.use((req, res, next) => {
|
|
294
|
+
// Vary by cookie for authenticated content
|
|
295
|
+
res.setHeader('Vary', 'Cookie');
|
|
296
|
+
next();
|
|
297
|
+
});
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
### Stale-While-Revalidate
|
|
301
|
+
|
|
302
|
+
```typescript
|
|
303
|
+
// Set SWR headers for dynamic content
|
|
304
|
+
this.responseInit.headers = {
|
|
305
|
+
'Cache-Control': 'public, max-age=60, stale-while-revalidate=3600',
|
|
306
|
+
};
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
## Error Handling
|
|
310
|
+
|
|
311
|
+
### SSR Error Boundaries
|
|
312
|
+
|
|
313
|
+
```typescript
|
|
314
|
+
// error-handler.ts
|
|
315
|
+
import { ErrorHandler, Injectable, inject } from '@angular/core';
|
|
316
|
+
import { PLATFORM_ID } from '@angular/core';
|
|
317
|
+
import { isPlatformServer } from '@angular/common';
|
|
318
|
+
|
|
319
|
+
@Injectable()
|
|
320
|
+
export class SsrError implements ErrorHandler {
|
|
321
|
+
private platformId = inject(PLATFORM_ID);
|
|
322
|
+
|
|
323
|
+
handleError(error: Error) {
|
|
324
|
+
if (isPlatformServer(this.platformId)) {
|
|
325
|
+
// Log server errors
|
|
326
|
+
console.error('SSR Error:', error);
|
|
327
|
+
// Could send to monitoring service
|
|
328
|
+
} else {
|
|
329
|
+
// Client-side error handling
|
|
330
|
+
console.error('Client Error:', error);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// Provide in app.config.ts
|
|
336
|
+
{ provide: ErrorHandler, useClass: SsrError }
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
### Graceful Degradation
|
|
340
|
+
|
|
341
|
+
```typescript
|
|
342
|
+
@Component({
|
|
343
|
+
template: `
|
|
344
|
+
@if (dataError()) {
|
|
345
|
+
<!-- Fallback content that works without data -->
|
|
346
|
+
<app-fallback-content />
|
|
347
|
+
} @else {
|
|
348
|
+
<app-data-content [data]="data()" />
|
|
349
|
+
}
|
|
350
|
+
`,
|
|
351
|
+
})
|
|
352
|
+
export class PageCmpt {
|
|
353
|
+
private dataService = inject(Data);
|
|
354
|
+
|
|
355
|
+
data = signal<Data | null>(null);
|
|
356
|
+
dataError = signal(false);
|
|
357
|
+
|
|
358
|
+
constructor() {
|
|
359
|
+
this.loadData();
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
private async loadData() {
|
|
363
|
+
try {
|
|
364
|
+
const data = await this.dataService.getData();
|
|
365
|
+
this.data.set(data);
|
|
366
|
+
} catch {
|
|
367
|
+
this.dataError.set(true);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
## Performance Optimization
|
|
374
|
+
|
|
375
|
+
### Lazy Hydration Strategy
|
|
376
|
+
|
|
377
|
+
```typescript
|
|
378
|
+
@Component({
|
|
379
|
+
template: `
|
|
380
|
+
<!-- Critical content - hydrate immediately -->
|
|
381
|
+
<header>
|
|
382
|
+
<app-navigation />
|
|
383
|
+
</header>
|
|
384
|
+
|
|
385
|
+
<!-- Main content - hydrate on viewport -->
|
|
386
|
+
<main>
|
|
387
|
+
@defer (hydrate on viewport) {
|
|
388
|
+
<app-product-grid [products]="products()" />
|
|
389
|
+
}
|
|
390
|
+
</main>
|
|
391
|
+
|
|
392
|
+
<!-- Below fold - hydrate on idle -->
|
|
393
|
+
@defer (hydrate on idle) {
|
|
394
|
+
<app-reviews [productId]="productId()" />
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
<!-- Interactive only - hydrate on interaction -->
|
|
398
|
+
@defer (hydrate on interaction) {
|
|
399
|
+
<app-chat-widget />
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
<!-- Static footer - never hydrate -->
|
|
403
|
+
@defer (hydrate never) {
|
|
404
|
+
<app-footer />
|
|
405
|
+
}
|
|
406
|
+
`,
|
|
407
|
+
})
|
|
408
|
+
export class ProductPage {}
|
|
409
|
+
```
|
|
410
|
+
|
|
411
|
+
### Preload Critical Data
|
|
412
|
+
|
|
413
|
+
```typescript
|
|
414
|
+
// app.routes.server.ts
|
|
415
|
+
export const serverRoutes: ServerRoute[] = [
|
|
416
|
+
{
|
|
417
|
+
path: 'products/:id',
|
|
418
|
+
renderMode: RenderMode.Server,
|
|
419
|
+
async getPrerenderParams() {
|
|
420
|
+
// Prerender top 100 products
|
|
421
|
+
const topProducts = await fetchTopProducts(100);
|
|
422
|
+
return topProducts.map(p => ({ id: p.id }));
|
|
423
|
+
},
|
|
424
|
+
},
|
|
425
|
+
];
|
|
426
|
+
```
|
|
427
|
+
|
|
428
|
+
### Streaming SSR (Experimental)
|
|
429
|
+
|
|
430
|
+
```typescript
|
|
431
|
+
// Enable streaming for faster TTFB
|
|
432
|
+
import { provideServerRendering } from '@angular/platform-server';
|
|
433
|
+
|
|
434
|
+
const serverConfig: ApplicationConfig = {
|
|
435
|
+
providers: [
|
|
436
|
+
provideServerRendering(),
|
|
437
|
+
// Streaming is automatic with @defer blocks
|
|
438
|
+
],
|
|
439
|
+
};
|
|
440
|
+
```
|
|
441
|
+
|
|
442
|
+
## Testing SSR
|
|
443
|
+
|
|
444
|
+
### Test Server Rendering
|
|
445
|
+
|
|
446
|
+
```typescript
|
|
447
|
+
import { renderApplication } from '@angular/platform-server';
|
|
448
|
+
import { App } from './app.component';
|
|
449
|
+
import { config } from './app.config.server';
|
|
450
|
+
|
|
451
|
+
describe('SSR', () => {
|
|
452
|
+
it('should render home page', async () => {
|
|
453
|
+
const html = await renderApplication(App, {
|
|
454
|
+
appId: 'my-app',
|
|
455
|
+
providers: config.providers,
|
|
456
|
+
url: '/',
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
expect(html).toContain('<h1>Welcome</h1>');
|
|
460
|
+
expect(html).toContain('</app-root>');
|
|
461
|
+
});
|
|
462
|
+
|
|
463
|
+
it('should render product page with data', async () => {
|
|
464
|
+
const html = await renderApplication(App, {
|
|
465
|
+
appId: 'my-app',
|
|
466
|
+
providers: config.providers,
|
|
467
|
+
url: '/products/123',
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
expect(html).toContain('Product Name');
|
|
471
|
+
expect(html).not.toContain('Loading...');
|
|
472
|
+
});
|
|
473
|
+
});
|
|
474
|
+
```
|
|
475
|
+
|
|
476
|
+
### Test Hydration
|
|
477
|
+
|
|
478
|
+
```typescript
|
|
479
|
+
import { TestBed } from '@angular/core/testing';
|
|
480
|
+
import { provideClientHydration } from '@angular/platform-browser';
|
|
481
|
+
|
|
482
|
+
describe('Hydration', () => {
|
|
483
|
+
beforeEach(() => {
|
|
484
|
+
TestBed.configureTestingModule({
|
|
485
|
+
providers: [provideClientHydration()],
|
|
486
|
+
});
|
|
487
|
+
});
|
|
488
|
+
|
|
489
|
+
it('should hydrate without errors', () => {
|
|
490
|
+
const fixture = TestBed.createComponent(App);
|
|
491
|
+
fixture.detectChanges();
|
|
492
|
+
|
|
493
|
+
// No hydration mismatch errors should be thrown
|
|
494
|
+
expect(fixture.componentInstance).toBeTruthy();
|
|
495
|
+
});
|
|
496
|
+
});
|
|
497
|
+
```
|