<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Varun Sisodia on Medium]]></title>
        <description><![CDATA[Stories by Varun Sisodia on Medium]]></description>
        <link>https://medium.com/@varunsisodia?source=rss-f0301aacdee4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*J0lda61BI2f-Ey4K.</url>
            <title>Stories by Varun Sisodia on Medium</title>
            <link>https://medium.com/@varunsisodia?source=rss-f0301aacdee4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 22:03:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@varunsisodia/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[React and Redux Server Side Rendering]]></title>
            <link>https://medium.com/@varunsisodia/react-and-redux-server-side-rendering-eca5c9c6fe9d?source=rss-f0301aacdee4------2</link>
            <guid isPermaLink="false">https://medium.com/p/eca5c9c6fe9d</guid>
            <dc:creator><![CDATA[Varun Sisodia]]></dc:creator>
            <pubDate>Sat, 15 Aug 2020 14:27:12 GMT</pubDate>
            <atom:updated>2020-08-15T14:37:55.716Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/60/0*oTZ7CVczGw1RyGD5" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*5LCf5nSxmC2daRhW.png" /></figure><p>This tutorial will give you basic understanding how to build React components, collect Redux store on server and make it usable in browser.</p><p>This guide is aimed at those who have basic knowledge of React and NodeJs.</p><h3>Pre requirements:</h3><ul><li>Installed <a href="https://nodejs.org/">Node.js</a></li><li>Basic knowledge of JavaScript.</li><li>npm or <a href="https://yarnpkg.com/en/">yarn</a></li></ul><h3>Getting started</h3><p>As this guide focused exceptionally on ssr , i offering you to clone my github react project <a href="https://github.com/varunsingh09/react-ssr-boilerplate.git">https://github.com/varunsingh09/react-ssr-redux-react</a>. This is react boilerplate only, without server side rendering.</p><p>Once downloading complete run install command yarn or npm install</p><p>After that, run yarn start command and navigate to localhost:8080</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/60/0*BRkAak_O8dEi3Wog" /></figure><p>If you see something like this, everything is ok and you installed project on the right way. I leave link to countries api <a href="https://restcountries.eu/rest/v2">https://restcountries.eu/rest/v2</a> , so you can feel free to play with.</p><h3>The Problem</h3><p>Move to the console and see React warning message.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/60/0*gpR1QznbLlAxArrK" /></figure><p>Then switch to the Network tab and choose localhost on the left bar.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/60/0*VGT7zqQNGswV1b9P" /></figure><p>The problem is that content generated locally by react does not match to content returned by server. So necessary to write server which will return original content.</p><h3>Server</h3><p>For correct handling ES6 features on servers bundle set up webpack server configuration.</p><p>Install externals plugin for webpack and express server library.</p><pre>yarn add webpack-node-externals express</pre><p>Create webpack.server.config.js in root of the project and put some code in there.</p><pre><strong>const </strong>path = require(&#39;path&#39;);<br><strong>const </strong>webpackNodeExternals = require(&#39;webpack-node-externals&#39;);</pre><pre>module.exports = {<br>  mode: &#39;development&#39;,<br>  target: &#39;node&#39;,<br>  entry: &#39;./server/index.js&#39;,<br>  output: {<br>    path: path.resolve(__dirname),<br>    filename: &#39;server.js&#39;<br>  },<br>  module: {<br>    rules: [<br>      {<br>        test: /\.js$/,<br>        use: &#39;babel-loader&#39;,<br>        exclude: /node_modules/<br>      }<br>    ]<br>  },<br>  externals: [webpackNodeExternals()]<br>};</pre><p>Put server bundling script to the package.json file.</p><pre>&quot;scripts&quot;: {<br>  &quot;start&quot;: &quot;webpack-dev-server&quot;,<br>  &quot;build&quot;: &quot;webpack --config webpack.build.config.js&quot;,<br>  &quot;server-build&quot;: &quot;webpack --config webpack.server.config.js&quot;<br>},</pre><p>Create server directory in the root of the project, then create index.js file in there. Set up your server in server/index.js file.</p><pre><strong>import </strong>express <strong>from </strong>&#39;express&#39;;</pre><pre><strong>const </strong>PORT = 8079;<br><strong>const </strong>app = express();</pre><pre>app.listen(PORT, () =&gt; console.log(`Frontend service listening on port: ${PORT}`));</pre><p>Run react bundle build command at first:</p><pre>yarn build</pre><p>Then run server bundling command:</p><pre>yarn server-build</pre><p>Once this command complete you should see server.js file in the root of your project. Run server by node js.</p><pre>node server.js</pre><p>Look at the terminal, you should see console message from your server.</p><pre>Frontend service listening on port: 8079</pre><p>Time to add route handler to server/index.js file.</p><pre><strong>import </strong>express <strong>from </strong>&#39;express&#39;;</pre><pre><strong>const </strong>PORT = 8079;<br><strong>const </strong>app = express();<br></pre><pre>app.get(&#39;*&#39;, (req, res) =&gt; {<br>  <strong>const </strong>raw =`<br>  &lt;!DOCTYPE html&gt;<br>      &lt;html lang=&quot;en&quot;&gt;<br>      &lt;head&gt;<br>        &lt;meta charset=&quot;UTF-8&quot;&gt;<br>        &lt;title&gt;Title&lt;/title&gt;<br>      &lt;/head&gt;<br>      &lt;body&gt;<br>      <br>      &lt;div id=&quot;app&quot;&gt;&lt;/div&gt;<br>      &lt;script src=&quot;dist/bundle.js&quot;&gt;&lt;/script&gt;<br>      &lt;/body&gt;<br>      &lt;/html&gt;<br>  `;</pre><pre>  res.send(raw);<br>});</pre><pre>app.listen(PORT, () =&gt; console.log(`Frontend service listening on port: ${PORT}`));</pre><p>Rebuild server and start again, switch to the browser and you will see raw content returning by the server.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/60/0*VVbM6-zl6wNfkT8v" /></figure><p>But there is no fetched countries there, because when browser calls scripts /dist/bundle.js server does not understand thats, and on each route server returning content. Fix that using express.static</p><pre><strong>import </strong>express <strong>from </strong>&#39;express&#39;;</pre><pre><strong>const </strong>PORT = 8079;<br><strong>const </strong>app = express();</pre><pre>app.use(&#39;/dist&#39;, express.static(&#39;dist&#39;));<br>app.use(&#39;/img&#39;, express.static(&#39;img&#39;));</pre><pre>app.get(&#39;*&#39;, (req, res) =&gt; {<br>  <strong>const </strong>raw =`<br>  &lt;!DOCTYPE html&gt;<br>      &lt;html lang=&quot;en&quot;&gt;<br>      &lt;head&gt;<br>        &lt;meta charset=&quot;UTF-8&quot;&gt;<br>        &lt;title&gt;Title&lt;/title&gt;<br>      &lt;/head&gt;<br>      &lt;body&gt;<br>      <br>      &lt;div id=&quot;app&quot;&gt;&lt;/div&gt;<br>      &lt;script src=&quot;dist/bundle.js&quot;&gt;&lt;/script&gt;<br>      &lt;/body&gt;<br>      &lt;/html&gt;<br>  `;</pre><pre>  res.send(raw);<br>});</pre><pre>app.listen(PORT, () =&gt; console.log(`Frontend service listening on port: ${PORT}`));</pre><p>Switch to the browser and see countries.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/60/0*Wa95vPV1J7HOrY8j" /></figure><p>But in server response #app container still empty, and warning massage on console still present.</p><p>As we can not use BrowserRouter on server side, we will use StaticRouter . Also we have same set up as frontend, but wrap it all by renderToString function from react-dom/server library.</p><pre><strong>import </strong>React <strong>from </strong>&#39;react&#39;;<br><strong>import </strong>express <strong>from </strong>&#39;express&#39;;<br><strong>import </strong>{ Provider } <strong>from </strong>&#39;react-redux&#39;;<br><strong>import </strong>{ StaticRouter } <strong>from </strong>&#39;react-router-dom&#39;;<br><strong>import </strong>{ renderToString } <strong>from </strong>&#39;react-dom/server&#39;;<br><strong>import </strong>store <strong>from </strong>&#39;../src/store&#39;;<br><strong>import </strong>Routes <strong>from </strong>&#39;../src/router/Routes&#39;;</pre><pre><strong>const </strong>PORT = 8079;<br><strong>const </strong>app = express();</pre><pre>app.use(&#39;/dist&#39;, express.static(&#39;dist&#39;));<br>app.use(&#39;/img&#39;, express.static(&#39;img&#39;));</pre><pre>app.get(&#39;*&#39;, (req, res) =&gt; {<br>  <strong>const </strong>content = renderToString(<br>    &lt;Provider <em>store</em>={store}&gt;<br>      &lt;StaticRouter&gt;<br>        &lt;Routes /&gt;<br>      &lt;/StaticRouter&gt;<br>    &lt;/Provider&gt;<br>  );</pre><pre>  <strong>const </strong>raw =`<br>  &lt;!DOCTYPE html&gt;<br>      &lt;html lang=&quot;en&quot;&gt;<br>      &lt;head&gt;<br>        &lt;meta charset=&quot;UTF-8&quot;&gt;<br>        &lt;title&gt;Title&lt;/title&gt;<br>      &lt;/head&gt;<br>      &lt;body&gt;<br>      <br>      &lt;div id=&quot;app&quot;&gt;${content}&lt;/div&gt;<br>      &lt;script src=&quot;dist/bundle.js&quot;&gt;&lt;/script&gt;<br>      &lt;/body&gt;<br>      &lt;/html&gt;<br>  `;</pre><pre>  res.send(raw);<br>});</pre><pre>app.listen(PORT, () =&gt; console.log(`Frontend service listening on port: ${PORT}`));</pre><p>To make ease working with raw render features, create server/render.js file and past all render things to it.</p><p>server/render.js file should looks like this:</p><pre><strong>import </strong>React <strong>from </strong>&#39;react&#39;;<br><strong>import </strong>{ renderToString } <strong>from </strong>&#39;react-dom/server&#39;;<br><strong>import </strong>{ Provider } <strong>from </strong>&#39;react-redux&#39;;<br><strong>import </strong>{ StaticRouter } <strong>from </strong>&#39;react-router-dom&#39;;<br><strong>import </strong>Routes <strong>from </strong>&#39;../src/router/Routes&#39;;</pre><pre><strong>export default </strong>store =&gt; {<br>  <strong>const </strong>content = renderToString(<br>    &lt;Provider <em>store</em>={store}&gt;<br>      &lt;StaticRouter&gt;<br>        &lt;Routes /&gt;<br>      &lt;/StaticRouter&gt;<br>    &lt;/Provider&gt;<br>  );<br></pre><pre>  <strong>return </strong>`<br>  &lt;!DOCTYPE html&gt;<br>      &lt;html lang=&quot;en&quot;&gt;<br>      &lt;head&gt;<br>        &lt;meta charset=&quot;UTF-8&quot;&gt;<br>        &lt;title&gt;Title&lt;/title&gt;<br>      &lt;/head&gt;<br>      &lt;body&gt;<br>      <br>      &lt;div id=&quot;app&quot;&gt;${content}&lt;/div&gt;<br>    <br>      &lt;script src=&quot;dist/bundle.js&quot;&gt;&lt;/script&gt;<br>      &lt;/body&gt;<br>      &lt;/html&gt;<br>  `;<br>};</pre><p>server/index.js file should looks like this:</p><pre><strong>import </strong>express <strong>from </strong>&#39;express&#39;;<br><strong>import </strong>render <strong>from </strong>&#39;./render&#39;;<br><strong>import </strong>store <strong>from </strong>&#39;../src/store&#39;;</pre><pre><strong>const </strong>PORT = 8079;<br><strong>const </strong>app = express();</pre><pre>app.use(&#39;/dist&#39;, express.static(&#39;dist&#39;));<br>app.use(&#39;/img&#39;, express.static(&#39;img&#39;));</pre><pre>app.get(&#39;*&#39;, (req, res) =&gt; {<br>  <strong>const </strong>content = render(store);</pre><pre>  res.send(content);<br>});</pre><pre>app.listen(PORT, () =&gt; console.log(`Frontend service listening on port: ${PORT}`));</pre><p>Add store object to raw content and stringify it before.</p><pre><strong>import </strong>React <strong>from </strong>&#39;react&#39;;<br><strong>import </strong>{ renderToString } <strong>from </strong>&#39;react-dom/server&#39;;<br><strong>import </strong>{ Provider } <strong>from </strong>&#39;react-redux&#39;;<br><strong>import </strong>{ StaticRouter } <strong>from </strong>&#39;react-router-dom&#39;;<br><strong>import </strong>Routes <strong>from </strong>&#39;../src/router/Routes&#39;;</pre><pre><strong>export default </strong>store =&gt; {<br>  <strong>const </strong>content = renderToString(<br>    &lt;Provider <em>store</em>={store}&gt;<br>      &lt;StaticRouter&gt;<br>        &lt;Routes /&gt;<br>      &lt;/StaticRouter&gt;<br>    &lt;/Provider&gt;<br>  );<br></pre><pre>  <strong>return </strong>`<br>  &lt;!DOCTYPE html&gt;<br>      &lt;html lang=&quot;en&quot;&gt;<br>      &lt;head&gt;<br>        &lt;meta charset=&quot;UTF-8&quot;&gt;<br>        &lt;title&gt;Title&lt;/title&gt;<br>      &lt;/head&gt;<br>      &lt;body&gt;<br>      <br>      &lt;div id=&quot;app&quot;&gt;${content}&lt;/div&gt;<br>      &lt;script&gt;<br>        window.INITIAL_STATE = ${JSON.stringify(store.getState())}<br>      &lt;/script&gt;<br>      &lt;script src=&quot;dist/bundle.js&quot;&gt;&lt;/script&gt;<br>      &lt;/body&gt;<br>      &lt;/html&gt;<br>  `;<br>};</pre><p>Build and run server again, navigate to browser, look at localhost on Network tab and see store object and rendered content returned from server.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/60/0*qM4e6D6aFUvrGCe7" /></figure><p>Error in console gone missing. you can see html content, but .countries-container is empty. To fix that necessary to handle fetching inside components on server. But before handling fetching need to completely rewrite routes.</p><p>Install react-router-config library at first:</p><pre>yarn add react-router-config</pre><p>Once installation finished move to /src/router/Routes.js file and set up routes schema.</p><pre><strong>import </strong>Countries <strong>from </strong>&quot;../components/Countries&quot;;<br><strong>import </strong>Country <strong>from </strong>&quot;../components/Country&quot;;<br></pre><pre><strong>export default </strong>[<br>  {<br>    component: Countries,<br>    path: &#39;/&#39;,<br>    exact: <strong>true<br>  </strong>},<br>  {<br>    component: Country,<br>    path: &#39;/:name&#39;<br>  }<br>];</pre><p>Next go to the /src/router/index.js file and modify it to server routes from router schema:</p><pre><strong>import </strong>React <strong>from </strong>&#39;react&#39;;<br><strong>import </strong>{ BrowserRouter } <strong>from </strong>&#39;react-router-dom&#39;;<br><strong>import </strong>{ renderRoutes } <strong>from </strong>&#39;react-router-config&#39;;<br><strong>import </strong>Routes <strong>from </strong>&#39;./Routes&#39;;</pre><pre><strong>export default </strong>() =&gt; {<br>  <strong>return </strong>(<br>    &lt;BrowserRouter&gt;<br>      &lt;div&gt;{renderRoutes(Routes)}&lt;/div&gt;<br>    &lt;/BrowserRouter&gt;<br>  );<br>};</pre><p>You can stop server and run frontend by command yarn start to make sure that new router definition works fine.</p><p>Move to to server/render.js file and do same thing with router such as frontend.</p><pre><strong>import </strong>React <strong>from </strong>&#39;react&#39;;<br><strong>import </strong>{ renderToString } <strong>from </strong>&#39;react-dom/server&#39;;<br><strong>import </strong>{ Provider } <strong>from </strong>&#39;react-redux&#39;;<br><strong>import </strong>{ StaticRouter } <strong>from </strong>&#39;react-router-dom&#39;;<br><strong>import </strong>{ renderRoutes } <strong>from </strong>&#39;react-router-config&#39;;<br><strong>import </strong>Routes <strong>from </strong>&#39;../src/router/Routes&#39;;</pre><pre><strong>export default </strong>(pathname, store, context) =&gt; {<br>  <strong>const </strong>content = renderToString(<br>    &lt;Provider <em>store</em>={store}&gt;<br>      &lt;StaticRouter <em>location</em>={pathname} <em>context</em>={context}&gt;<br>        &lt;div&gt;{renderRoutes(Routes)}&lt;/div&gt;<br>      &lt;/StaticRouter&gt;<br>    &lt;/Provider&gt;<br>  );<br></pre><pre>  <strong>return </strong>`<br>  &lt;!DOCTYPE html&gt;<br>      &lt;html lang=&quot;en&quot;&gt;<br>      &lt;head&gt;<br>        &lt;meta charset=&quot;UTF-8&quot;&gt;<br>        &lt;title&gt;Title&lt;/title&gt;<br>      &lt;/head&gt;<br>      &lt;body&gt;<br>      <br>      &lt;div id=&quot;app&quot;&gt;${content}&lt;/div&gt;<br>      &lt;script&gt;<br>        window.INITIAL_STATE = ${JSON.stringify(store.getState())}<br>      &lt;/script&gt;<br>      &lt;script src=&quot;dist/bundle.js&quot;&gt;&lt;/script&gt;<br>      &lt;/body&gt;<br>      &lt;/html&gt;<br>  `;<br>};</pre><p>Rebuild and run server to make sure there is no errors. Than look at your terminal and see context warning:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/60/0*_dLzIF68eQzMSlgi" /></figure><p>To fix that and finish work with server/render.js file pass context object and pathname to it StaticRouter</p><pre><strong>import </strong>React <strong>from </strong>&#39;react&#39;;<br><strong>import </strong>{ renderToString } <strong>from </strong>&#39;react-dom/server&#39;;<br><strong>import </strong>{ Provider } <strong>from </strong>&#39;react-redux&#39;;<br><strong>import </strong>{ StaticRouter } <strong>from </strong>&#39;react-router-dom&#39;;<br><strong>import </strong>{ renderRoutes } <strong>from </strong>&#39;react-router-config&#39;;<br><strong>import </strong>Routes <strong>from </strong>&#39;../src/router/Routes&#39;;</pre><pre><strong>export default </strong>(pathname, store, context) =&gt; {<br>  <strong>const </strong>content = renderToString(<br>    &lt;Provider <em>store</em>={store}&gt;<br>      &lt;StaticRouter <em>location</em>={pathname} <em>context</em>={context}&gt;<br>        &lt;div&gt;{renderRoutes(Routes)}&lt;/div&gt;<br>      &lt;/StaticRouter&gt;<br>    &lt;/Provider&gt;<br>  );<br></pre><pre>  <strong>return </strong>`<br>  &lt;!DOCTYPE html&gt;<br>      &lt;html lang=&quot;en&quot;&gt;<br>      &lt;head&gt;<br>        &lt;meta charset=&quot;UTF-8&quot;&gt;<br>        &lt;title&gt;Title&lt;/title&gt;<br>      &lt;/head&gt;<br>      &lt;body&gt;<br>      <br>      &lt;div id=&quot;app&quot;&gt;${content}&lt;/div&gt;<br>      &lt;script&gt;<br>        window.INITIAL_STATE = ${JSON.stringify(store.getState())}<br>      &lt;/script&gt;<br>      &lt;script src=&quot;dist/bundle.js&quot;&gt;&lt;/script&gt;<br>      &lt;/body&gt;<br>      &lt;/html&gt;<br>  `;<br>};</pre><p>Do not forget pass it from server/index.js</p><pre><strong>import </strong>express <strong>from </strong>&#39;express&#39;;<br><strong>import </strong>render <strong>from </strong>&#39;./render&#39;;<br><strong>import </strong>store <strong>from </strong>&#39;../src/store&#39;;</pre><pre><strong>const </strong>PORT = 8079;<br><strong>const </strong>app = express();</pre><pre>app.use(&#39;/dist&#39;, express.static(&#39;dist&#39;));<br>app.use(&#39;/img&#39;, express.static(&#39;img&#39;));</pre><pre>app.get(&#39;*&#39;, (req, res) =&gt; {<br>  <strong>const </strong>context = {};<br>  <strong>const </strong>content = render(req.path, store, context);</pre><pre>  res.send(content);<br>});</pre><pre>app.listen(PORT, () =&gt; console.log(`Frontend service listening on port: ${PORT}`));</pre><p>Rebuild and rerun server, context warning message should be disappear.</p><h3>Fetching</h3><p>Move to src/components/Countries/index.js file, look at Countries component. To make easy fetching from on server add static fetching function to it. Server will only get static fetching method from this class, handle it and attach result of this method to store. After that server will pass full of data store object to the client.</p><pre><strong>import </strong>React, { Component } <strong>from </strong>&#39;react&#39;;<br><strong>import </strong>{ connect } <strong>from </strong>&#39;react-redux&#39;;<br><strong>import </strong>{ fetchCountries } <strong>from </strong>&quot;../../action/countries&quot;;<br><strong>import </strong>{ Loading } <strong>from </strong>&#39;../../common&#39;;<br><strong>import </strong>CountriesItem <strong>from </strong>&#39;./CountriesItem&#39;;</pre><pre><strong>const </strong>m = ({ countries }) =&gt; ({ countries });<br></pre><pre>@connect(m, { fetchCountries })<br><strong>export default class </strong>Countries <strong>extends </strong>Component {</pre><pre>  <strong>static </strong>fetching ({ dispatch }) {<br>    <strong>return </strong>[dispatch(fetchCountries())];<br>  }</pre><pre>  componentDidMount() {<br>    <strong>this</strong>.props.fetchCountries();<br>  }<br></pre><pre>  render() {<br>    <strong>const </strong>{ countries: { isFetching, data } } = <strong>this</strong>.props;</pre><pre>    <strong>if</strong>(isFetching) {<br>      <strong>return </strong>&lt;Loading /&gt;<br>    }</pre><pre>    <strong>return</strong>(<br>      &lt;div <em>className</em>=&quot;container&quot;&gt;<br>        &lt;div <em>className</em>=&quot;countries-container&quot;&gt;<br>          {data.map((item, i) =&gt; &lt;CountriesItem <em>key</em>={i} {...item} /&gt;)}<br>        &lt;/div&gt;<br>      &lt;/div&gt;<br>    );<br>  }<br>};</pre><p>fething method returns array because initial actions can be more then 1. Each of element can returning promise, so server should handle all the promises from all components returned from StackRouter.</p><p>Move to server/index.js file and handle all the promises.</p><pre><strong>import </strong>express <strong>from </strong>&#39;express&#39;;<br><strong>import </strong>{ renderToString } <strong>from </strong>&#39;react-dom/server&#39;;<br><strong>import </strong>{ matchRoutes } <strong>from </strong>&#39;react-router-config&#39;;<br><strong>import </strong>render <strong>from </strong>&#39;./render&#39;;<br><strong>import </strong>store <strong>from </strong>&#39;../src/store&#39;;<br><strong>import </strong>Routes <strong>from </strong>&#39;../src/router/Routes&#39;;<br></pre><pre><strong>const </strong>PORT = process.env.PORT || 8079;<br><strong>const </strong>app = express();</pre><pre>app.use(&#39;/dist&#39;, express.static(&#39;dist&#39;));<br>app.use(&#39;/img&#39;, express.static(&#39;img&#39;));<br>app.get(&#39;*&#39;, <strong>async </strong>(req, res) =&gt; {<br></pre><pre>  <strong>const </strong>actions = matchRoutes(Routes, req.path)<br>    .map(({ route }) =&gt; route.component.fetching ? route.component.fetching(store) : <strong>null</strong>)<br>    .map(<strong>async </strong>actions =&gt; <strong>await </strong>Promise.all(<br>      (actions || []).map(p =&gt; p &amp;&amp; <strong>new </strong>Promise(resolve =&gt; p.then(resolve).catch(resolve)))<br>      )<br>    );</pre><pre>  <strong>await  </strong>Promise.all(actions);<br>  <strong>const </strong>context = {};<br>  <strong>const </strong>content = render(req.path, store, context);</pre><pre>  res.send(content);<br>});</pre><pre>app.listen(PORT, () =&gt; console.log(`Frontend service listening on port: ${PORT}`));</pre><p>After all promises will handled, server will return full of data store, and full of content html raw. Rebuild and rerun server, switch to the browser. Open Network tab, click on localhost on the left bar.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/60/0*xnW4wyezd0qeUunA" /></figure><p>Ssr works fine. But what about single country? If you navigate to <a href="http://localhost:8079/United%20States%20of%20America">http://localhost:8079/United%20States%20of%20America</a> server returns only html mok for this country, but not the data.</p><p>To fix that move to src/components/Country/index.js , look at componentDidMount hook, you will see <strong>this</strong>.props.fetchCountry(<strong>this</strong>.props.match.params.name); . Necessary to pass country name somehow. Remember that, but now just write static fetching method in this class.</p><pre><strong>static </strong>fetching ({ dispatch, path }) {<br>  <strong>return </strong>[dispatch(fetchCountry(path.substr(1)))];<br>}</pre><p>That pass pathname to fetching method move to server/index.js and destruct store object and pass req.path to it.</p><pre><strong>import </strong>express <strong>from </strong>&#39;express&#39;;<br><strong>import </strong>{ renderToString } <strong>from </strong>&#39;react-dom/server&#39;;<br><strong>import </strong>{ matchRoutes } <strong>from </strong>&#39;react-router-config&#39;;<br><strong>import </strong>render <strong>from </strong>&#39;./render&#39;;<br><strong>import </strong>store <strong>from </strong>&#39;../src/store&#39;;<br><strong>import </strong>Routes <strong>from </strong>&#39;../src/router/Routes&#39;;<br></pre><pre><strong>const </strong>PORT = process.env.PORT || 8079;<br><strong>const </strong>app = express();</pre><pre>app.use(&#39;/dist&#39;, express.static(&#39;dist&#39;));<br>app.use(&#39;/img&#39;, express.static(&#39;img&#39;));<br>app.get(&#39;*&#39;, <strong>async </strong>(req, res) =&gt; {<br></pre><pre>  <strong>const </strong>actions = matchRoutes(Routes, req.path)<br>    .map(({ route }) =&gt; route.component.fetching ? route.component.fetching({...store, path: req.path }) : <strong>null</strong>)<br>    .map(<strong>async </strong>actions =&gt; <strong>await </strong>Promise.all(<br>      (actions || []).map(p =&gt; p &amp;&amp; <strong>new </strong>Promise(resolve =&gt; p.then(resolve).catch(resolve)))<br>      )<br>    );</pre><pre>  <strong>await  </strong>Promise.all(actions);<br>  <strong>const </strong>context = {};<br>  <strong>const </strong>content = render(req.path, store, context);</pre><pre>  res.send(content);<br>});</pre><pre>app.listen(PORT, () =&gt; console.log(`Frontend service listening on port: ${PORT}`));</pre><p>Rebuild and rerun server and you will se raw html with data, and country data in store returned from server.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/60/0*ctYFG2ObAUYdwH0b" /></figure><h3>Done</h3><p>This is easiest way setting up server side rendering with react and redux. I leave link to finished sample <a href="https://github.com/varunsingh09/react-ssr-redux-react">https://github.com/varunsingh09/react-ssr-redux-react</a></p><p>If yo have any questions or propositions feel free to ask responses.</p><p>Thank for reading.</p><ul><li><a href="https://medium.com/tag/react">React</a></li><li><a href="https://medium.com/tag/javascript">JavaScript</a></li><li><a href="https://medium.com/tag/redux">Redux</a></li><li><a href="https://medium.com/tag/expressjs">Expressjs</a></li></ul><p>WRITTEN BY</p><h3>Varun Singh</h3><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eca5c9c6fe9d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>