I want to start by mentioning that as of starting this I didn’t have a lot of experience with SSR besides adding styled components in a NextJS app.
What’s the story here?
So I have this website I created called https://awesometalks.party that was being rendered on the client. I was having issues with SEO and I also wanted to be cool of course and render it on the server for performance and coolness bonus.
I did this using Razzle that is this really awesome tool and this meant that I had a server.js that ran on the server, an app.js that was isomorphic and also a client.js for all your client needs.
Here is an example of the most basic server you can use with Razzle: https://github.com/jaredpalmer/razzle/blob/next/examples/basic/src/server.js
Before you call me a noob, that I am, I want to say that this project was not just a React project but it also included several things that needed to be SSR’d as well, like:
So we are going to start with the ones that went super smoothly:
React Router (AKA: At least Michael Jackson loves me)
React Router is the best, we all know that but what I didn’t know is that server side rendering it didn’t involve a single tear and it was by far the fastest one to do so. All you need is to spread the magic over three files.
So in your app that is isomorphic you place all the routes but without the actual react-router provider like so:
In the actual client.js you place the provider since this will only run on the client but the routes are for everything:
Why isomorphic-fetch? You never know in these things!
What about the server? Well you just have to wrap your component to be SSR’d in a Static Router and that is about the only change you need to do:
That’s it! Those are the only differences we have to make in the server.js to get React Router working on the server as well 😀
Font Awesome (Yes, it needs SSR otherwise weird stuff happens)
I actually didn’t do this one, it was a PR but I have no idea where there are docs for this, but ok.
Font Awesome was implemented in a way where only the icons we needed were imported and added to the library so that everyone doesn’t get a 3mb icon font.
There is a icons.js with:
Notice the config where we tell font awesome that we will add the CSS and instead of it being added by the fontawesome package automatically. After that, this file needs to be imported into our app.js, but on this side, that is it. You just need to know that this config exists.
Then on the server some markup had to be modified for us to add the actual CSS: