How Server Rendering Works
When a request arrives at your server, TanStack Start follows this flow:- Request Handler: The
createStartHandlerreceives the incoming request - Router Initialization: A router instance is created with memory history for the requested URL
- Route Matching: The router matches the URL path to your route definitions
- Data Loading: Route loaders execute on the server to fetch data
- Component Rendering: React components render to HTML string or stream
- Hydration Data: Serialized state is embedded in the HTML for client hydration
Rendering Modes
TanStack Start supports two rendering approaches:String Rendering
Renders the entire HTML as a string before sending the response. Best for static pages or when you need the complete HTML immediately.renderRouterToString from @tanstack/react-router/ssr/server:
Stream Rendering
Streams HTML to the client as it renders, allowing the browser to start processing content before the entire page is ready. This is the recommended approach for better perceived performance.renderRouterToStream:
SSR Configuration
Control SSR behavior per route or globally:Per-Route SSR Control
app/routes/index.tsx
Disable SSR for Specific Routes
app/routes/dashboard.tsx
Global SSR Configuration
Set default SSR behavior for all routes:app/start.ts
The StartServer Component
The<StartServer> component wraps your router on the server:
- Renders the
<RouterProvider>with the server router instance - Manages SSR-specific context and state
- Coordinates with the serialization system
Server-Side Data Loading
Route loaders execute on the server during SSR:app/routes/posts.$postId.tsx
- Fetched on the server
- Serialized and embedded in the HTML
- Available immediately on the client without refetching
Hydration
After the server sends HTML, the client “hydrates” the page:app/client.tsx
- The client router reads serialized state from the HTML
- React attaches event listeners to the server-rendered HTML
- The app becomes interactive without re-rendering
Request Context
Access request information during SSR:Response Headers
Set response headers during SSR:Asset Manifest
TanStack Start automatically generates an asset manifest during build that includes:- JavaScript module preloads
- CSS stylesheets
- Client entry script
createStartHandler:
Error Handling
Handle errors during SSR:Best Practices
Use streaming for better performance
Use streaming for better performance
Stream rendering (
defaultStreamHandler) provides better perceived performance by sending content as it renders:Optimize loader execution
Optimize loader execution
Keep loaders fast and focused. Use parallel loading when possible:
Handle environment-specific code
Handle environment-specific code
Use conditional logic for server-only code:
Set appropriate cache headers
Set appropriate cache headers
Use response headers to control caching:
Related Concepts
Streaming
Learn how to stream content progressively
Server Functions
Execute server-side logic from client components
Deployment
Deploy your SSR application to production
SSR Guide
Complete guide to SSR and streaming