Guides & Tutorials

A Guide to Streaming Server-Side Rendering (SSR) with React 18

Posted on October 5, 2023 at 5:04 PM

Author's avatar

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:

  1. Create a new directory for your project:
1mkdir my-react-app
2cd my-react-app
  1. Initialize a new Node.js project:
1npm init -y
  1. Install React, ReactDOM, and other required dependencies:
1npm install react react-dom
  1. Create a new file called index.js and import React and ReactDOM:
1import React from 'react';
2import ReactDOM from 'react-dom';
  1. 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;
  1. In index.js, render the App component using ReactDOM:
1import React from 'react';
2import ReactDOM from 'react-dom';
3import App from './App';
4ReactDOM.render(<App />, document.getElementById('root'));
  1. Create an index.html file in the root of your project and include a root 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>
  1. 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.

  1. Upgrade React and ReactDOM to version 18:
1npm install react@^18 react-dom@^18
  1. Import @react-dom/server in index.js:
1import { createServerRender } from 'react-dom/server';
  1. Modify the rendering code in index.js to use the createServerRender method:
1import { createServerRender } from 'react-dom/server';
2import App from './App';
3const { html } = createServerRender(App);
4document.getElementById('root').innerHTML = html;
  1. 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});
  1. Modify the start script in package.json to run the server:
1{
2  "scripts": {
3    "start": "node server.js"
4  }
5}
  1. 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.

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.