Server-Side Rendering (SSR)
Server-Side Rendering (SSR) is a core feature of TanStack Start that renders your React components on the server before sending them to the client. This improves initial load times, SEO, and provides a better user experience.What is SSR?
SSR means:- Server-side execution: Components render on the server first
- HTML generation: Full HTML is sent to the browser
- Hydration: React attaches event handlers on the client
- Progressive enhancement: Page works even before JavaScript loads
- SEO-friendly: Search engines can index your content
How SSR Works in TanStack Start
When a request comes in:- Request received: Server receives HTTP request
- Router matches: TanStack Router finds matching routes
- Loaders execute: Route loaders run on the server
- Components render: React renders components to HTML
- HTML sent: Generated HTML is sent to browser
- Hydration: Client-side React takes over
packages/start-server-core/src/createStartHandler.ts:424-647
Basic Setup
TanStack Start handles SSR automatically:examples/react/start-basic/src/router.tsx:1-16
SSR with Data Loading
Route loaders execute on the server during SSR:Hydration
Hydration is the process of attaching React event handlers to server-rendered HTML:- React compares server HTML with client render
- Event listeners are attached to existing DOM
- Application becomes interactive
- Client-side navigation is enabled
SSR Context
Access server-side context in your components:packages/start-server-core/src/createStartHandler.ts:596-600
SSR Utilities
TanStack Start provides utilities for SSR:Router SSR Utils
- Asset preloading
- Critical CSS injection
- Deferred data streaming
packages/start-server-core/src/createStartHandler.ts:568-571
Preventing SSR for Specific Components
Some components should only render on the client:Head Management
Manage document head during SSR:Headers and Status Codes
Control HTTP response during SSR:packages/start-server-core/src/createStartHandler.ts:582-584
Error Handling
Handle errors during SSR:Deferred Data
Defer non-critical data to speed up initial render:examples/react/start-basic/src/routes/deferred.tsx:18-29
Memory Management
TanStack Start automatically cleans up router state after SSR:packages/start-server-core/src/createStartHandler.ts:637-645
Asset Preloading
TanStack Start automatically preloads critical assets:packages/start-server-core/src/createStartHandler.ts:561-565
Development vs Production
Development
- Fresh manifest on each request
- Includes route-specific dev styles
- Source maps enabled
- Detailed error messages
packages/start-server-core/src/createStartHandler.ts:165-167
Production
- Cached manifest (computed once)
- Minified bundles
- Optimized asset URLs
- Production error handling
packages/start-server-core/src/createStartHandler.ts:169-171
CDN Integration
Transform asset URLs for CDN hosting:packages/start-server-core/src/createStartHandler.ts:59-111
Shell Mode
Generate an empty shell for static hosting:packages/start-server-core/src/createStartHandler.ts:474-477
Best Practices
1. Keep Loaders Fast
2. Use Deferred Data
3. Set Appropriate Cache Headers
4. Handle Errors Gracefully
Performance Tips
1. Parallel Data Loading
2. Database Query Optimization
3. Smart Preloading
Next Steps
- Learn about Streaming for progressive rendering
- Explore Server Functions for data mutations
- Review Deployment for production hosting
- Understand Full-Stack Architecture for the complete picture