TNS
VOXPOP
You’re most productive when…
A recent TNS post discussed the factors that make developers productive. You code best when:
The work is interesting to me.
0%
I get lots of uninterrupted work time.
0%
I am well-supported by a good toolset.
0%
I understand the entire code base.
0%
All of the above.
0%
I am equally productive all the time.
0%
Serverless / Software Development

How to Build a Serverless, SEO-Friendly React Blog

May 1st, 2017 12:00pm by
Featued image for: How to Build a Serverless, SEO-Friendly React Blog

Roger Jin
Roger Jin is a developer and technical writer at ButterCMS where he focuses on tools to make developers’ lives easier. Mixing his passion of programming and education, he writes articles focusing on serverless development, microservices, and much more. He develops mostly with React, Node.js, Elixir, and Ruby.

Serverless application architectures are gaining in popularity and it’s no mystery why. Developers are able to build and iterate on products faster when they have less infrastructure to maintain and don’t need to worry about server maintenance, outages, and scaling bottlenecks.

In this tutorial, we are going to show you how to build a serverless, CMS-powered blog using React, ButterCMS, and built-in prerendering through Netlify. The finished code for this tutorial is available on Github.

ButterCMS is a hosted API-based CMS and content API that lets you build CMS-powered apps using any programming language including Ruby, Rails, Node.js, .NET, Python, Phoenix, Django, Flask, React, Angular, Go, PHP, Laravel, Elixir, and Meteor. Butter lets you manage content using a hosted dashboard and integrate it into your front-end of choice with our API — you can think of Butter as similar to WordPress except that you build your website in your language of choice and then plug-in the dynamic content using an API.

Netlify is a static website hosting service that streamlines integration with prerendering services like Prerender.io, SEO.js, and Brombone.

Getting Started

We’ll use the Create React App starter kit.

Install Create React App:


Then create the boilerplate for our app:

Adding Routing

Our blog needs two pages: one for listing all posts and another for displaying individual posts. Create BlogHome.jsand BlogPost.js components in the src directory:



Create React App doesn’t offer routing out-of-the-box so we’ll add react-router:


In the source folder, create a new file called routes.js. We’ll create routes for the blog home page with and without page parameters, as well as the individual post page:

Next, we’ll update index.js so it uses our routes when initializing the application:

And finally, we’ll update App.js so it nests child components specified in our routes:

Building the Blog

We’ll use ButterCMS to build our blog. ButterCMS provides content APIs for blog posts, categories, tags, and authors.

First, we’ll install the ButterCMS Node.js API client:


We’ll then update ‘BlogHome’ to fetch posts from ButterCMS and render them. Use the API token in the example below or get your own by signing into ButterCMS with Github.

Next, we’ll update BlogPost.js to fetch and display posts based on the route:

<Head> Elements

Our blog is working but our post pages don’t have properly set HTML titles or meta tags. To fix this, we’ll use React Helmet.

First, install the package:


Import the Helmet module in our BlogPost and use it in our render() method to set HTML tags that should be in the <head>.

Here’s what the complete code for the component looks like. Inspect the DOM to verify that tags are getting set correctly.

Prerendering

Our blog is setup, but crawlers from search engines and social networks do not execute Javascript so our blog has terrible SEO.

There are a number of ways to address this, including setting up server-side rendering and pre-rendering services like Prerender.io and SEO.js. But these options all require setting up a Node.js server, and in this tutorial we want our app to be serverless. The solution we’ll use is hosting the app on Netlify, which offers a built-in prerendering service for their paid plans.

To do this, first we’ll create a production build of our app (you’ll need Yarn installed first):


Check your build directory to verify that the production build was created successfully. All you have to do to host the app is drag the build folder onto Netlify’s dashboard. Prerendering can be enabled from the app settings. Check out our live example.

Wrap Up

That’s it! We’ve built an SEO-friendly serverless blog using React and two third-party services. With our serverless approach, we can now get back to building software without having to ever worry about server maintenance, outages, or scaling bottlenecks.

I hope you enjoyed this tutorial. If you have any questions about setting up your ButterCMS-powered React app reach out to me at roger@buttercms.com and I’ll definitely reply. To get more articles and tutorials like this, sign up for the ButterCMS Newsletter.

Group Created with Sketch.
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.