Skip to content

Commit ec348ea

Browse files
committed
fix: migrate ssr.md with platform-specific providers section
Added: - NOTE about preferring platform-specific providers - IMPORTANT warning about isPlatformBrowser in templates - New section: Providing platform-specific implementations (71 lines) Fixed lint errors: - ユーザーエクスペリエンス -> ユーザー体験 - Removed doubled joshi particle
1 parent cdffbf2 commit ec348ea

File tree

1 file changed

+71
-0
lines changed
  • adev-ja/src/content/guide

1 file changed

+71
-0
lines changed

adev-ja/src/content/guide/ssr.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,13 +267,84 @@ export class MyComponent {
267267
}
268268
```
269269

270+
NOTE: `isPlatformBrowser``isPlatformServer` を使用したランタイムチェックよりも、[プラットフォーム固有のプロバイダー](guide/ssr#providing-platform-specific-implementations)を推奨します。
271+
272+
IMPORTANT: テンプレートで `isPlatformBrowser``@if` やその他の条件と共に使用して、サーバーとクライアントで異なるコンテンツをレンダリングすることは避けてください。これはハイドレーションの不一致やレイアウトシフトを引き起こし、ユーザー体験と[Core Web Vitals](https://web.dev/learn-core-web-vitals/)に悪影響を与えます。代わりに、ブラウザ固有の初期化には `afterNextRender` を使用し、レンダリングされるコンテンツをプラットフォーム間で一貫性のあるものに保ちます。
273+
270274
## サーバーでプロバイダーを設定する {#setting-providers-on-the-server}
271275

272276
サーバー側では、トップレベルのプロバイダー値は、アプリケーションコードが最初に解析および評価されたときに一度設定されます。
273277
これは、`useValue` で設定されたプロバイダーは、サーバーアプリケーションが再起動されるまで、複数のリクエストにわたって値を保持することを意味します。
274278

275279
リクエストごとに新しい値を生成したい場合は、`useFactory` を使用してファクトリープロバイダーを使用します。ファクトリー関数は、受信リクエストごとに実行され、毎回新しい値が作成されてトークンに割り当てられることを保証します。
276280

281+
## プラットフォーム固有の実装を提供する {#providing-platform-specific-implementations}
282+
283+
アプリケーションがブラウザとサーバーで異なる動作を必要とする場合、各プラットフォーム用に個別のサービス実装を提供します。このアプローチにより、プラットフォームロジックを専用のサービスに集中させることができます。
284+
285+
```ts
286+
export abstract class AnalyticsService {
287+
abstract trackEvent(name: string): void;
288+
}
289+
```
290+
291+
ブラウザ実装を作成します。
292+
293+
```ts
294+
@Injectable()
295+
export class BrowserAnalyticsService implements AnalyticsService {
296+
trackEvent(name: string): void {
297+
// ブラウザベースのサードパーティ分析プロバイダーにイベントを送信します
298+
}
299+
}
300+
```
301+
302+
サーバー実装を作成します。
303+
304+
```ts
305+
@Injectable()
306+
export class ServerAnalyticsService implements AnalyticsService {
307+
trackEvent(name: string): void {
308+
// サーバーでイベントを記録します
309+
}
310+
}
311+
```
312+
313+
メインアプリケーション構成でブラウザ実装を登録します。
314+
315+
```ts
316+
// app.config.ts
317+
export const appConfig: ApplicationConfig = {
318+
providers: [
319+
{ provide: AnalyticsService, useClass: BrowserAnalyticsService },
320+
]
321+
};
322+
```
323+
324+
サーバー構成でサーバー実装をオーバーライドします。
325+
326+
```ts
327+
// app.config.server.ts
328+
const serverConfig: ApplicationConfig = {
329+
providers: [
330+
{ provide: AnalyticsService, useClass: ServerAnalyticsService },
331+
]
332+
};
333+
```
334+
335+
コンポーネントでサービスを注入して使用します。
336+
337+
```ts
338+
@Component({/* ... */})
339+
export class Checkout {
340+
private analytics = inject(AnalyticsService);
341+
342+
onAction() {
343+
this.analytics.trackEvent('action');
344+
}
345+
}
346+
```
347+
277348
## DIを介したDocumentへのアクセス {#accessing-document-via-di}
278349

279350
サーバーサイドレンダリングを使用する場合、`document` のようなブラウザ固有のグローバルを直接参照することは避けるべきです。代わりに、[`DOCUMENT`](api/core/DOCUMENT) トークンを使用して、プラットフォームに依存しない方法でdocumentオブジェクトにアクセスします。

0 commit comments

Comments
 (0)