I noticed that the reactDOM.renderToString() method starts to slow down significantly when rendering a large component tree on the server.
Background
A bit of background. The system is a completely isomorphic stack. The top-level component of the App displays templates, pages, dom elements, and other components. Looking at the reaction code, I found that it renders ~ 1500 components (this applies to any simple dom tag, which is considered to be a simple component, <p>this is a react component</p> .
In development, rendering ~ 1500 components takes ~ 200-300ms. By removing some components, I was able to get ~ 1200 components for visualization in ~ 175-225 ms.
In production, renderToString for ~ 1500 components takes about 50-200 ms.
Time seems linear. None of the components are slow, but rather the sum of many.
Problem
This creates some problems on the server. A lengthy method results in longer server response times. TTFB is much higher than it should be. For API calls and business logic, the response should be 250 ms, and when rendering 250 ms, it will double! Bad for SEO and users. In addition, being a synchronous method, renderToString() can block the host server and create backup copies of subsequent requests (this can be solved using 2 separate host servers: 1 as a web server and 1 as a service designed solely to respond to rendering) .
Attempts
Ideally, rendering a ToString in production would require 5-50 ms. I worked on some ideas, but I'm not quite sure what the best approach would be.
Idea 1: Component Caching
Any component that is marked as “static” can be cached. By renderToString() cache with visualized markup, renderToString() can check the cache before rendering. If it finds a component, it automatically captures the string. Doing this on a high-level component will save the mounting of all nested child components. You need to replace the rootID of the reactive partitioning of the rootID component with the current rootID.
Idea 2: Labeling Components as Simple / Mute
By defining a component as “simple,” the reaction should skip all life cycle methods when rendering. React already does this for the main response components ( <p/> , <h1/> , etc.). It would be nice to extend custom components to use the same optimization.
Idea 3: Skip components when rendering on the server side
Components that should not be returned by the server (without an SEO value) may simply be skipped on the server. Once the client boots up, set the clientLoaded flag to true and pass it down to force re-rendering.
Closing and other attempts
The only solution I have implemented so far is to reduce the number of components displayed on the server.
Here are some of the projects we're looking at:
Has anyone encountered similar problems? What could you do? Thank you