the calls to rendertostring() are synchronous, so they are blocking the thread while they are running. so its no surprise that when you have 100+ concurrent requests that you have an extremely blocked up queue hanging for ~10 seconds.

edit: it was pointed out that react v16 natively supports streaming, but you need to use the rendertonodestream() method for streaming the html to the client. it should return the exact same string as rendertostring() but streams it instead, so you don't have to wait for the full html to be rendered before you start sending data to the client.


1. run express in a cluster

a single instance of node.js runs in a single thread. to take advantage of multi-core systems, the user will sometimes want to launch a cluster of node.js processes to handle the load.

as node is single threaded the problem may also be in a file lower down the stack were you are initialising express.

there are a number of best practices when running a node app that are not generally mentioned in react threads.

a simple solution to improve performance on a server running multiple cores is to use the built in node cluster module

this will start multiple instance of your app on each core of your server giving you a significant performance improvement (if you have a multicore server) for concurrent requests

see for more information on express performance

you may also want to throttle you incoming connections as when the thread starts context switching response times drop rapidly this can be done by adding something like nginx / ha proxy in front of your application

2. wait for the store to be hydrated before calling render to string

you don't want to have to render you layout until your store has finished updating as other comments note this is a blocks the thread while rendering.

below is the example taken from the saga repo which shows how to run the sagas with out the need to render the template until they have all resolved

  store.runsaga(rootsaga).done.then(() => {
    console.log('sagas complete')
  }).catch((e) => {

3. make sure node environment is set correctly

also ensure you are correctly using node_env=production when bundling / running your code as both express and react optimise for this


enter image description here

in the above image, i am doing reactdom.hydrate(...) i can also load my initial and required state and send it down in hydrate.

enter image description here

i have written the middleware file and i am using this file to decide based on what url i should send which file in response.

enter image description here

above is my middleware file, i have created the html string of the whichever file was requested based on url. then i add this html string and return it using res.render of express.

enter image description here

above image is where i compare the requested url path with the dictionary of path-file associations. once it is found (i.e. url matches) i use reactdomserver render to string to convert it into html. this html can be used to send with handle bar file using res.render as discussed above.

this way i have managed to do ssr on my most web apps built using stack.

hope my answer helped you and please write comment for discussions

Related Query

More Query from same tag