home / skills / hoangnguyen0403 / agent-skills-standard / ssr
This skill helps implement server-side rendering practices by guiding hydration, transfer state, and prerendering for React and Next.js apps.
npx playbooks add skill hoangnguyen0403/agent-skills-standard --skill ssrReview the files below or copy the command above to add this skill to your agents.
---
name: SSR (Server-Side Rendering)
description: Hydration, TransferState, and Prerendering standards.
metadata:
labels: [angular, ssr, hydration, server-side]
triggers:
files: ['**/*.server.ts', 'server.ts']
keywords: [hydration, transferState, afterNextRender, isPlatformServer]
---
# SSR (Server-Side Rendering)
## **Priority: P2 (MEDIUM)**
## Principles
- **Hydration**: Enable Client Hydration in `app.config.ts`.
- **Platform Checks**: Use `afterNextRender` or `afterRender` for browser-only code (e.g., accessing `window`).
- **TransferState**: Use `makeStateKey` and `TransferState` to prevent double-fetching data on client.
## Guidelines
- **Browser Objects**: Never access `window`, `document`, or `localStorage` directly in component logic. Implement abstractions or use `afterNextRender`.
- **Prerendering**: Use SSG for static pages (Marketing, Blogs).
## References
- [Hydration](references/hydration.md)
This skill codifies server-side rendering (SSR) best practices focused on hydration, TransferState, and prerendering. It guides TypeScript and framework-specific implementations to avoid client/server pitfalls and to optimize first-load performance. The goal is predictable hydration behavior, no duplicate data fetches, and appropriate use of static generation.
The skill inspects rendering flows and recommends enabling client hydration in app configuration so server-rendered markup becomes interactive. It enforces platform checks by suggesting afterRender/afterNextRender hooks for browser-only code and prescribes TransferState patterns (makeStateKey + TransferState) to serialize server-fetched data to the client and prevent double-fetching. It also directs when to use prerendering/SSG for static content.
What is TransferState and when should I use it?
TransferState serializes server-fetched data into the HTML so the client can reuse it on hydration, preventing duplicate API calls. Use it whenever you fetch data on the server that will also be needed immediately by client code.
How do I avoid referencing window or document during SSR?
Avoid direct access in shared code. Use platform checks or run browser-only code inside afterRender/afterNextRender hooks or inject an abstraction that safely returns undefined on the server.