<?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 Antony Budianto on Medium]]></title>
        <description><![CDATA[Stories by Antony Budianto on Medium]]></description>
        <link>https://medium.com/@antonybudianto?source=rss-128419355b17------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*XZ3Nk42NhTdut7BWm4qeKA.jpeg</url>
            <title>Stories by Antony Budianto on Medium</title>
            <link>https://medium.com/@antonybudianto?source=rss-128419355b17------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 20:55:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@antonybudianto/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 Testing with react-testing-library]]></title>
            <link>https://medium.com/@antonybudianto/react-testing-with-react-testing-library-3b8554f6acfd?source=rss-128419355b17------2</link>
            <guid isPermaLink="false">https://medium.com/p/3b8554f6acfd</guid>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[software-testing]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[testing]]></category>
            <dc:creator><![CDATA[Antony Budianto]]></dc:creator>
            <pubDate>Sun, 25 Nov 2018 09:45:17 GMT</pubDate>
            <atom:updated>2019-02-07T02:02:39.380Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*GeTKyss99H4Q5cWqFsNO8Q.jpeg" /></figure><p>Hi, first of all I’d like to <strong>thank you</strong> for all your support for my <a href="https://medium.com/@antonybudianto/react-router-testing-with-jest-and-enzyme-17294fefd303">React Router Testing with Jest and enzyme</a> article that reached over <strong>100+ fans</strong> and <strong>600+ claps! </strong>It’s been really meaningful to me and motivated me to write new article about testing.</p><p>So this time I’d like to review the recently popular testing library: <a href="https://github.com/kentcdodds/react-testing-library">react-testing-library</a> by <a href="https://medium.com/@kentcdodds">Kent C. Dodds</a>.</p><p>From its README:</p><blockquote>The react-testing-library is a very light-weight solution for testing React components. It provides light utility functions on top of react-dom and react-dom/test-utils, in a way that encourages better testing practices.</blockquote><p>I recently used <a href="https://github.com/kentcdodds/react-testing-library">react-testing-library</a> in one of my OSS project (<a href="https://github.com/antonybudianto/react-save-localstorage">react-save-localstorage</a>) and I think it’s good, simple to use, and works seamlessly with <a href="https://jestjs.io/">Jest</a>.</p><h4>Testing simple React component</h4><p>Let’s say we have a simple component:</p><pre>import React from &#39;react&#39;</pre><pre>const Hello = ({ name }) =&gt; &lt;div&gt;{name}&lt;/div&gt;</pre><pre>export default Hello</pre><p>I’d like to test that the name renders correctly, so let’s write the test:</p><pre>import { render, cleanup } from &#39;react-testing-library&#39;<br>import Hello from &#39;./Hello&#39;</pre><pre>afterEach(cleanup)</pre><pre>describe(&#39;Hello&#39;, () =&gt; { <br>  it(&#39;should contains name&#39;, () =&gt; {<br>    const { getByText } = render(&lt;Hello name=&quot;Antony&quot; /&gt;)<br>    getByText(&#39;Antony&#39;)<br>  })<br>})</pre><p>Let’s review one by one:</p><ul><li><strong>cleanup</strong>: Unmounts React trees that were mounted with <a href="https://github.com/kentcdodds/react-testing-library#render">render</a>.</li><li><strong>render: </strong>Render into a container which is appended to document.body</li><li><strong>getByText</strong> will search for all elements that have a text node with textContent matching the given <a href="https://github.com/kentcdodds/react-testing-library#textmatch">TextMatch</a></li></ul><p>Let’s change name=&quot;Antony&quot; into name=&quot;Whoops&quot; and you’ll get an error telling that the element cannot be found:</p><pre>Unable to find an element with the text: Whoops</pre><h4>Testing props change</h4><p>Testing props change is also simple, you can use <strong>rerender</strong> utils after rendering once.</p><pre>import { render, cleanup } from &#39;react-testing-library&#39;<br>import Hello from &#39;./Hello&#39;</pre><pre>afterEach(cleanup)</pre><pre>describe(&#39;Hello&#39;, () =&gt; { <br>  it(&#39;should contains name with prop change&#39;, () =&gt; {<br>    const { getByText, rerender } = render(&lt;Hello name=&quot;Antony&quot; /&gt;)<br>    getByText(&#39;Antony&#39;)<br>    rerender(&lt;Hello name=&quot;Budi&quot; /&gt;)<br>    getByText(&#39;Budi&#39;)<br>  })<br>})</pre><h4>Testing events</h4><p>You can also test events like clicking element using <strong>fireEvent</strong> util.</p><pre>import { render, cleanup, fireEvent } from &#39;react-testing-library&#39;<br>import Hello from &#39;./Hello&#39;</pre><pre>afterEach(cleanup)</pre><pre>describe(&#39;Hello&#39;, () =&gt; { <br>  it(&#39;should click on text&#39;, () =&gt; {<br>    const { getByText } = render(&lt;Hello name=&quot;Antony&quot; /&gt;)<br>    const textEl = getByText(&#39;Antony&#39;)<br>    fireEvent.click(textEl)<br>  })<br>})</pre><h4>Testing async rendering</h4><p>You can also wait for an element to resolve promise and check its output using <strong>waitForElement</strong>:</p><pre>import { <br>  render,<br>  cleanup,<br>  waitForElement<br>} from &#39;react-testing-library&#39;<br>import Hello from &#39;./Hello&#39;</pre><pre>afterEach(cleanup)</pre><pre>describe(&#39;Hello&#39;, () =&gt; { <br>  it(&#39;should contains name&#39;, async () =&gt; {<br>    const { getByText } = render(&lt;Hello name=&quot;Antony&quot; /&gt;)<br>    await waitForElement(() =&gt; getByText(&#39;Antony&#39;))<br>  })<br>})</pre><h4>Debugging experience</h4><p>This is one reason why I love <strong>react-testing-library</strong>, when there’s failed expectation, it’ll print a nice DOM structure so you can debug clearly:</p><pre>Unable to find an element with the text: Whoops. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.</pre><pre>&lt;body&gt;<br>      &lt;div&gt;<br>        &lt;div&gt;<br>          Budi<br>        &lt;/div&gt;<br>      &lt;/div&gt;<br>    &lt;/body&gt;</pre><p>You can also trigger this manually by calling <strong>debug</strong> after render:</p><pre>const { debug } = render(&lt;Hello name=&quot;Antony&quot; /&gt;)</pre><pre>debug()</pre><p>Okay I think that’s all from me, actually there’s still a lot of APIs to cover but I think it’s a good introduction to <a href="https://github.com/kentcdodds/react-testing-library">react-test-library</a>. I’ll probably update the article if I find some good APIs. I hope it’s useful for you all.</p><p>If you like the article, please share with your friends and don’t forget to clap! Look forward for the next article. ❤</p><p>P.S. I just started my <a href="https://www.youtube.com/channel/UCHgFuZ220_FDdxFL7OSay4A">new YouTube channel</a>, and I’ve posted “Learn React testing in 2 minutes”, <strong>please watch, like, share, and subscribe</strong>!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FdZTNdNNkED8%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DdZTNdNNkED8&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FdZTNdNNkED8%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c54de6b0d77d4934524ea70319f4a53/href">https://medium.com/media/3c54de6b0d77d4934524ea70319f4a53/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3b8554f6acfd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building reusable front-end toolkit at Tokopedia]]></title>
            <link>https://medium.com/tokopedia-engineering/building-reusable-front-end-toolkit-at-tokopedia-39656af41475?source=rss-128419355b17------2</link>
            <guid isPermaLink="false">https://medium.com/p/39656af41475</guid>
            <category><![CDATA[tokopedia]]></category>
            <category><![CDATA[toolkit]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Antony Budianto]]></dc:creator>
            <pubDate>Mon, 05 Nov 2018 02:03:06 GMT</pubDate>
            <atom:updated>2018-11-05T11:01:43.941Z</atom:updated>
            <content:encoded><![CDATA[<p>Hi everyone, today I’d like to share my experience about building front-end toolkit at <a href="https://www.tokopedia.com/">Tokopedia</a>.</p><h3>Background</h3><p>If you followed the latest trend on front-end technologies (especially <a href="https://reactjs.org/">React</a>), you might’ve heard about the famous toolkit: <a href="https://github.com/facebook/create-react-app">create-react-app</a>. So why it’s famous (53k+ ⭐️)? Here is its tagline:</p><pre>Create React apps with <strong>no build configuration</strong>.</pre><p>So, before <a href="https://github.com/facebook/create-react-app">create-react-app</a> existed, it’s been painful just to get started with <a href="https://reactjs.org/">React</a> development. This applies to any front-end library/frameworks without official CLI support. One might build a starter/boilerplate from scratch and some will just clone existing popular starters.</p><p>After having the starter, the next problem arises:</p><blockquote><em>“How do I get update from the upstream? I want that new shiny feature!”</em></blockquote><blockquote>“I have many apps, how to keep them updated?”</blockquote><p>You will have a hard time keeping your <strong>starter-based app</strong> to be up-to-date with the upstream starter, all the more if you have many apps. FYI, my team at <a href="https://www.tokopedia.com/">Tokopedia</a> have <strong>more than 10 (and counting) front-end applications </strong>and of course you need to keep maintaining them<strong>.</strong></p><blockquote>“So what is the better solution for this? “</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*j4yDz7nAhzP0nyEvohpoVA.png" /></figure><p>I recommend you to watch Dan Abramov’s <a href="https://www.youtube.com/watch?v=G39lKaONAlA">talk on Youtube</a>. So he’s one of React team at Facebook and he’s building <a href="https://github.com/facebook/create-react-app">create-react-app</a>. In his talk, he recommended us to build a <strong>reusable toolkit/toolboxes</strong>. So toolkit comes in form of CLI, it <strong>abstracts away the painful configuration</strong> so you can just focus on code and the project just need to contains your application code.</p><p>There are many zero config toolkit these days, like <a href="https://github.com/facebook/create-react-app">create-react-app</a>, <a href="https://github.com/antonybudianto/cra-universal">cra-universal</a> (mine 😂), <a href="https://jestjs.io/">Jest</a>, <a href="https://github.com/jaredpalmer/razzle">Razzle</a>, <a href="https://github.com/zeit/next.js/">Nextjs</a>, <a href="https://docusaurus.io/">Docusaurus</a>, <a href="https://webpack.js.org/">Webpack 4</a>, <a href="https://parceljs.org/">Parcel</a>, the list goes on and each with their own purposes.</p><p>In this case, we want a toolkit that’s able to start a dev server, build a bundle, testing, generate project, and other common dev tools.</p><p>By using a reusable toolkit, just bump your toolkit’s version in your package.json and your app is updated. Since it’s versioned, you can keep compatibility between apps without worry, and one improvement can be enjoyed by the rest of the apps.</p><p>So we already have <a href="https://github.com/facebook/create-react-app">create-react-app</a> (or other framework CLI), so why bother creating another toolkit?</p><ul><li>It may not suite your <strong>needs</strong></li><li>You already have <strong>existing</strong> legacy apps and find it hard to migrate them into third-party toolkit since you’ll face many incompatibilities. It’s better to just pull out their common config and build a toolkit for them, they’ll be compatible no matter what and you can migrate them <strong>incrementally</strong></li><li>You have <strong>opinionated</strong> and high <strong>customization</strong></li><li>Everyone have their own toolkit for their own <strong>purpose</strong>, example: <a href="https://github.com/kentcdodds/kcd-scripts">kcd-scripts</a>, paypal-scripts (<a href="https://blog.kentcdodds.com/automation-without-config-412ab5e47229">private toolkit by Kent C Dodds</a>)</li></ul><h3>Building the toolkit</h3><p>Initially, we have some legacy front-end apps, they are starter-based app. Since we’re growing so fast and keep launching new products, we lack agility to bootstrap a new front-end app, and worse, we’re having a hard time maintaining them because each app have their own tooling, and not standardized. Based in this problem, the toolkit development comes in mind.</p><p>So, we use <a href="https://medium.com/airbnb-engineering/isomorphic-javascript-the-future-of-web-apps-10882b7a2ebc">isomorphic</a> React starter in <a href="https://www.tokopedia.com/">Tokopedia</a>, initially the project consists of the following components:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*kI9ks3AYEdilExeg3trmzw.png" /><figcaption>starter-based app</figcaption></figure><p>The orange ones are considered unique per app, while the blue ones are considered similar per app. Let me explain the components:</p><ul><li>The client/server codes are simply your application code</li><li>The client/server bundler contains <a href="https://webpack.js.org/">Webpack</a>, <a href="https://babeljs.io/">Babel</a>, <a href="https://github.com/webpack-contrib/sass-loader">SASS</a>, and many other configuration. We use this to run local dev-server and build production-ready bundle</li><li>We also have <a href="https://expressjs.com/">Express</a> SSR server to do server-side rendering and its implementation is same across apps</li><li>Shared tools are chore tooling that are not related with bundling system, but useful to boost <a href="https://hackernoon.com/developer-experience-dx-devs-are-people-too-6590d6577afe">DX</a> (e.g.: automation), and you usually copy-and-paste them across apps. <br><strong>Example:</strong> <a href="https://github.com/isaacs/rimraf">rimraf</a>, <a href="https://eslint.org/">eslint</a>, <a href="https://storybook.js.org/">Storybook</a> config, <a href="https://github.com/GoogleChrome/puppeteer">puppeteer</a> config for E2E tests or pre-rendering</li></ul><p>The <strong>main idea</strong> is to move the blue ones into centralized space, the toolkit.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*4pf3hc8k15PFHzb6WkRCkA.png" /><figcaption>toolkit-based app</figcaption></figure><p>Now, it’s looking good! Your app now only contains your application code and depends on your toolkit as development dependency.</p><p>Actually it feels not worth the effort if you only have one app, but if you have many apps to maintain, you will greatly feel the benefits. The picture below is example how we can maintain multiple apps with different versioning:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*X_SEDdh4cSh9OuGNcGP9zA.png" /><figcaption>multiple toolkit-based apps with different versions</figcaption></figure><p>Toolkit is versioned so you can maintain compatibility and easy updates across your apps. When your product owner wanted to launch a new product, you can quickly generate a new project using the toolkit instead of cloning a starter again. This way you have more reusable, scalable, and maintainable front-end applications.</p><blockquote><strong>Note:</strong></blockquote><blockquote>- The generated project is also a starter, but it only contains your app code and it depends on your toolkit</blockquote><blockquote>- You may build multiple toolkit(s) instead of one, remember “S” of <a href="https://en.wikipedia.org/wiki/SOLID">SOLID</a> <em>😄</em></blockquote><h3>Our front-end toolkit tech stack:</h3><p>What did we use to build the toolkit? Here is our tech stack:</p><ul><li><a href="https://github.com/yargs/yargs">yargs</a> (for CLI engine)</li><li><a href="https://webpack.js.org/">Webpack</a> (for module bundler)</li><li><a href="https://lernajs.io/">Lerna</a> (for monorepo management)</li><li><a href="https://github.com/webpack-contrib/sass-loader">SASS</a> (for styling)</li></ul><blockquote>You can find out more at <a href="https://github.com/antonybudianto/react-kits">https://github.com/antonybudianto/react-kits</a>.</blockquote><blockquote>It’s based on <a href="https://github.com/antonybudianto/react-ssr-starter">this starter</a></blockquote><blockquote>P.S.: We have separate version for Tokopedia <em>😆</em></blockquote><p>I used <a href="https://webpack.js.org/api/node/">Webpack Node API</a> to watch and bundle the app via CLI script. So you might be wondering:</p><blockquote>If you put Webpack and its config on the toolkit, then it <strong>won’t work</strong> because Webpack will be resolving files on the toolkit directory, not the app one right?</blockquote><p>Yes, by default <a href="https://webpack.js.org/">Webpack</a> will resolve on the current directory, which in this case, your current directory is your toolkit directory, not your app directory even though you called the toolkit command on your app directory. It’s because <a href="https://webpack.js.org/">Webpack</a> is called by the script on your toolkit, not on your app directory.</p><p>The solution is to use <a href="https://webpack.js.org/configuration/entry-context/#context">context</a> option to make sure <a href="https://webpack.js.org/">Webpack</a> running on app working directory. In order to do this, you need to get the absolute path to your app working directory. Here’s how to do it:</p><pre>const path = require(&#39;path&#39;);<br>const cwd = process.cwd();<br>const pcwd = path.resolve(cwd);</pre><pre>___</pre><pre><br>{<br>  context: pcwd<br>}</pre><p>Okay, now <a href="https://webpack.js.org/">Webpack</a> finally able to resolve the entry file in your application code.</p><p>The next one is when trying to resolve <a href="https://webpack.js.org/">Webpack</a> loaders, you’ll get errors caused by missing loaders. You can solve this by adding the toolkit’s absolute path into resolveLoaders option:</p><pre>function resolveDir(...args) {<br>  return path.resolve(__dirname, ...args)<br>}</pre><pre>___</pre><pre><br>{<br>  resolveLoader: { <br>    modules: [resolveDir(&#39;../../node_modules&#39;), &#39;node_modules&#39;]<br>  },<br>}</pre><p>Now, your webpack should work correctly just like when it was in starter-form.</p><h3>Challenges</h3><p>I believe that there’s no silver bullet to everything, so here’s what we faced during development of the toolkit that’s worth to note:</p><ul><li>Determine the orange and blue on current apps. <br>Deciding whether some stuff should be put on the toolkit or app-side. There are things that are better be put at app-side like public path config, since we have different CDN for each app. Think carefully about this</li><li>Research many <a href="https://webpack.js.org/api/node/">Webpack API(s)</a> to enable code+config separation</li><li>Wrong abstraction can be more fatal than duplication</li><li>Allowing customization is actually tricky, you need to define what can be customized without exposing too much. Many customization on app side usually means you do a wrong abstraction</li><li>Managing <a href="https://nodejs.org/en/blog/npm/peer-dependencies/">Peer dependency</a>. <a href="https://reactjs.org/">React</a> and <a href="https://reactjs.org/docs/react-dom.html">React DOM</a> usually installed on app-side, not on toolkit-side. You must define whether some packages should be installed on toolkit or app side. <br><strong>Tips:</strong> Core framework like <a href="https://reactjs.org/">React</a>, <a href="https://reactjs.org/docs/react-dom.html">React DOM</a>, <a href="https://redux.js.org/">Redux</a>, <a href="https://graphql.org/">GraphQL</a>, usually installed on app side so they can be updated anytime. Having them installed on toolkit means you <strong>locked</strong> their versions to the toolkit version</li><li>Keeping the toolkit clean from over-bloated configuration</li><li>Keeping compatibility between new and legacy apps</li></ul><p>I think that’s it for my experience of developing front-end toolkit at <a href="https://www.tokopedia.com/">Tokopedia</a>. There is no silver bullet, our approach might not be the best but we already feel the benefit by doing this and will keep improving it. I hope you find it useful too.</p><p>If you have any ideas or questions, please post a response below, and if you like this article, please <strong>share</strong> with your friends and claps on! Thank you so much for reading.</p><p>P.S.: We’re hiring! If you’re curious and wanna be a part of amazing team, <a href="https://www.tokopedia.com/careers/">apply now</a>!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=39656af41475" width="1" height="1" alt=""><hr><p><a href="https://medium.com/tokopedia-engineering/building-reusable-front-end-toolkit-at-tokopedia-39656af41475">Building reusable front-end toolkit at Tokopedia</a> was originally published in <a href="https://medium.com/tokopedia-engineering">Tokopedia Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Tips for server-side rendering with React]]></title>
            <link>https://itnext.io/tips-for-server-side-rendering-with-react-e42b1b7acd57?source=rss-128419355b17------2</link>
            <guid isPermaLink="false">https://medium.com/p/e42b1b7acd57</guid>
            <category><![CDATA[server-side-rendering]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[tips]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Antony Budianto]]></dc:creator>
            <pubDate>Sat, 03 Nov 2018 09:57:37 GMT</pubDate>
            <atom:updated>2018-12-15T06:37:02.570Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*QvtSaHdquK5FBIFoJ0gc_A.jpeg" /></figure><p>Hi, today I’d like to share some tips when you’re developing server-side rendered application with <a href="https://reactjs.org/">React</a>.</p><h4>Web API usage</h4><p>Be careful when you use <a href="https://developer.mozilla.org/en-US/docs/Web/API">Web API</a>(s) since they won’t work on server-side. These are common Web API we usually use:</p><ul><li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model">DOM</a> (document)</li><li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Window">Window</a> (window, <a href="https://developer.mozilla.org/en-US/docs/Web/API/Navigator">navigator</a>, etc)</li><li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Storage">Storage</a> (localStorage, sessionStorage, etc)</li></ul><p>Avoid using them on these lifecycles since they’re called during server render:</p><ul><li>constructor</li><li><a href="https://reactjs.org/docs/react-component.html#static-getderivedstatefromprops">getDerivedStateFromProps</a></li><li><a href="https://reactjs.org/docs/react-component.html#unsafe_componentwillmount">componentWillMount</a> (deprecated)</li></ul><p>You can do it safely in <a href="https://reactjs.org/docs/react-component.html#componentdidmount">componentDidMount</a> and <a href="https://reactjs.org/docs/react-component.html#componentdidupdate">componentDidUpdate</a> lifecycle and any class methods that run after componentDidMount phase.</p><blockquote>I assume we use either <a href="https://reactjs.org/docs/react-dom-server.html#rendertostring">renderToString</a> and <a href="https://reactjs.org/docs/react-dom-server.html#rendertonodestream">renderToNodeStream</a> API for server-side rendering. <a href="https://prerender.io/">Pre-rendering libraries</a> don’t count as doing SSR since client-only lifecycles still get executed</blockquote><p>So, you might be thinking:</p><blockquote>“But can I just check if window is available?”</blockquote><p>Well, it depends, but <strong>don’t</strong> do this!</p><pre>constructor(props) {<br>  super(props)<br>  this.state = {<br>    data: typeof window !== &#39;undefined&#39; ? &#39;client&#39; : &#39;server&#39;<br>  }<br>}</pre><pre>render()<br>  return &lt;div&gt;{this.state.data}&lt;/div&gt;<br>}</pre><p>You will get the following warning on your browser console:</p><pre>index.js:2178 Warning: Text content did not match. Server: &quot;server&quot; Client: &quot;client&quot;</pre><p>So what’s wrong here? It’s because React expects the one rendered on the <strong>server</strong> should be the <strong>same</strong> with one rendered on <strong>client</strong> during hydration. It’s okay to use window type-checking before doing DOM access/operations but make sure it <strong>won’t affect rendering</strong> difference between client and server.</p><p>Okay, now you might be asking:</p><blockquote>“But I need to render based on window/browser APIs…”</blockquote><p>If you have some cases where you need to render based on DOM objects or Web APIs, then you need to render them after componentDidMount lifecycle, and on the server, you can render the placeholder (loading component, or just null)</p><pre>constructor(props) {<br>  super(props)<br>  this.state = {<br>    ssrDone: false<br>  }<br>}</pre><pre>componentDidMount() {<br>  this.setState({ ssrDone: true, online: navigator.onLine })<br>}</pre><pre>render() {<br>  if(!this.state.ssrDone) {<br>    return (<br>      &lt;div&gt;loading...&lt;/div&gt;<br>    )<br>  }<br>  return &lt;div&gt;{this.state.online ? &#39;on&#39; : &#39;off&#39;}&lt;/div&gt;<br>}</pre><p>This is especially useful when you use non-SSR friendly 3rd party React components (which will throw errors during server render). The <a href="https://reactjs.org/docs/react-dom.html#hydrate">official React doc</a> also shows the same technique (the name is “two-pass rendering”), but it also states:</p><blockquote>“Note that this approach will make your components slower because they have to render twice, so use it with caution.”</blockquote><p>Make sure you <strong>encapsulate</strong> these DOM-dependent components so the double rendering will only happen to them, not the whole page.</p><h4>Hydration warning</h4><p>If you got some hydration warnings and want to ignore them for some reason, you can use <a href="https://reactjs.org/docs/dom-elements.html#suppresshydrationwarning">suppressHydrationWarning</a> prop on the element.</p><h4>Bonus</h4><p>Using ReactDOM.render() to hydrate a server-rendered container is deprecated and will be <strong>removed</strong> in React 17. Use <a href="https://reactjs.org/docs/react-dom.html#hydrate">hydrate()</a> instead. (From <a href="https://reactjs.org/docs/react-dom.html">React official doc</a>)</p><p>I think that’s all from me, I hope it’s useful for you guys.</p><p>If you have any questions or ideas, please post a reply, and if you like it, please <strong>clap and share</strong> with your friends!</p><p>Thank you.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e42b1b7acd57" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/tips-for-server-side-rendering-with-react-e42b1b7acd57">Tips for server-side rendering with React</a> was originally published in <a href="https://itnext.io/">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[New cra-universal released ]]></title>
            <link>https://medium.com/@antonybudianto/new-cra-universal-3-0-0-released-37ceb19d31fb?source=rss-128419355b17------2</link>
            <guid isPermaLink="false">https://medium.com/p/37ceb19d31fb</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[server-side-rendering]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[cli]]></category>
            <category><![CDATA[universal]]></category>
            <dc:creator><![CDATA[Antony Budianto]]></dc:creator>
            <pubDate>Sat, 30 Jun 2018 11:54:44 GMT</pubDate>
            <atom:updated>2018-08-18T00:59:17.921Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/704/1*VK508QZOHvxzoA0uk1tzPw.png" /></figure><p>Hi, I’m very happy to announce that the latest major version of <a href="https://github.com/antonybudianto/cra-universal">cra-universal</a> is released! 🎉🎉🎉</p><h4>Introduction</h4><p>First heard about <a href="https://github.com/antonybudianto/cra-universal">cra-universal</a>? So it’s a CLI for your <a href="https://github.com/facebook/create-react-app">create-react-app</a> to enable universal app features like server-side rendering, without <a href="https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/template/README.md#alternatives-to-ejecting">ejecting</a> at all.</p><p>This release includes many improvements and new features:</p><ul><li><strong>Zero config</strong> by default, with customization support!</li><li><a href="https://webpack.js.org/concepts/hot-module-replacement/"><strong>HMR</strong></a> support on server!</li><li>Isomorphic style support, check <a href="https://github.com/kriasoft/isomorphic-style-loader">here</a></li><li>Basic assets support (png, jpg, gif, bmp, svg)</li></ul><p>With zero config, it means:</p><ul><li>One dependency for tooling, easy updates!</li><li>Customizable</li><li>No need maintain server/webpack.config.js anymore</li><li>No need to maintain server/package.json anymore, all server dependencies can be listed on root package.json</li></ul><h4>Get started</h4><p>You can bootstrap universal <a href="https://github.com/facebook/create-react-app">create-react-app</a> with these steps</p><pre># Create new cra<br>$ create-react-app myapp<br>$ cd myapp<br><br># Install new cra-universal<br>$ yarn add -D cra-universal<br><br># Start with no config at all!<br>$ npx cra-universal start</pre><pre># Start your CRA client<br>$ npm start</pre><p>Cool right? Unlike previous version, you don’t need to have server folder anymore, just start right away!</p><h4>Customization</h4><p><strong>Zero config</strong> doesn’t mean you cannot customize it at all, it just means it abstracts away the painful setup and you can still override as you need.</p><p>If you prefer to maintain the server like in previous version, it’s supported too. You can still keep your server folder, or to create a new one, you can run:</p><pre>$ npx cra-universal init</pre><p>It will bootstrap the server folder inside your <a href="https://github.com/facebook/create-react-app">CRA</a> client app, it contains server/index.js that handles the listen and log stuff, and server/app.js that handles the app server code. You can also have server/.babelrc file, it’ll use default babel configuration if not provided.</p><p>You can also define crau.config.js to override webpack configuration:</p><pre>// crau.config.js<br>module.exports = {</pre><pre>  webpackPlugins: [],</pre><pre>  modifyWebpack: config =&gt; config</pre><pre>}</pre><p>For now, it’ll only expose webpack and plugins overrides, but I think it should be enough for people to customize as their needs.</p><p>Please check out the <a href="https://github.com/antonybudianto/cra-universal/releases/tag/v3.0.0">release note here</a> for more details. You can also visit <a href="https://cra-universal.now.sh/">https://cra-universal.now.sh/</a> for official landing and demo, or my personal website at <a href="https://antonybudianto.com/">https://antonybudianto.com</a>.</p><p>If you’ve any feedbacks or ideas, please post a reply! I’d like to hear from you.</p><p>If you like the article, please share it with your friends, give some claps and star the <a href="https://github.com/antonybudianto/cra-universal">repo</a> on GitHub.</p><p>Thank you for reading!</p><h4>Related article:</h4><p><a href="https://medium.com/@antonybudianto/cra-universal-universal-create-react-app-without-ejecting-f50d4d179261">CRA Universal: Universal create-react-app without ejecting</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=37ceb19d31fb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building React library using react-lib-scripts]]></title>
            <link>https://itnext.io/building-react-library-using-react-lib-scripts-eab6f0fd21f2?source=rss-128419355b17------2</link>
            <guid isPermaLink="false">https://medium.com/p/eab6f0fd21f2</guid>
            <category><![CDATA[os]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[libraries]]></category>
            <category><![CDATA[cli]]></category>
            <dc:creator><![CDATA[Antony Budianto]]></dc:creator>
            <pubDate>Tue, 19 Jun 2018 02:27:43 GMT</pubDate>
            <atom:updated>2018-06-24T05:19:52.609Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*yDNSy8SRSyedGig3lD8KOA.jpeg" /><figcaption>react-lib-scripts</figcaption></figure><p>Hi, today I’d like to share my latest open source project for building <a href="https://reactjs.org/">React</a> library, <a href="https://github.com/antonybudianto/react-lib-scripts">react-lib-scripts</a>.</p><p>It’s a CLI that will help starting development and bundling library, so you can <strong>focus on your solution, not the tooling</strong>. It’s inspired from <a href="https://github.com/facebook/create-react-app/tree/next/packages/react-scripts">react-scripts</a> used by <a href="https://github.com/facebook/create-react-app">create-react-app</a> which helps React application development.</p><p>The CLI itself is made using <a href="https://oclif.io/">OCLIF by Heroku</a>, and the bundler is made using <a href="https://webpack.js.org/">Webpack 4</a>. It can also bundle your library styles and images.</p><p>Overall, <a href="https://github.com/antonybudianto/react-lib-scripts">react-lib-scripts</a> only has 2 commands:</p><ul><li>start (starting development flow. This will bundle and watch your library source)</li><li>build (prepare production ready bundle for publishing)</li></ul><h3>Get started</h3><p>You can clone the starter repo here:</p><pre><a href="https://github.com/antonybudianto/react-lib-starter">https://github.com/antonybudianto/react-lib-starter</a></pre><p>This is a starter that demonstrates <a href="https://github.com/antonybudianto/react-lib-scripts">react-lib-scripts</a> usage and also a mono-repo setup using <a href="https://lernajs.io/">Lerna</a>. You can find <strong>example-app</strong> and <strong>example-lib</strong> inside the <strong>packages</strong> folder.</p><blockquote>You can use <a href="https://github.com/antonybudianto/react-lib-scripts">react-lib-scripts</a> without <a href="https://github.com/antonybudianto/react-lib-starter">react-lib-starter</a>/<a href="https://lernajs.io/">Lerna</a> since they only provide a nice base for development library</blockquote><p>Let’s examine:</p><ul><li>example-app (plain create-react-app, uses example-lib)</li><li>example-lib (our React library, uses <a href="https://github.com/antonybudianto/react-lib-scripts">react-lib-scripts</a>)</li></ul><p>Let’s make sure you’re on the root of the starter, and install dependencies:</p><pre>npm install</pre><pre># for yarn user<br>yarn</pre><p>One advantage using <a href="https://lernajs.io/">Lerna</a> is that you can bootstrap and link your packages dependencies with only one command. Let’s bootstrap it!</p><pre>npm run bootstrap</pre><pre># for yarn user<br>yarn bootstrap</pre><p>It actually runs lerna bootstrap command, and for convenience, I put it as npm script on the starter. This command will bootstrap all your packages (including npm-installing, and linking), now you don’t have to do it manually for each packages.</p><h3>Development flow</h3><p>After installing stuff, we’re ready for the development flow.</p><p>You can change directory to <strong>example-lib</strong> and run:</p><pre>npm start</pre><pre># for yarn user<br>yarn start</pre><p>Your library is now ready to be consumed by <strong>example-app</strong>!</p><p>npm start script is an alias to react-lib-scripts start</p><blockquote><strong>Important:</strong> You need to provide <strong>NODE_ENV</strong> environment variable before start or build command.</blockquote><blockquote>It’s already provided on react-lib-starter via <a href="https://github.com/antonybudianto/react-lib-starter/blob/master/packages/example-lib/package.json#L14">better-npm-run</a></blockquote><p>Now, you can change directory to <strong>example-app</strong> and run:</p><pre>npm start</pre><pre># for yarn user<br>yarn start</pre><p>You can start library development now. While changing <strong>example-lib</strong> code, the changes will be updated on <strong>example-app</strong> too.</p><h3>Production flow</h3><p>Okay, so your library is done and ready for publishing.</p><p>You can build your production-ready bundle by running:</p><pre>npm run build</pre><p>npm run build script is an alias to react-lib-scripts build</p><p>Now your bundle is ready on <strong>lib</strong> folder.</p><p>Next, you must set the following fields on your <strong>example-lib</strong>’s package.json (only <strong>once</strong>):</p><pre>&quot;main&quot;: &quot;lib/index.js&quot;,<br>&quot;files&quot;: [<br>  &quot;lib&quot;,<br>  ...<br>],</pre><p>Also, bump your “version” field too every pre-publish.</p><p>Okay, let’s publish it.</p><pre>npm publish</pre><p>Now your library is live!</p><blockquote>react-lib-starter also has npm run release (alias to lerna publish) which can handle dependencies version update, version bump chooser, git-tagging, and npm-publishing automatically.</blockquote><h3>FAQ</h3><ol><li>Why no test command? Because I thought <a href="https://facebook.github.io/jest/">Jest</a> is already simple enough to use, there’s almost nothing to be abstracted away. Unlike before Jest comes in, it’s a trouble configuring test runner, framework, coverage and stuffs. Now it’s easy as running jest :)</li><li><a href="https://rollupjs.org/guide/en">Rollup</a>? For now, no, because there’s <a href="https://github.com/rollup/rollup/issues/1666">watch issue</a> when using their Node API. I might revisit it after it’s solved.</li><li>Can I add <a href="https://babeljs.io/">babel</a> plugin/presets or <a href="https://github.com/postcss/postcss">postcss</a> plugins? Yes! Check <a href="https://github.com/antonybudianto/react-lib-scripts#customize">here</a></li></ol><p>The <a href="https://github.com/antonybudianto/react-lib-scripts">react-lib-scripts</a> project is still new, and needs improvements, so your <strong>feedbacks and contributions</strong> will be VERY helpful for the project.</p><p>I hope the story will be helpful for all of you. Please post a response for any feedbacks/ideas/questions, and kindly <strong>share</strong> this to your friends who might need it, and finally, claps on please! 👏👏👏</p><p>Thanks for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eab6f0fd21f2" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/building-react-library-using-react-lib-scripts-eab6f0fd21f2">Building React library using react-lib-scripts</a> was originally published in <a href="https://itnext.io/">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Managing packages with LernaJS]]></title>
            <link>https://medium.com/@antonybudianto/managing-packages-with-lernajs-a15aaa786366?source=rss-128419355b17------2</link>
            <guid isPermaLink="false">https://medium.com/p/a15aaa786366</guid>
            <category><![CDATA[lerna]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Antony Budianto]]></dc:creator>
            <pubDate>Sun, 13 May 2018 05:24:07 GMT</pubDate>
            <atom:updated>2018-12-21T11:53:34.604Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/612/1*GS0ohg3qm14E4Ya7fljicw.jpeg" /><figcaption>Multiple packages? Lerna comes to rescue!</figcaption></figure><p>Hi, it’s been a while since my last post. Today I’d like to share my experience using <a href="https://lernajs.io/">LernaJS</a> to maintain <a href="https://github.com/antonybudianto/create-react-app-express">create-react-app-express</a>.</p><p>So what is LernaJS?</p><blockquote>A tool for managing JavaScript projects with multiple packages.</blockquote><p>So basically, Lerna is a tool that can help you with <strong>bootstraping</strong>, <strong>building</strong>, <strong>cleaning</strong>, and <strong>releasing</strong> your packages, and yes, it can do more than that.</p><p>I started <a href="https://github.com/antonybudianto/create-react-app-express">create-react-app-express</a> as a single package, and then grows into multiple packages (now it’s <a href="https://github.com/antonybudianto/cra-universal">cra-universal</a>). At first, I do it manually, like npm linking, building, cleaning, and releasing. I realize that some commands are actually similar between packages, like cleaning, running test, build. What if the tasks can be defined once in parent and can be run for all the packages? Lerna comes to rescue!</p><h3>Install Lerna</h3><p>Let’s install Lerna CLI:</p><pre># Using yarn<br>yarn global add lerna</pre><pre># or npm<br>npm i -g lerna</pre><p>You can initialize Lerna config by running:</p><pre>lerna init</pre><p>You’ll get <strong>lerna.json</strong> file and packages/ folder in your project folder.</p><pre>lerna-repo/<br>  packages/<br>  package.json<br>  lerna.json</pre><p>You can now start creating your packages inside the packages/ folder.</p><h3>Lerna usual commands</h3><p>Now you can open your root package.json (lerna-repo/package.json) and insert some lerna commands:</p><pre>&quot;scripts&quot;: {</pre><pre>  &quot;bootstrap&quot;: &quot;lerna bootstrap&quot;,</pre><pre>  &quot;test&quot;: &quot;lerna run test:ci&quot;,</pre><pre>  &quot;clean&quot;: &quot;lerna clean&quot;,</pre><pre>  &quot;clean:build&quot;: &quot;lerna exec -- rimraf lib&quot;,</pre><pre>  &quot;release&quot;: &quot;lerna publish&quot;,</pre><pre>  &quot;ls&quot;: &quot;lerna ls&quot;</pre><pre>},</pre><p>You can use <strong>bootstrap</strong> for installing your packages dependencies and create symlinks. Suppose you have package core and server , and server depends on core , Lerna will symlink the package instead of installing the real one on registry.</p><p>You can use <strong>run</strong> for running specific npm script on your packages, and will skip ones that don’t have the command. So if you have “test:ci” script on package A, B, but not C, then it’ll only run on package A and B. The cool thing is, you just need to install <a href="https://facebook.github.io/jest/"><strong>Jest</strong></a> on the root package and it can be used on all the packages. You can also add --scope argument if you only want to run on some packages.</p><p>You can use <strong>exec</strong> for running a shell command on all your packages, similar with run command, but you don’t need to define the script on all packages! As you see in the script snippet above, we can install rimraf on the root, and define the clean:build command and it’ll run on all the packages.</p><p>You can use <strong>clean</strong> for removing node_modules on your packages.</p><p>You can use <strong>publish</strong> for publishing all your packages, including git tagging, and selecting semantic versioning update, and publishing to npm registry.</p><blockquote>When publishing, Lerna will also update the package version on the package that depends on it. So if package A got a new version, let’s say 1.1.0, and package B depends on it as “^1.0.0”, it will be updated as “^1.1.0” automatically.</blockquote><p>You can use <strong>ls</strong> for listing all your packages and their current version.</p><h4>Bonus</h4><p>Lerna <strong>bootstrap</strong> also rearranges your <strong>package.json</strong> dependencies order so you don’t need to do so manually 😄</p><h3>My takeaway</h3><p>Personally I like the <strong>run</strong> and <strong>exec</strong> command, now you can have scripts that can be reusable for all the packages (e.g. build, testing, cleaning, linting), and put its definition and dependencies on the root package while you can have normal script that belongs to specific packages (e.g. build step that’s different for each package).</p><p>You can take a look at my root scripts <a href="https://github.com/antonybudianto/create-react-app-express/blob/master/package.json">here</a> for example.</p><p>I think that’s all from me, if you’ve another good tips using <a href="https://github.com/lerna/lerna/">Lerna</a>, please comment below!</p><p>Thank you for reading.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a15aaa786366" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[CRA Universal: Universal create-react-app without ejecting]]></title>
            <link>https://medium.com/@antonybudianto/cra-universal-universal-create-react-app-without-ejecting-f50d4d179261?source=rss-128419355b17------2</link>
            <guid isPermaLink="false">https://medium.com/p/f50d4d179261</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[create-react-app]]></category>
            <category><![CDATA[code-splitting]]></category>
            <category><![CDATA[ssrs]]></category>
            <category><![CDATA[universal]]></category>
            <dc:creator><![CDATA[Antony Budianto]]></dc:creator>
            <pubDate>Sun, 10 Dec 2017 04:11:06 GMT</pubDate>
            <atom:updated>2018-12-21T07:36:21.940Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><strong>Note:</strong> Content updated for latest cra-universal 4.0.0 release</blockquote><p>Hello, today I’d like to share about my latest work, <a href="https://github.com/antonybudianto/cra-universal">CRA Universal</a>.</p><p>It’s a CLI to initialize universal <a href="https://github.com/facebookincubator/create-react-app">create-react-app</a>, so you’ll get server-side rendering that works with code-splitting, and yes, without ejecting your precious create-react-app and start with zero config by default.</p><p>Let’s get started!</p><h3>Prerequisites:</h3><ul><li><a href="https://nodejs.org/en/">Nodejs</a> for SSR server</li><li>create-react-app project (of course!)</li></ul><p>Initialize your new create-react-app and change your directory to the newly created app:</p><pre>npx create-react-app my-app<br>cd ./my-app</pre><p>Install cra-universal</p><pre>npm i -D cra-universal </pre><p>Finally, run the following commands with different terminal tabs:</p><pre># Start client<br>npm start </pre><pre># Start server<br>npx cra-universal start</pre><p>Next, you need to change your render method from ReactDOM.render into ReactDOM.hydrate in your <strong>index.html </strong>so that the hydration works properly.</p><p>You can visit <a href="http://localhost:3001/">http://localhost:3001</a> and your create-react-app with SSR is ready!</p><p>Now try to use curl to check whether you get the full rendered version or not</p><pre>curl http://localhost:3001</pre><p>Now, you may want to deploy your app, let’s build it by running:</p><pre>npx cra-universal build</pre><p>This will build both the client and server bundle and put them together into dist folder. You can deploy dist into your Nodejs server, like now.sh, Firebase functions, or even your own server instance.</p><p>If you want to clean up the build results, you can run:</p><pre>npx cra-universal clean</pre><blockquote><strong>Tips:</strong> You can store cra-universal commands into your npm scripts for your convenience 😄</blockquote><p>Here is the CLI repo if you want to contribute (it’s very welcomed! 😃)</p><p><a href="https://github.com/antonybudianto/cra-universal">antonybudianto/cra-universal</a></p><p>For demo, you can visit my website at <a href="https://antonybudianto.com/">https://antonybudianto.com</a> and <a href="https://cra-universal.now.sh/">https://cra-universal.now.sh</a></p><p>Thank you for reading, please share to your friends who might need this and claps in! 😄 👏</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f50d4d179261" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Server-side rendering with React and Firebase Functions]]></title>
            <link>https://medium.com/@antonybudianto/server-side-rendering-with-react-and-firebase-functions-cd67fdb2b605?source=rss-128419355b17------2</link>
            <guid isPermaLink="false">https://medium.com/p/cd67fdb2b605</guid>
            <category><![CDATA[server-side-rendering]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[firebase-cloud-functions]]></category>
            <category><![CDATA[universal-app]]></category>
            <category><![CDATA[firebase]]></category>
            <dc:creator><![CDATA[Antony Budianto]]></dc:creator>
            <pubDate>Sun, 20 Aug 2017 10:15:46 GMT</pubDate>
            <atom:updated>2020-07-30T09:21:19.357Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*LCU8zQ3Tw51MPlLtSCFyfw.jpeg" /><figcaption>React SSR with Firebase Cloud Functions</figcaption></figure><p>Hello, today I’d like to share how I enabled Server-side rendering with <a href="https://facebook.github.io/react/">React</a> and <a href="https://firebase.google.com/docs/functions/?utm_source=google&amp;utm_medium=cpc&amp;utm_campaign=1001467%20%7C%20Firebase%20%7C%20Global%20%7C%20en%20%7C%20Hybrid%20%7C%20Text%20%7C%20BKWS%20~%20Features%20%5B2017%5D*&amp;utm_term=%7Bkeyword%7D&amp;gclid=CjwKCAjwxJnNBRAMEiwA8X_-QRh053GRQc2oVqQG0SQ3o-kmj11LSL3zkRQP7x9cSjrCrzUFD-OUExoC47cQAvD_BwE">Firebase Functions</a> on my own personal website at <a href="https://antonybudianto.com/">https://antonybudianto.com</a>.</p><blockquote><strong>Update: </strong>Content is updated for the latest cra-universal 3.0.0 release, read it <a href="https://medium.com/@antonybudianto/new-cra-universal-3-0-0-released-37ceb19d31fb">here</a>!.</blockquote><h4>Server-side rendering and Code-splitting with React</h4><p>So firstly I’ve the following tech stacks:</p><ul><li><a href="https://github.com/facebookincubator/create-react-app">Create React App</a></li><li><a href="https://firebase.google.com/">Firebase</a></li></ul><p>Actually I’ve been reading <a href="https://medium.com/@benlu/ssr-with-create-react-app-v2-1b8b520681d9">article</a> on how to do server-side rendering (SSR) with <em>create-react-app</em> without ejecting it, and it’s great.</p><p>After reading the article, I decided to fork the repo to add some customization like <a href="https://webpack.js.org/">Webpack</a> for production build, code-splitting, and separate client and server packages installation, so that it’s portable and reusable for other existing projects.</p><p>Well, turned out it worked well. Now you can copy./server into your <em>create-react-app</em> project and it’s done! Now you have a server-side rendering server with Code-splitting support. I also made the <a href="https://github.com/antonybudianto/cra-universal">cra-universal </a>out of it to ease the copying stuff and build task.</p><p>For starter, you can try:</p><pre>npx create-react-app my-app<br>cd my-app<br>npm i -D cra-universal<br>npx cra-universal start --both</pre><p>Open http://localhost:3001 in your browser and your create-react-app with SSR is ready!</p><h4>Firebase functions</h4><p>Now, we have SSR-ready server, but since <a href="https://firebase.google.com/docs/hosting/">Firebase hosting</a> can only host static assets, you can’t really start a node server there.</p><p>I looked up on how to do SSR with Firebase, and then I found this cool repo:</p><p><a href="https://github.com/firebase/functions-samples/tree/master/isomorphic-react-app">https://github.com/firebase/functions-samples/tree/master/isomorphic-react-app</a></p><p>So actually you can host your server with Firebase functions and <strong>it’s free</strong>! Unless you do outbound networking (Google services are free) within your function.</p><p>Firstly you need to change the rewrite on your <strong>firebase.json</strong>:</p><p>Initial:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/edff0e947b930899a84d669da0a898c6/href">https://medium.com/media/edff0e947b930899a84d669da0a898c6/href</a></iframe><p>Change the “destination” key to “function”.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/19531002086ea5ba8fcf4fa347514ba3/href">https://medium.com/media/19531002086ea5ba8fcf4fa347514ba3/href</a></iframe><p>“app” is the export name from ./functions/index.js</p><p>Now you need to build the bundle for client and server.</p><p>Run the following command:</p><pre>npx cra-universal build</pre><p>This command will build both your client and server and produces ./dist folder.</p><p>Don’t forget to remove ./build/index.html from client build, because it’ll make server-side rendering not working on root path.</p><p>Now create crau-dist folder inside ./functions then copy ./dist contents into ./functions/crau-dist.</p><p>Here is my function index.js:</p><pre><em>const</em> functions = require(&#39;firebase-functions&#39;);</pre><pre><em>const</em> app = require(&#39;./crau-dist/server/bundle&#39;);</pre><pre>exports.app = functions.https.onRequest(app);</pre><p>So you need to grab the server bundle and then pass app (which is an <a href="https://expressjs.com/">express</a> instance) into Firebase function.</p><p>Finally, you run firebase deploy to deploy all your Firebase database, hosting, and functions.</p><p>To test it, you can use curl https://your-domain.com and check whether your app container (&lt;div id=”root”&gt;) is already populated with pre-rendered markup from server.</p><p>For the complete code, you can find it here:</p><p><a href="https://github.com/antonybudianto/antonybudianto-web-old">https://github.com/antonybudianto/antonybudianto-web-old</a></p><p>Current challenges:</p><ul><li>Need to copy the server bundle and client index.html (needed for SSR) into the functions folder manually (for now I used npm script)</li><li>Firebase function is still in Beta, it might not be stable and the API may change in the future</li></ul><p>Feel free to ask or discuss. Thank you for reading!</p><h4>References:</h4><ul><li><a href="https://github.com/antonybudianto/cra-universal/">antonybudianto/cra-universal</a></li><li><a href="https://medium.com/@benlu/ssr-with-create-react-app-v2-1b8b520681d9">SSR with Create React App v2</a></li><li><a href="https://firebase.google.com/docs/functions/">Cloud Functions for Firebase | Firebase</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cd67fdb2b605" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Testing React Router apps with Jest and Enzyme]]></title>
            <link>https://medium.com/@antonybudianto/react-router-testing-with-jest-and-enzyme-17294fefd303?source=rss-128419355b17------2</link>
            <guid isPermaLink="false">https://medium.com/p/17294fefd303</guid>
            <category><![CDATA[jest]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[enzyme]]></category>
            <category><![CDATA[react-router]]></category>
            <dc:creator><![CDATA[Antony Budianto]]></dc:creator>
            <pubDate>Fri, 11 Aug 2017 13:40:15 GMT</pubDate>
            <atom:updated>2019-02-07T01:48:45.912Z</atom:updated>
            <content:encoded><![CDATA[<p>I would like to share with you on how I test my React application that used React Router.</p><p>Test case:</p><p>Suppose you have following route configuration on App.js:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/192c2556616fa0e5304c0f8e0b927f04/href">https://medium.com/media/192c2556616fa0e5304c0f8e0b927f04/href</a></iframe><p>I would like to test that if I visit some random unknown path, then my App should render <em>NotFoundPage</em> component, or else, it should render the correct component.</p><p>So you can make the app to go to specific route by using <em>MemoryRouter</em> provided by react-router.</p><p>We also need to check the deep rendered component by the router so we must use <em>mount()</em> from <a href="https://github.com/airbnb/enzyme">enzyme</a>.</p><pre>test(&#39;invalid path should redirect to 404&#39;, () <em>=&gt;</em> {</pre><pre><em>  const</em> wrapper = mount(</pre><pre>    &lt;MemoryRouter <strong>initialEntries</strong>={[ &#39;/random&#39; ]}&gt;</pre><pre>      &lt;App/&gt;</pre><pre>    &lt;/MemoryRouter&gt;</pre><pre>  );</pre><pre>  expect(wrapper.find(LandingPage)).toHaveLength(0);</pre><pre>  expect(wrapper.find(NotFoundPage)).toHaveLength(1);</pre><pre>});</pre><p><em>MemoryRouter</em> have <strong>initialEntries</strong> prop to custom your initial route path.</p><p>Don’t forget to mock the <em>BrowserRouter</em> used by App.js by creating __mocks__/react-router-dom.js and here is how to mock it:</p><pre>import React from &#39;react&#39;;</pre><pre><em>const</em> rrd = require(&#39;react-router-dom&#39;);</pre><pre>// Just render plain div with its children<br>rrd.BrowserRouter = ({<em>children</em>}) <em>=&gt;</em> &lt;div&gt;{children}&lt;/div&gt;</pre><pre>module.exports = rrd;</pre><p>We need to mock it because it’ll overlap with our <em>MemoryRouter</em> and causing the initialEntries not working.</p><p>Now try to run your test by running npm test and it should pass correctly.</p><p>Here is my full test:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fc8565a4b1ff6b90b4e5236d1bae10b2/href">https://medium.com/media/fc8565a4b1ff6b90b4e5236d1bae10b2/href</a></iframe><p>You can check the repo <a href="https://github.com/antonybudianto/antonybudianto-web">here</a> too. (coming soon)</p><p>Stack used:</p><ul><li>Create React App</li><li>React Router 4.1.2</li><li>Jest</li><li>Enzyme</li></ul><p>If you have any questions or suggestions, feel free to ask.</p><p>Thank you for reading!</p><p>P.S. I just started my <a href="https://www.youtube.com/channel/UCHgFuZ220_FDdxFL7OSay4A">new YouTube channel</a>, and I’ve posted “Learn React testing in 2 minutes”, <strong>please watch, like, share, and subscribe</strong>!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FdZTNdNNkED8%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DdZTNdNNkED8&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FdZTNdNNkED8%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c54de6b0d77d4934524ea70319f4a53/href">https://medium.com/media/3c54de6b0d77d4934524ea70319f4a53/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=17294fefd303" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Automate your create-react-app build on Heroku using Buildpack]]></title>
            <link>https://medium.com/@antonybudianto/automate-your-create-react-app-build-on-heroku-using-buildpack-beb77408ffee?source=rss-128419355b17------2</link>
            <guid isPermaLink="false">https://medium.com/p/beb77408ffee</guid>
            <category><![CDATA[heroku]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[automation]]></category>
            <category><![CDATA[buildpack]]></category>
            <category><![CDATA[reactjs]]></category>
            <dc:creator><![CDATA[Antony Budianto]]></dc:creator>
            <pubDate>Tue, 11 Jul 2017 02:50:34 GMT</pubDate>
            <atom:updated>2017-07-11T03:10:19.725Z</atom:updated>
            <content:encoded><![CDATA[<p>Hi,</p><p>Today I’d like to share how easy it’s to automate your <a href="https://github.com/facebookincubator/create-react-app">create-react-app</a> build and deploy on Heroku.</p><p>You may ever use Heroku CLI to do the build script, but now you can use <a href="https://github.com/mars/create-react-app-buildpack#quick-start">create-react-app buildpack</a> to automate your build process on Heroku. So you don’t need to script at all. Just follow its simple quickstart:</p><pre>create-react-app $APP_NAME<br>cd $APP_NAME<br>git init<br>heroku create $APP_NAME --buildpack https://github.com/mars/create-react-app-buildpack.git<br>git add .<br>git commit -m &quot;Start with create-react-app&quot;<br>git push heroku master<br>heroku open</pre><p>and that’s it! The next deploy will use the buildpack and the app is ready.</p><p>If you want to look how it works behind the scene:</p><pre>-----&gt; React.js (create-react-app) multi app detected</pre><pre>-----&gt; Configure create-react-app build environment</pre><pre>Using `NODE_ENV=development`</pre><pre>=====&gt; Downloading Buildpack: https://github.com/heroku/heroku-buildpack-multi.git</pre><pre>=====&gt; Detected Framework: Multipack</pre><pre>=====&gt; Downloading Buildpack: https://github.com/heroku/heroku-buildpack-nodejs.git</pre><pre>=====&gt; Detected Framework: Node.js</pre><pre>-----&gt; Creating runtime environment</pre><pre>NPM_CONFIG_LOGLEVEL=error</pre><pre>NPM_CONFIG_PRODUCTION=false</pre><pre>NODE_VERBOSE=false</pre><pre>NODE_ENV=development</pre><pre>NODE_MODULES_CACHE=true</pre><pre>-----&gt; Installing binaries</pre><pre>engines.node (package.json):  unspecified</pre><pre>engines.npm (package.json):   unspecified (use default)</pre><pre>engines.yarn (package.json):  unspecified (use default)</pre><pre>Resolving node version 6.x via semver.io...</pre><pre>Downloading and installing node 6.11.0...</pre><pre>Using default npm version: 3.10.10</pre><pre>Resolving yarn version (latest) via semver.io...</pre><pre>Downloading and installing yarn (0.27.5)...</pre><pre>Installed yarn 0.27.5</pre><pre>-----&gt; Restoring cache</pre><pre>Skipping cache restore (new-signature)</pre><pre>-----&gt; Building dependencies</pre><pre>Installing node modules (yarn.lock)</pre><pre>yarn install v0.27.5</pre><pre>[1/4] Resolving packages...</pre><pre>[2/4] Fetching packages...</pre><pre>warning fsevents@1.0.17: The platform &quot;linux&quot; is incompatible with this module.</pre><pre>info &quot;fsevents@1.0.17&quot; is an optional dependency and failed compatibility check. Excluding it from installation.</pre><pre>[3/4] Linking dependencies...</pre><pre>[4/4] Building fresh packages...</pre><pre>Done in 24.62s.</pre><pre>-----&gt; Caching build</pre><pre>Clearing previous node cache</pre><pre>Saving 2 cacheDirectories (default):</pre><pre>- node_modules</pre><pre>- bower_components (nothing to cache)</pre><pre>-----&gt; Build succeeded!</pre><pre>=====&gt; Downloading Buildpack: https://github.com/mars/create-react-app-inner-buildpack.git</pre><pre>=====&gt; Detected Framework: React.js (create-react-app)</pre><pre>Using existing `static.json`</pre><pre>Enabling runtime environment variables</pre><pre>&gt; gitfun-web@0.1.0 build /tmp/build_68262707387f88c5d8887155e7154921/antonybudianto-gitfun-web-b656dc6</pre><pre>&gt; react-scripts build</pre><pre>Creating an optimized production build...</pre><pre>Compiled successfully.</pre><pre>File sizes after gzip:</pre><pre>60.11 KB  build/static/js/main.03c59a74.js</pre><pre>457 B     build/static/css/main.167ae284.css</pre><pre>The project was built assuming it is hosted at the server root.</pre><pre>To override this, specify the homepage in your package.json.</pre><pre>For example, add this to build it for GitHub Pages:</pre><pre>&quot;homepage&quot;: &quot;http://myname.github.io/myapp&quot;,</pre><pre>The build folder is ready to be deployed.</pre><pre>You may serve it with a static server:</pre><pre>yarn global add serve</pre><pre>serve -s build</pre><pre>=====&gt; Downloading Buildpack: https://github.com/heroku/heroku-buildpack-static.git</pre><pre>=====&gt; Detected Framework: Static HTML</pre><pre>% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current</pre><pre>Dload  Upload   Total   Spent    Left  Speed</pre><pre>-----&gt; Installed directory to /app/bin</pre><pre>Using release configuration from last framework (Static HTML).</pre><pre>-----&gt; Discovering process types</pre><pre>Procfile declares types     -&gt; (none)</pre><pre>Default types for buildpack -&gt; web</pre><pre>-----&gt; Compressing...</pre><pre>Done: 30.7M</pre><pre>-----&gt; Launching...</pre><pre>Released v17</pre><pre>https://gitfun-web.herokuapp.com/ deployed to Heroku</pre><blockquote>This works for super simple app. If you have custom/complicated build script, you may better still use Heroku CLI.</blockquote><p>Thanks for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=beb77408ffee" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>