Server Rendering Overview
React provides several APIs for rendering React components on the server. These APIs are exported fromreact-dom/server and enable server-side rendering (SSR), static site generation (SSG), and streaming HTML to the client.
When to Use Server Rendering
Server rendering is useful when you need to:- Improve initial page load performance - Send HTML to the client immediately, allowing users to see content before JavaScript loads
- Enhance SEO - Search engines can crawl and index your content more effectively
- Support devices with limited JavaScript - Provide a baseline experience on devices where JavaScript may be disabled or slow
- Enable progressive enhancement - Start with working HTML and enhance with JavaScript as it loads
Server Rendering APIs
React provides different server rendering APIs for different use cases:Legacy APIs (Synchronous)
renderToString
Synchronously render React components to an HTML string with React attributes
renderToStaticMarkup
Synchronously render React components to static HTML without React attributes
- No Suspense support
- Blocking - must wait for entire tree to render
- No streaming capabilities
- Not recommended for new projects
Modern APIs (Streaming)
renderToPipeableStream
Stream HTML to a Node.js Writable stream with full Suspense support
renderToReadableStream
Stream HTML using Web Streams API for edge runtimes and browsers
- Full Suspense support
- Progressive streaming - send HTML as it becomes ready
- Better user experience with selective hydration
- Error boundaries work correctly
- Recommended for all new projects
Runtime Differences
React server APIs work across different JavaScript runtimes:- Node.js
- Edge Runtime
- Bun
react-dom/server.node- Node.js environments (default)react-dom/server.edge- Edge runtimes (Vercel Edge, Cloudflare Workers)react-dom/server.bun- Bun runtimereact-dom/server.browser- Browser environments (rare)
SSR vs SSG
Server-Side Rendering (SSR)
Render HTML on-demand for each request:- Content changes frequently
- Personalized content per user
- Real-time data
Static Site Generation (SSG)
Pre-render HTML at build time:- Content rarely changes
- Same content for all users
- Maximum performance
Streaming Architecture
Streaming allows you to send HTML to the client in chunks as it becomes ready:Shell vs Content
- Shell: Initial UI structure, sent on
onShellReady - Content: Everything else, streamed progressively
- Suspense boundaries: Wait for async data, stream when ready
Server Components
React Server Components let you write components that run only on the server:Error Handling
All streaming APIs support error handling callbacks:Best Practices
Use streaming APIs for new projects
Use streaming APIs for new projects
renderToPipeableStream and renderToReadableStream provide better performance and user experience than legacy renderToString.Implement proper error boundaries
Implement proper error boundaries
Use Error Boundaries to catch and handle errors gracefully during server rendering.
Optimize Suspense boundaries
Optimize Suspense boundaries
Place Suspense boundaries strategically to balance initial content delivery with progressive loading.
Use onShellReady for interactive content
Use onShellReady for interactive content
Start streaming in
onShellReady to minimize time to first byte for users.Consider using onAllReady for crawlers
Consider using onAllReady for crawlers
For SEO purposes, wait for
onAllReady when serving content to search engine crawlers.Next Steps
renderToString
Learn about synchronous HTML generation
renderToPipeableStream
Implement streaming SSR in Node.js
renderToReadableStream
Use Web Streams API for edge runtimes
Server Components
Build with React Server Components