<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Daniel Mathieu</title>
    <description>The latest articles on Forem by Daniel Mathieu (@coderdan).</description>
    <link>https://forem.com/coderdan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F242503%2F41f1cc99-c888-4de5-9b83-5893ba1f33bc.png</url>
      <title>Forem: Daniel Mathieu</title>
      <link>https://forem.com/coderdan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/coderdan"/>
    <language>en</language>
    <item>
      <title>5 issues I encountered while converting a create-react-app to SSR (and how I solved them)</title>
      <dc:creator>Daniel Mathieu</dc:creator>
      <pubDate>Sat, 19 Oct 2019 05:27:08 +0000</pubDate>
      <link>https://forem.com/coderdan/5-issues-i-encountered-while-converting-a-create-react-app-to-ssr-and-how-i-solved-them-4lje</link>
      <guid>https://forem.com/coderdan/5-issues-i-encountered-while-converting-a-create-react-app-to-ssr-and-how-i-solved-them-4lje</guid>
      <description>&lt;p&gt;&lt;em&gt;This is the first post in a series I'll be writing about the creation of my new webapp Readlist. Readlist is an app that lets you earn money by creating and sharing book lists. Check it out here --&amp;gt; &lt;a href="https://readlist.io"&gt;https://readlist.io&lt;/a&gt; It's free!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you're new to ReactJS like me, you probably created your first app using &lt;code&gt;create-react-app&lt;/code&gt; (CRA). CRA is a super easy, beginner-friendly way to get started with React, but it comes with a big caveat: your app will most likely be "client-side only", which means that search engines such as Google and social sites such as Facebook and Twitter probably won't read your metatags, and therefore can't index or create social cards for your pages.&lt;/p&gt;

&lt;p&gt;If your web-app exists behind an auth barrier and you don't intend for the uninitiated to see it, then a CRA is probably fine. But what happens if you have a lot of outward-facing, publicly-accessible pages that desperately need to be SEO-friendly and read by the social networks?&lt;/p&gt;

&lt;h4&gt;
  
  
  Possible Solution: Use &lt;code&gt;react-helmet&lt;/code&gt;.
&lt;/h4&gt;

&lt;p&gt;React-helmet is a library that supposedly lets you append metatags to your head tag, and therefore be more easily indexed. I tried react-helmet in my app, but after several days of debugging, it just wasn't working. Therefore, I decided to take a deep dive and start exploring something called server-side rendering (SSR) that I'd seen people talking about.&lt;/p&gt;

&lt;h4&gt;
  
  
  Real Solution: Use Server-Side Rendering (NextJS)
&lt;/h4&gt;

&lt;p&gt;Server-side rendering, like it sounds, is a way to render your page on the server before serving it to the end-user. Faster loading times is one of the main benefits, but for me, the real benefit is loading in data before the render, which allows me to customize HTML metatags on the page before it gets indexed. &lt;/p&gt;

&lt;p&gt;After some research, I discovered two frameworks for SSR: Gatsby and NextJS. Gatsby is optimized for delivering static pages, but Readlist has lots of content that will update frequently, so I opted to use NextJS.&lt;/p&gt;

&lt;p&gt;With companies like TikTok, Nike, and Twitch using NextJS, you can be sure that NextJS is a capable framework for your SSR needs.&lt;/p&gt;

&lt;p&gt;In the remainder of this post, I'd like to go through 5 difficulties I encountered while converting my CRA app to NextJS, and how I solved them.&lt;/p&gt;

&lt;h4&gt;
  
  
  1. Handling Global State
&lt;/h4&gt;

&lt;p&gt;Global state is a must-have for a single-page app (SPA) like Readlist, which handles authentication on initial page load.&lt;/p&gt;

&lt;p&gt;In a normal CRA app, it's easy to wrap your main App component in a state provider like Redux or the React Context Provider. It's simple to do in NextJS as well, but it took me several times reading through the excellent NextJS docs to discover the proper way.&lt;/p&gt;

&lt;p&gt;The trick is to create an &lt;code&gt;_app.js&lt;/code&gt; folder in the &lt;code&gt;pages/&lt;/code&gt; directory and extend the App class. Then you can wrap the Component class (a NextJS class) with your State provider. Not bad!&lt;/p&gt;

&lt;h4&gt;
  
  
  2. Routing
&lt;/h4&gt;

&lt;p&gt;Routing your pages in NextJS is a bit more complicated than in a CRA app. To convert your CRA app into a NextJS app, you need to create a &lt;code&gt;pages/&lt;/code&gt; folder in the root directory and place all of your app pages there. &lt;/p&gt;

&lt;p&gt;Since NextJS allows for both server-side routing and client-side routing, it also uses a special Router component found in next/router. &lt;/p&gt;

&lt;p&gt;The NextJS router has its own custom Link component that handles client-side routing, but the syntax is a bit tricky. Link takes two properties: &lt;code&gt;href&lt;/code&gt; and &lt;code&gt;as&lt;/code&gt;. If you have a simple link like "about", then you can just use &lt;code&gt;href&lt;/code&gt;. However, if you need a more complicated link like /user/coderdannn, you will need both &lt;code&gt;href&lt;/code&gt; and &lt;code&gt;as&lt;/code&gt;. In this case, &lt;code&gt;href&lt;/code&gt; takes a &lt;em&gt;template&lt;/em&gt; of your URL, with the dynamic part encased in []. So in the above case, it would be &lt;code&gt;href="/user/[userID]"&lt;/code&gt;. Then in &lt;code&gt;as&lt;/code&gt;, you actually write the real path: &lt;code&gt;/user/${userID}&lt;/code&gt;. It took me several hours of trial and error to finally get that working!&lt;/p&gt;

&lt;h4&gt;
  
  
  3. getInitialProps
&lt;/h4&gt;

&lt;p&gt;getInitialProps is part of the magic of NextJS -- where the real server-side rendering really happens. NextJS pages are statically optimized by default, and therefore not server-side rendered. However, on those pages you want to contain metatags and be indexed or shared (such as user profile pages, or book lists) you will need to render them server-side. &lt;/p&gt;

&lt;p&gt;getInitialProps is the NextJS solution to that. getInitialProps can be called on the default export component of your .js or .tsx file, and it returns an object that, when finished, gets sent to the props of your default export. The page will not finish loading until that fetch has completed. For example, if I want to fetch a user object to display in my User component, I can call getInitialProps, await fetch my object, and then &lt;code&gt;return {userObject:fetchedObject}&lt;/code&gt;. It's that easy!&lt;/p&gt;

&lt;p&gt;Note that if you're using Firebase like I am, if you use getInitialProps to fetch your data, you cannot use the onSnapshot method to listen to changes in your objects. Which is kind of a bummer. &lt;/p&gt;

&lt;h4&gt;
  
  
  4. Environment Variables
&lt;/h4&gt;

&lt;p&gt;Environment variables work basically the same way as in a CRA app, except you need to also declare them in the next.config.js file within an &lt;code&gt;env:{}&lt;/code&gt; object. Then you can call &lt;code&gt;process.env.VAR_NAME&lt;/code&gt; as you would in a CRA app. Big thanks to  NextJS lead developer &lt;a class="comment-mentioned-user" href="https://dev.to/timneutkens"&gt;@timneutkens&lt;/a&gt;
 for pointing this out to me. I have to say that the NextJS community is super friendly and always willing to help out!&lt;/p&gt;

&lt;h4&gt;
  
  
  5. CSS and Dynamic Loading
&lt;/h4&gt;

&lt;p&gt;NextJS by default enables code-splitting, which splits your bundles into multiple pieces. This is great for server-side rendering because it means your user has less data to consume before becoming interactive. In addition, you can also dynamically require components using next/dynamic, which will split that component into its own separate bundle. &lt;/p&gt;

&lt;p&gt;One area to be careful is in your external css modules. For Readlist, I used a lot of global external CSS modules, which turned out to be pretty inefficient in NextJS because the entire CSS module had to load before it can render the page. NextJS recommends you use local CSS whenever possible, and it uses a special &lt;code&gt;styled-jsx&lt;/code&gt; library to achieve that. One of my biggest speed optimization gains was copying all of that external CSS into the files that consumed it. &lt;/p&gt;

&lt;h4&gt;
  
  
  Further Thoughts
&lt;/h4&gt;

&lt;p&gt;Converting my CRA app into a NextJS SSR was definitely a painful task, but then again, so is learning anything new. Thanks to all the great community members for writing great docs, and also for creating dozens of examples of NextJS on Github. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8sY9yrNn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/t525au5404ojvwg1xt0l.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8sY9yrNn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/t525au5404ojvwg1xt0l.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;My app performance still isn't perfect yet, as you can see above, but by converting to SSR, I achieved my main goal: allowing users to share their lists to Twitter and Facebook. High five! &lt;/p&gt;

&lt;p&gt;I'm active on Twitter so if you'd like to chat about developing, startups, books, living in Asia, or anything else, feel free to DM me! @coderdannn&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_LsG3VWl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/w83iotgqlaxz0m01oe8p.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_LsG3VWl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/w83iotgqlaxz0m01oe8p.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
