<?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 &quot;Apak&quot; mahmud apak on Medium]]></title>
        <description><![CDATA[Stories by &quot;Apak&quot; mahmud apak on Medium]]></description>
        <link>https://medium.com/@apakmahmud?source=rss-26545bc16911------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*n5tVwTEWU2_mzpkv6fmOUQ.png</url>
            <title>Stories by &amp;quot;Apak&amp;quot; mahmud apak on Medium</title>
            <link>https://medium.com/@apakmahmud?source=rss-26545bc16911------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 04:04:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@apakmahmud/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[Server-Side Rendering React with .NET Core]]></title>
            <link>https://medium.com/@apakmahmud/server-side-rendering-react-with-net-core-1f3eaf8962fb?source=rss-26545bc16911------2</link>
            <guid isPermaLink="false">https://medium.com/p/1f3eaf8962fb</guid>
            <category><![CDATA[server-side-rendering]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[dotnet-core]]></category>
            <dc:creator><![CDATA["Apak" mahmud apak]]></dc:creator>
            <pubDate>Wed, 15 May 2019 11:41:07 GMT</pubDate>
            <atom:updated>2020-01-24T06:03:32.180Z</atom:updated>
            <content:encoded><![CDATA[<h3>Server-Side Rendering React with .NET Core</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*MFgDa9vfsC9pP-H8" /><figcaption>Photo by <a href="https://unsplash.com/@shan_mcinnes?utm_source=medium&amp;utm_medium=referral">Shannon McInnes</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Server-side rendering at a glance</h3><p>There are couple of benefits to server-side rendering such as SEO and initial render time. To see a comparison between ssr and none-ssr app you can checkout <a href="https://codeburst.io/next-js-ssr-vs-create-react-app-csr-7452f71599f6">this post</a>. We will show you how to create one and explain things necessary on the way.</p><p>To do server-side rendering what exactly we want is that send an html page which includes two things. First one is a markup of our rendered app along with initial state attached to window object. Second one is our good old client-side js file which will eventually render our classic react app in the browser side.</p><p><strong>Disclaimer</strong>: Be aware that this is not a complete explanation to ssr itself this is just a practical example of how to do it.</p><h4>prerequisites:</h4><p>Basic understanding of <em>webpack</em> , <em>dotnet-mvc</em> and <em>react</em> will help you along the way in order to get most out of the following tutorial.</p><h3><em>Tutorial</em></h3><ul><li><em>You need dotnet-core-sdk and nodejs installed in your environment</em></li></ul><h4>Dotnet-react project without ssr</h4><p>After you install dotnet-core sdk you should be able to use dotnet’s cli which has a command initializes a <a href="https://facebook.github.io/create-react-app/">cra</a>-like app , it is served by dotnet. So, here is the command:</p><pre>dotnet new react -o my-ssr-app</pre><p>First thing we will do is to <strong>remove everything under ClientApp folder </strong>basicly the whole cra-like app</p><h4><strong>Init NPM</strong></h4><p>Inside the root of our project my-ssr-app, initialize a node package</p><pre>cd my-ssr-app<br>npm init</pre><ul><li>install dev-dependencies below</li></ul><pre>npm i -D webpack webpack-cli webpack-dev-middleware webpack-merge babel-loader css-loader style-loader @babel/core @babel/preset-env @babel/preset-react aspnet-prerendering aspnet-webpack</pre><ul><li>and also install these dependencies</li></ul><pre>npm i react react-dom react-helmet react-router-dom react-router history react-redux</pre><p>you might want to add some other dependencies as well, depending on your needs. Add following scripts to package.json</p><pre>&quot;build&quot;: &quot;dotnet build &amp;&amp; webpack&quot;,</pre><pre>&quot;build-prod&quot;: &quot;dotnet build &amp;&amp; webpack --env.prod=true&quot;,</pre><ul><li>Add .babelrc file to your project root with below content; This will allow us to use latest javascript features and jsx in our code:</li></ul><pre>{<br>&quot;presets&quot;: [&quot;@babel/preset-env&quot;, &quot;@babel/preset-react&quot;]<br>}</pre><h4>Add webpack</h4><ul><li>Add webpack.config.js file to root of your project.</li></ul><p>We need two output bundles one for the server to render a static page and the other one for client app, so we need two different webpack configs but these have common properties so we will create a shared object then we will use it and create two different bundles. Our basic webpack structure will be like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2797c7418b6d6dbd1bc4f6689da0cfaa/href">https://medium.com/media/2797c7418b6d6dbd1bc4f6689da0cfaa/href</a></iframe><p>Of course <a href="https://gist.github.com/Apak00/7deabe7bb6747930faee685a70958b11">this</a> can be extended with performance optimization plugins and so on but It will be enough to generate two bundles for us once we run build script</p><h3>Server-side rendering</h3><h4>Steps:</h4><p><strong>1) dotnet routing</strong></p><p><strong>2) pre-rendering a static page</strong> in the cshtml before we send it to browser</p><p><strong>3) Injecting necessary data to cshtml</strong> in order to let client-side javascript to go on where server-side left off</p><h4>1) dotnet routing</h4><ul><li>In Startup.cs file replace app.useMvc and app.UseSpa with following:</li></ul><pre>app.UseMvc();</pre><ul><li>Add following inside ConfigureServices after services.AddMvc</li></ul><pre>services.AddSpaPrerenderer();</pre><pre>services.AddNodeServices();</pre><p>Also we need to connect webpack to dotnet so that dotnet uses webpack dev middleware to handle requests, to do this:</p><ul><li>Add following “using” to top of file</li></ul><pre>using Microsoft.AspNetCore.SpaServices.Webpack;</pre><ul><li>Add following right after app.UseDeveloperExceptionPage();</li></ul><pre>app.UseWebpackDevMiddleware();</pre><ul><li>Replace services.AddMvc() with following which will match all urls and respond with index.cshtml file</li></ul><pre>services.AddMvc().AddRazorPagesOptions(options =&gt;{<br>options.Conventions.AddPageRoute(&quot;/index&quot;, &quot;{*url}&quot;);<br>})</pre><ul><li>Remove everything under Pages folder and add an index.cshtml file with below content:</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b05afb679d2df7848bb2bf3845e5bd18/href">https://medium.com/media/b05afb679d2df7848bb2bf3845e5bd18/href</a></iframe><p>Remove Controllers folder since we use razor pages (see that “@page” tag ) we wont be needing any controller.</p><p>At this point we should be able to see TESTING but there is no server-side rendering of react yet when we run the command:</p><pre>dotnet run</pre><h4>2) pre-rendering a static page</h4><p>Now we will add necessary pieces to render a page in the index.cshtml file</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a51ec125049573ee84b7073f620dc38b/href">https://medium.com/media/a51ec125049573ee84b7073f620dc38b/href</a></iframe><p>Notice that we need to provide redux state information and save it in the window object for later use. In the next step we will provide main-server.js</p><p><strong>3) Injecting necessary data to cshtml</strong></p><ul><li>Create a file called boot-server.js under ClientApp folder. This file will handle creation of necessary markup and redux state in the server to complete the above cshtml file</li><li>boot-server.js:</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3d456f9fdfa2296ec969bd35e5f3f23b/href">https://medium.com/media/3d456f9fdfa2296ec969bd35e5f3f23b/href</a></iframe><ul><li>Create a file called boot-client.js under ClientApp folder. This file will be added to html we send using script tag. It will be our client app, only difference between this file and a classic react app will be that it will rehydrate the data in the existing markup which comes from server, in this case it is window.initialReduxState. Keep in mind that this file is bundled with name of main-client.js</li><li>boot-client.js:</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/65a46d35a081e09032f8e06926d2f9a9/href">https://medium.com/media/65a46d35a081e09032f8e06926d2f9a9/href</a></iframe><ul><li>Create a file called configureStore.js under ClientApp folder. This will generate us stores in both boot-client.js and boot-server.js files those 2 stores will be completely separate from each other. The one on server-side will be trash after we send html to client-side because we will sent it’s current state with window object and the one on the client-side will be initialized using this data(window.initialReduxState).</li><li>configureStore.js:</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/52c3741d14a2e44781ab654490b9ebcf/href">https://medium.com/media/52c3741d14a2e44781ab654490b9ebcf/href</a></iframe><p>To bring up this tutorial I used <a href="https://github.com/NickMaev/react-core-boilerplate">this boiler plate</a> and simplified it</p><p>You can checkout full repo <a href="https://github.com/Apak00/react-dotnet-ssr">here</a></p><p>Thanks and if you have any question don’t hesitate to ask.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1f3eaf8962fb" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>