Integration of React Components with Server Side Rendering
This guide walks you through setting up Syncfusion React components with server-side rendering (SSR) using Express.js.
What is SSR?
Server-Side Rendering (SSR) is a technique where a React application is rendered on the server instead of in the browser, allowing the server to generate and send fully rendered HTML to the client. This results in a faster initial page load, as users see meaningful content immediately without waiting for JavaScript to execute. It also improves SEO, since search engines can crawl the complete HTML content, enhances user experience on slower networks by reducing perceived loading time.
SSR works by processing the React components on the server using tools like ReactDOMServer, generating HTML, and sending it to the browser, where React then hydrates the content to make it interactive.
Prerequisites
System requirements for Syncfusion® React UI components
Set up the React project
To get started with SSR and Syncfusion® React components, set up a basic React application using Create React App.
Add Syncfusion® React packages
To add the React Toolbar component, install the @syncfusion/react-navigations package by running the following command:
Install SSR dependencies
For server-side rendering functionality with Express, install additional packages. Run the following command to install all the required dependencies:
Configure server
- First, create a
serverdirectory in the project root and add a file namedroot.jsinside it. This file will serve as the entry point for the SSR process.
- Next, create a file named
server.jsinside theserverdirectory. This server component is responsible for rendering the React application on the server and delivering it to the client. Configure it as follows:
Configure client
The client-side React application needs to be structured to work correctly with SSR:
- Now, let's update the client entry point file
src/index.js. This file will hydrate the React application on the client side after the initial server-side render:
- Update
src/App.jsfile that will contains Syncfusion® React component. This component will be rendered both on the server and client sides:
Run the project
Before running the project, update the package.json to include the server script for running the SSR implementation.
- Add the following script to the
package.jsonfile:
- First, build the production version of the React application:
- After building the application, start the
Expressserver that handles SSR:
The output will appear as follows: