A Guide to Streaming Server-Side Rendering (SSR) with React 18
Posted on October 5, 2023 at 5:04 PM

Krste Rajchevski
Software Engineer @ Bugpilot
Prerequisites
Before diving into streaming SSR with React 18, it's important to have a basic understanding of React and server-side rendering. Familiarity with JavaScript and Node.js is also recommended. Additionally, ensure that you have React 18 installed and set up in your development environment.
What is Streaming SSR?
Streaming SSR is an optimization technique in which the server starts sending HTML response to the client as soon as possible, while the server continues to render the remaining JSX components. This approach allows the client to start parsing and rendering the HTML incrementally, resulting in faster perceived page load times and improved time-to-interaction.
Setting Up a React 18 Project
To get started with streaming SSR using React 18, let's set up a basic React project:
- Create a new directory for your project:
1mkdir my-react-app 2cd my-react-app
- Initialize a new Node.js project:
1npm init -y
- Install React, ReactDOM, and other required dependencies:
1npm install react react-dom
- Create a new file called
index.js
and import React and ReactDOM:
1import React from 'react'; 2import ReactDOM from 'react-dom';
- Create a new file called
App.js
and create a simple React component:
1import React from 'react'; 2const App = () => { 3 return ( 4 <div> 5 <h1>Hello, Streaming SSR!</h1> 6 <p>Welcome to my React 18 streaming SSR guide.</p> 7 </div> 8 ); 9}; 10export default App;
- In
index.js
, render theApp
component using ReactDOM:
1import React from 'react'; 2import ReactDOM from 'react-dom'; 3import App from './App'; 4ReactDOM.render(<App />, document.getElementById('root'));
- Create an
index.html
file in the root of your project and include aroot
element for React to mount onto:
1<!DOCTYPE html> 2<html> 3 <head> 4 <title>Streaming SSR with React 18</title> 5 </head> 6 <body> 7 <div id="root"></div> 8 <script src="index.js"></script> 9 </body> 10</html>
- Start a local development server and open your application in the browser:
1npm start
Congratulations! You've set up a basic React 18 project. Now let's dive into implementing streaming SSR with React 18.
Implementing Streaming SSR with React 18
To enable streaming SSR with React 18, we need to make a few modifications to our existing React project.
- Upgrade React and ReactDOM to version 18:
1npm install react@^18 react-dom@^18
- Import
@react-dom/server
inindex.js
:
1import { createServerRender } from 'react-dom/server';
- Modify the rendering code in
index.js
to use thecreateServerRender
method:
1import { createServerRender } from 'react-dom/server'; 2import App from './App'; 3const { html } = createServerRender(App); 4document.getElementById('root').innerHTML = html;
- Create a new file called
server.js
and import the necessary modules:
1import express from 'express'; 2import { createServerRender } from 'react-dom/server'; 3import App from './App'; 4const app = express(); 5const { html } = createServerRender(App); 6app.get('/', (req, res) => { 7 res.write('<!DOCTYPE html>'); 8 res.write('<html>'); 9 res.write('<head>'); 10 res.write('<title>Streaming SSR with React 18</title>'); 11 res.write('</head>'); 12 res.write('<body>'); 13 res.write('<div id="root">'); 14 res.write(html); 15 res.write('</div>'); 16 res.write('<script src="index.js"></script>'); 17 res.write('</body>'); 18 res.write('</html>'); 19 res.end(); 20}); 21app.listen(3000, () => { 22 console.log('Server is running on http://localhost:3000'); 23});
- Modify the
start
script inpackage.json
to run the server:
1{ 2 "scripts": { 3 "start": "node server.js" 4 } 5}
- Start the server and visit http://localhost:3000 in your browser. You should see the same content as before, but now rendered using streaming SSR.
Conclusion
In this guide, we explored how to implement streaming SSR with React 18. We learned about the concept of streaming SSR, set up a basic React 18 project, and made the necessary modifications to enable streaming SSR. By leveraging streaming SSR, you can greatly improve the perceived performance and user experience of your React applications. Happy coding!
I hope you found this guide helpful and insightful. Feel free to experiment with different features and optimizations to further enhance the performance of your streaming SSR implementation. Stay ahead of the curve with React 18 and take advantage of its powerful capabilities for server-side rendering.
Posted by

Krste Rajchevski
Software Engineer @ Bugpilot
Get started with Bugpilot
Bugpilot makes it easy to find and fix errors in your web apps. Our AI can find hidden user-facing bugs, and alert you before your users do.
More from this author
Mastering Configuration Files in Next.js for Optimal Performance
Configuration Files in Next js Next js provides several configuration files that allow you to…
Read more
Generating PDFs in React with react-pdf
What is react pdf React pdf is a JavaScript library that allows you to render PDF documents…
Read more
React 18, React Redux 8, and TypeScript: What You Should Know
React is one of the most popular JavaScript libraries for building user interfaces It provides a…
Read more
Top 3 Logrocket Alternatives in 2023 That You Need to Try Out Now
Logrocket has long been a go to tool for developers when it comes to application monitoring and…
Read more
Demystifying Next.js Hydration Errors: Causes, Fixes, and Best Practices
What are Hydration Errors Hydration errors in Next js occur when the initial HTML sent from…
Read more
Deploy a Next.js app to AWS
Prerequisites Before we dive into the deployment process make sure you have the following…
Read more