Skip to main content

Server-Side Rendering (SSR)

Server-Side Rendering (SSR) improves initial page load performance and SEO by rendering your application on the server. TanStack Router provides full SSR support through TanStack Start.

Overview

SSR with TanStack Router:
  • Renders HTML on the server for faster initial loads
  • Provides full hydration support
  • Enables streaming SSR for progressive rendering
  • Supports data loading on the server
  • Works with React Server Components (RSC)
For full SSR support with TanStack Router, use TanStack Start - the official full-stack framework built on TanStack Router.

TanStack Start

TanStack Start is the recommended way to use SSR with TanStack Router:
Start provides:
  • File-based routing with SSR out of the box
  • Server functions
  • API routes
  • Streaming SSR
  • React Server Components support

Basic SSR Setup

Root Route with SSR

src/routes/__root.tsx

Server-Side Data Loading

Load data on the server before rendering:
src/routes/posts.tsx

Server Functions

Define functions that only run on the server:
src/routes/users.tsx

API Routes

Create API endpoints:
src/routes/api/posts.ts

Streaming SSR

Stream HTML as it’s generated for faster time-to-first-byte:
src/routes/dashboard.tsx

Deferred Data Loading

Defer non-critical data to speed up initial render:
src/routes/posts/$postId.tsx

Meta Tags and SEO

Dynamic meta tags for SEO:
src/routes/posts/$postId.tsx

Environment-Specific Code

Run code only on server or client:

Error Handling in SSR

Handle errors during server rendering:
src/routes/posts/$postId.tsx

Static Site Generation (SSG)

Generate static HTML at build time:

Hydration

TanStack Router automatically handles hydration:
src/main.tsx

Best Practices

Use Server Functions

Keep database queries and sensitive logic in server functions

Defer Non-Critical Data

Use defer() for data that isn’t needed immediately

Optimize Images

Use optimized image components and lazy loading

Cache Aggressively

Cache server responses to reduce load times
Performance: SSR improves initial page load but adds server processing time. Use streaming and deferred data to optimize.
Client-only APIs: Avoid using browser APIs (window, document) in code that runs on the server. Use environment checks.

Deployment

Deploy SSR apps to platforms that support Node.js:
  • Vercel: Zero-config deployment
  • Netlify: Edge functions support
  • Cloudflare Workers: Edge runtime
  • AWS: Lambda or EC2
  • Any Node.js host: VPS, containers, etc.

Next Steps

TanStack Start Docs

Learn more about TanStack Start

Code Splitting

Optimize bundle size with lazy loading