We migrated [our home page](https://manifold.co) from a basic React boilerplate ([React Boilerplate](https://github.com/react-boilerplate/react-boilerplate)) to [Next.js](https://github.com/zeit/next.js/), a progressive web app framework for React. We made no other changes, and the switch was basically invisible. The load times were better than we expected—it was basically free performance:
<a href="https://gist.github.com/dangodev/9e8227edf4d387b66f1c73304f9a0105#file-manifold-co-next-js-md" class="embedly-card" data-card-width="100%" data-card-controls="0">Embedded content: https://gist.github.com/dangodev/9e8227edf4d387b66f1c73304f9a0105#file-manifold-co-next-js-md</a>
On a fast connection, fast CPU, our site load time went from 1.5 seconds to 0.2 seconds — 7.5× faster! On an average connection and device, our site load time went from 7.5 seconds down to 1.3 seconds.
## Free performance, you say?
To understand what a progressive web app (PWA) does, it’ll take a bit of understanding what happens in the time between a user visiting a URL (our website), and seeing something rendered on the page (in this case, our React app).
### The stages
1. Once a user hits a URL, the network request is a flat cost in resolving the DNS and requesting that path from the server. It’s pretty quick, usually happening in < 100 milliseconds, but it’s mentioned because it does take *some* time.
1. From there, the server returns the HTML for the page, but the page will stay blank while every `<script>` and `<link>` tag inside `<head>` is downloaded ([unless it’s marked async](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#Notes)). There’s more going on in this step than the diagram shows, but for our purposes we’ll lump these processes together.
1. With the HTML and critical assets loaded, the browser starts to paint what it can, while downloading the rest (images, etc.) in the background. Ever wonder why images sometimes “pop” in or sometimes take a while to load? This is why! This lets you look at pages sooner.
1. In the case of a React app, it has to now modify the DOM, which will trigger another browser paint after it’s all done, and another cycle of asset downloads. The time this will take will depend on the complexity of the page.
### Sooner is better!
Because a progressive web app takes React and spits out static HTML and CSS, that means** users are looking at your React app at Stage 3 rather than Stage 5.** Which, in our tests, took anywhere from **0.2–4 seconds **depending on your connection and device speed, compared to 1.5–22 seconds.
*…seriously—free performance? No catch?*
No catch! PWAs are a bulletproof way to serve your React app faster.
The reason why progressive web apps and PWA frameworks like Next.js are still catching on is because, traditionally, JS frameworks didn’t make it easy to generate static HTML. Today, the landscape is much different, with React, Vue, Angular, and the rest having first-class support for server-side-rendering. However, a deep understanding of bundlers and build tooling is still required to make it happen, and it’s not without its fair share of pain points. The recent appearance of PWA frameworks like Next.js and Gatsby (both late 2016/early 2017) have become a boon to PWA adoption by lowering the barrier to entry (and arguably making it *fun!*).
While not every application will fit into Next.js, for many React applications it boasts free performance and bandwidth boosts.
## How bad is the move?
All-in-all, our migration of [manifold.co](https://manifold.co) wasn’t too bad, but we did run into the following pain points because of our unique setup:
### 🏎 React Router gets left in the dust
React Router had to go, because Next.js has its own built-in router that communicates better with its code splitting optimizations—*on top* of the PWA architecture I just described—to load pages much faster than any client-side router could do on its own.
In practice, because we weren’t doing a lot of complicated routing that React Router offers, for us it was as simple as replacing React Router’s standard <Link /> component with Next.js’s:
<a href="https://gist.github.com/dangodev/d55337c3a87dbe41c7e0cd81f973aa14#file-manifold-co-next-router-js" class="embedly-card" data-card-width="100%" data-card-controls="0">Embedded content: https://gist.github.com/dangodev/d55337c3a87dbe41c7e0cd81f973aa14#file-manifold-co-next-router-js</a>
Overall, not too bad! We basically renamed a prop and added an `<a>` tag for SSR. Because we’re using Styled Components as well, I also found adding the `passHref` prop was necessary in most instances to make sure the href always made it to the generated `<a>` tag.
To see Next.js’ fancy router optimizations in action, pop open your **Network Tab on [manifold.co](https://manifold.co)** and click a link. In the image above, clicking a link to /services returns an XHR request for services.js rather than a normal 200 request for /services.
### ⬛️ Blackboxed Redux
Continuing from the previous point, all that fancy Next.js optimization and code-splitting comes at a price: because it handles code-splitting at the page level, it won’t let you access the root React component or React DOM’s render method. If you’ve set up Redux yourself before, you’ll notice this poses a problem: where does the Redux <Provider /> sit?
In Next.js, they made a special [withRedux HOC wrapper](https://github.com/zeit/next.js/tree/canary/examples/with-redux) you must wrap every top level component with, on every page:
export default withRedux(HomePage);
While that’s not too bad, if you need shimmed `createStore()` methods like in the case of [redux-reducer-injectors](https://github.com/GuillaumeCisco/redux-reducers-injector), plan on some extra time debugging their wrapper (sidenote: don’t use reducer injectors—ever).
Also because of its blackboxed nature, using [Immutable](https://facebook.github.io/immutable-js/) with Redux becomes problematic. Although Immutable is supposed to work with this, I ran into problems with either the top-level state not being immutable (“get is not a function”), or the wrapper trying to use JS object dot notation rather than .get() (“Can’t get catalog of undefined”). And I had to dive into source code to debug this. After all, Next makes you use their provider for a reason.
In general, this highlights a key problem with Next.js: **very little is documented**. They have [many examples to draw from](https://github.com/zeit/next.js/tree/canary/examples/), but if your usecase isn’t there, good luck!
### 🐶 Stop trying to make `fetch` happen
We were using [react-inlinesvg](https://github.com/gilbarbara/react-inlinesvg), which offers the stylability of inline SVGs and the cacheability of remote requests. Only problem is: there’s no such thing as an XHR request when server-side rendering (at least, not in the sense of webpack URLs like it was expecting), so this blew up trying to SSR.
While there are some other inline SVG libraries that support SSR, I decided to drop it since most our SVGs were one-off illustrations anyway. I either swapped them out to an `<img>` tag if it didn’t need styling, or embedded them as React JSX. This might be a win-win, as the JSX illustrations are now delivered in the initial page load, and the client bundle had 1 library removed.
If you do need fetch in general (I did end up needing it for another library), you can configure that via next.config.js using whatwg-fetch and node-fetch:
<a href="https://gist.github.com/dangodev/dea6347ae6415b82da47e5d811807401#file-manifold-co-node-fetch-js" class="embedly-card" data-card-width="100%" data-card-controls="0">Embedded content: https://gist.github.com/dangodev/dea6347ae6415b82da47e5d811807401#file-manifold-co-node-fetch-js</a>
### 👯 Client JS & server JS: the dynamic duo
The last confusing part of Next.js is it runs twice — once for server, and again for client. This blurs the lines a bit between client JS and Node JS in the same codebase, resulting in errors you’re not used to like fs is undefined when Node’s fs tool is trying to run in the client.
As a result, you’ll have to engineer shims like this within next.js.config:
<a href="https://gist.github.com/dangodev/329f2ccb3a5232b371087f54e250c5e1#file-manifold-co-next-webpack-js" class="embedly-card" data-card-width="100%" data-card-controls="0">Embedded content: https://gist.github.com/dangodev/329f2ccb3a5232b371087f54e250c5e1#file-manifold-co-next-webpack-js</a>
The config.isServer flag within webpack becomes your best friend when the same code has to run in multiple environments.
You also have Next.js’ propretary getInitialProps() lifecycle addition to React, which only runs in server-mode, as well:
<a href="https://gist.github.com/dangodev/31e84c32f91ecb6d7b57fbb63b0110aa#file-manifold-co-next-react-class-js" class="embedly-card" data-card-width="100%" data-card-controls="0">Embedded content: https://gist.github.com/dangodev/31e84c32f91ecb6d7b57fbb63b0110aa#file-manifold-co-next-react-class-js</a>
Oh, and let’s not forget about our good friend window, needed for event listening, window measurement, and a plethora of useful functions, which is Node.js’ greatest weakness:
<a href="https://gist.github.com/dangodev/de6e0d1c25fe18ab0a3b0b0b5d25d5a3#file-manifold-co-next-window-js" class="embedly-card" data-card-width="100%" data-card-controls="0">Embedded content: https://gist.github.com/dangodev/de6e0d1c25fe18ab0a3b0b0b5d25d5a3#file-manifold-co-next-window-js</a>
## What’s next, after Next?
We may, in the future, explore other options if and when our application needs server-side-rendering *and* has a more complicated server layer (we’ve talked about integrating single-sign-on between [manifold.co](https://manifold.co) and [dashboard.manifold.co](https://dashboard.manifold.co)). But until then, Next.js was a huge payoff for little time investment.
<h3 class="blog-cta-headline">Add your API to our catalog and reach millions of developers.</h3>
<a class="button button-brand" href="/providers"><div class="button-text">Get started</div><img src="https://assets.website-files.com/5d5acff74a2e86484bb721fc/5d7bbab3af998bd22c394550_arrow_right.svg" alt="" class="icon"></a>