javascript

How to Migrate Your Blog from Gatsby to Next.js

Nikola Đuza

Nikola Đuza on

How to Migrate Your Blog from Gatsby to Next.js

A while back, you were probably looking for a great solution to create your blog, and Gatsby was the most popular solution floating around at the time.

Now that some time has passed, a new player has risen to fame on the scene: Next.js has taken developer blogs by storm.

Whatever your reasons for switching, we will explain how to switch from the Gatsby stack over to Next.js in this post.

Why Use Gatsby?

When Gatsby became popular, it got advertised as the best thing around. You get convenient plugins, little to no setup, and the fantastic power of GraphQL. Plus, the word 'speed' was stamped all over it. Your static content is indeed fast with Gatsby, as it loads only essential data and then fetches other needed resources.

What is excellent about Gatsby is that you can jump-start a website with a couple of commands in your terminal. There is no more need to set up Webpack, code split, image optimize, and all the pesky details you usually do. You can choose preexisting themes, customize them at your own will and have a great-looking site. Plus, you get GraphQL to manage any data fetching you might need.

In reality, users (like myself) do not utilize GraphQL in Gatsby for what it's worth. The real power comes when you have multiple data sources to build your website. For example, you load content from your local files, plus you have some content living in some headless CMS. In that case, GraphQL may become very useful. In other cases, it is overkill, I believe (unless you want to get the hang of GraphQL).

Other than that, Gatsby generates HTML pages in build time and lets you host them wherever you like, meaning that you don't need a running server in some cases.

So, what can compete against a JavaScript (React) framework for static site generation? What gives Next.js the edge over Gatsby? Let's find out.

Why Switch to Next.js?

Next.js is also advertised as a React framework, but its focus is on Single Page Applications. The idea is that you can use it for other things than just rendering static content.

Powerful Server-Side Rendering with Next.js

The power of Next.js lies in Server-Side Rendering (SSR), which is becoming increasingly popular in the React community. Next.js is a hybrid tool for server-side rendered pages and a static sites generator.

The main benefit of SSR is that you get a faster Time To First Draw (TTFD) because the content can serve as HTML directly from the server. But, Time To First Byte (TTFB) can be something that it potentially lacks. That means that all the heavy lifting gets done on the server, unlike other frameworks that minimize TTFB by loading critical resources first and then working on the client, thus increasing TTFD.

The main reason why you'd choose Next.js is when you need some dynamic parts of a page versus plain static content. An example of this can be a website offering products. You can generate parts of it as static content and then render the product shop dynamically. This is the advantage that Next.js has over Gatsby.

So Next.js offers more when you need a mixture of static and dynamic content. But you should consider the kind of website you are building. Remember that Gatsby has many features that require little to no configuration if you need to get started quickly. If you're making a product and deciding between the two, I believe Next.js is a winner. Also, if you want more fine-grain control over configuration, Next.js gives you more freedom there.

Now let's work on a simple use case and show how to migrate a simple Gatsby blog to Next.js.

Gatsby to Next.js Migration Challenges

We want to migrate an existing Gatsby blog to use Next. The requirements are simple — we want blog readers to not notice that something has changed visually and content-wise. Also, we want to keep our SEO and performance at the same levels, if not better.

To do this, we have to migrate content (our blog posts) and all the things that come with it — images, code highlighting, content, and any potential hidden quirks there. Also, we want our sitemap to function correctly so that search indexes know our blog's pages. And, finally, we want to deploy our blog to the internet so others can read it.

Laying Out the Groundwork

To start, let's generate a simple Next.js app, so we have a clean plate. Starting from a simple app will help us understand both Next.js and the process to follow.

Luckily, we don't have to write everything by hand. There's a handy create-next-app tool at our disposal. We will start by generating our Next.js project:

1npx create-next-app nextjs-blog --use-npm

With the command above, we will get all the essential files needed to start and run our Next.js app for now. If you peek at package.json, you will see a simple setup where we have next, react, and react-dom` as dependencies. It seems pretty lightweight compared to Gatsby, where you get a lot of things out of the box.

You can check whether everything is working correctly by running npm run dev. You should get a "Welcome to Next.js" page on http://localhost:3000/.

Welcome screen of the Next.js app

Cool, now that we have it running, let's analyze the content of our new app. From the start, you will see that the file structure is stoic. We have the pages dir with _app.js and index.js. There's also the api directory, but we won't touch it in this blog post.

The _app.js is the 'root' of our project, and here we can define global styles, SEO, and other site-wide shenanigans. What is important is that every file you create in pages dir, will create a page on your website. This is similar to what Gatsby does, but with one quirk that we will go into later. To test this out, let's create pages/about.js:

1// pages/about.js
2
3import styles from "../styles/Home.module.css";
4
5export default function About() {
6  return (
7    <main className={styles.main}>
8      <h1 className={styles.title}>Hey, this is the about page.</h1>
9    </main>
10  );
11}

Now, let's go to http://localhost:3000/about, and we should see the following:

About page

Pretty cool! Now that we've figured that out, we will jump into the next section and migrate content into our brand new Next.js app.

Migrating Content Into Next.js

The heart of a blog is its content — without it, our blog won't be a true blog, right? So let's investigate how we can render blog posts in our Next.js blog. In Gatsby, the content is usually stored in the root of the project inside the content directory. We can create a similar structure here as well.

I went ahead and created content/blog directory, and put two Markdown files there — first-post.md and second-post.md. Here's how they look:

1# content/blog/first-post.md
2
3---
4title: My first post on Next.js blog
5description: I tried my best.
6date: 8 January 2021
7---
8
9Hey there, this is my first post.
1# content/blog/second-post.md
2
3---
4title: My second post, with code examples
5description: Best code is no code.
6date: 21 February 2021
7---
8
9Here's some Ruby code:
10
11```rb
12def hey_there
13  puts 'Hey there'
14end
15```
16
17And, here's some JavaScript code:
18
19```javascript
20const hello() => {
21  console.log('Hello')
22}
23```

Excellent, so how do we show these in the browser? Glad you asked. We need to somehow load these Markdown files to Next.js. There are several ways, but my favorite is to read file slugs from the posts directory and load them one by one.

Load Posts on the Index Page

If you come from Gatsby, you will find this part a bit unexpected. Gatsby allows you to add a plugin to make Markdown files available and parses them to you with GraphQL. Unfortunately, we're not in Gatsby, and we have to take more control in our hands. So, let's see how we can load and parse Markdown files so that our React components understand them.

Let's create a lib directory in the root of our project and put a file named posts.js there. This file will contain the logic for loading, parsing, and sorting our blog posts. Here's how the file should look:

1// lib/posts.js
2
3import fs from "fs";
4import { join } from "path";
5import matter from "gray-matter";
6
7const postsDirectory = join(process.cwd(), "content/blog");
8
9export const posts = () => {
10  const slugs = fs.readdirSync(postsDirectory);
11  const allPosts = slugs
12    .map((slug) => getPostBySlug(slug))
13    .sort((post1, post2) =>
14      new Date(post1.frontmatter.date) > new Date(post2.frontmatter.date)
15        ? -1
16        : 1
17    );
18
19  return allPosts;
20};
21
22export const getPostBySlug = (slug) => {
23  const realSlug = slug.replace(/\.md$/, "");
24  const fullPath = join(postsDirectory, `${realSlug}.md`);
25  const fileContents = fs.readFileSync(fullPath, "utf8");
26  const { data: frontmatter, content } = matter(fileContents);
27
28  return { slug: realSlug, frontmatter, content };
29};

We use fs to load files and gray-matter library to parse its content. For this, you will have to install gray-matter in the project with:

1npm install gray-matter

So, what happens in lib/posts.js? We find out our postsDirectory, then we get all the file slugs and feed them one by one into getPostBySlug. The getPostBySlug then reads the file and parses it using the matter function from the gray-matter lib. We want to use matter to parse the frontmatter we added in our blog posts (meta-information about our posts, like title, description, and date).

Cool, now that we got that out of the way, let's load our blog posts on the index page. We will go into pages/index.js, which serves our index page, and modify it a bit to look like this:

1// pages/index.js
2
3import styles from "../styles/Home.module.css";
4import { posts } from "../lib/posts";
5import Layout from "../components/layout";
6
7export default function Home({ allPosts }) {
8  if (!allPosts || !allPosts.length) return;
9
10  return (
11    <Layout>
12      <h1 className={styles.title}>
13        Welcome to <a href="https://nextjs.org">Next.js!</a>
14      </h1>
15
16      <p className={styles.description}>
17        Get started by editing{" "}
18        <code className={styles.code}>pages/index.js</code>
19      </p>
20
21      {allPosts.map((post) => (
22        <div key={post.slug} className={styles.grid}>
23          <a href={post.slug} className={styles.card}>
24            <h2>{post.frontmatter.title}</h2>
25            <span>{post.frontmatter.date}</span>
26            <p>{post.frontmatter.description}</p>
27          </a>
28        </div>
29      ))}
30    </Layout>
31  );
32}
33
34export async function getStaticProps() {
35  const allPosts = posts();
36
37  return {
38    props: {
39      allPosts,
40    },
41  };
42}

We cleaned up the index.js a bit by creating a Layout component that holds common rendering logic which we'll later reuse. The important thing here is the getStaticProps function. getStaticProps is an async function that gets picked up by Next.js. If you provide this function, Next.js will pre-render this page at build time using the props returned by getStaticProps. So we ensure our posts are loaded at build time, and then later, we can use them in the Home component.

In the Home component, we receive allPosts props and render them one by one. I used and modified the styling a bit from the generated Next.js app, but you can go crazy and do whatever you like here. You can go without styles or bring in your own. Styling and CSS are huge topics and we can discuss them in another blog post. Let's keep it simple for now.

Cool, now that we added the code above, we should see something like this in our browser:

Index page with blog posts

Looks pretty nice so far. Now when users come to our website, they will see the blog posts we have. But there's one problem. When they click on the blog post, they will get a 404 page. So it's time to implement rendering logic for each blog post.

Loading a Single Post

To do this, let's create a [slug].js file inside pages directory. If you haven't seen a file name like this before, you might be confused. Don't worry, this is a Next.js feature. Our file resides in pages/[slug].js, which means that requests like /some-blog-post or /abc will be handled by the logic in pages/[slug].js. Next.js calls it dynamic routing, and it is a very elegant solution.

Let's add some logic to pages/[slug].js so it renders a blog post:

1// pages/[slug].js
2
3import Layout from "../components/layout";
4import { posts, getPostBySlug } from "../lib/posts";
5
6const Post = ({ content, frontmatter }) => {
7  return (
8    <Layout>
9      <article>
10        <h1>{frontmatter.title}</h1>
11        <p>{frontmatter.date}</p>
12
13        {content}
14      </article>
15    </Layout>
16  );
17};
18
19export default Post;
20
21export async function getStaticProps({ params }) {
22  const { content, frontmatter } = getPostBySlug(params.slug);
23
24  return {
25    props: {
26      content,
27      frontmatter,
28    },
29  };
30}
31
32export async function getStaticPaths() {
33  const allPosts = posts();
34
35  return {
36    paths: allPosts.map((post) => {
37      return {
38        params: {
39          slug: post.slug,
40        },
41      };
42    }),
43    fallback: false,
44  };
45}

If you remember the Layout component — I extracted it to lib/components, and I'm calling it from there. In the body of Post component, I render some frontmatter data we set in the Markdown file. We get that data from the previously mentioned getStaticProps. But, no, we have a new kid on the block — getStaticPaths. The getStaticPaths function should return paths, so Next.js knows which paths to render to HTML at build time.

All we do in getStaticPaths is fetch all our posts and let Next.js know that each of those slugs should be rendered at build time. Now we can open a blog post from our index page, and we should see something like this:

Basic blog post

The title and the date are there, but the content of the post is a little messed up. That's because we didn't convert the raw Markdown to HTML. Let's go ahead and do that. We will use the ReactMarkdown component from the react-markdown library. Let's install it first with:

1npm install react-markdown

After react-markdown is installed, we can modify our pages/[slug].js to look like this:

1// pages/[slug].js
2
3...
4
5import ReactMarkdown from "react-markdown";
6
7const Post = ({ content, frontmatter }) => {
8  return (
9    <Layout>
10      <article>
11        <h1>{frontmatter.title}</h1>
12        <p>{frontmatter.date}</p>
13
14        <ReactMarkdown>{content}</ReactMarkdown>
15      </article>
16    </Layout>
17  );
18};
19
20...

And with that, we should see a better-looking blog post like this:

Blog post with Markdown rendered

Now our blog post looks better. But there's one thing missing — code highlighting. Let's jump in and highlight code sections.

Add Code Highlighting

We will use highlight.js library to highlight code inside our blog posts. First, let's install the library with:

1npm install highlight.js

Now, we can highlight our code. To do it, we will pass a custom component to ReactMarkdown to render code blocks. Here's how it should look:

1// pages/[slug].js
2
3...
4import highlight from "highlight.js";
5
6const Post = ({ content, frontmatter }) => {
7  return (
8    <Layout>
9      <article>
10        <h1>{frontmatter.title}</h1>
11        <p>{frontmatter.date}</p>
12
13        <ReactMarkdown
14          components={{
15            code: ({ ...props }) => <Code {...props} />,
16          }}
17        >
18          {content}
19        </ReactMarkdown>
20      </article>
21    </Layout>
22  );
23};
24
25const Code = ({ children, className, ...props }) => {
26  const highlightedHtml = highlight.highlightAuto(children[0]).value;
27
28  return (
29    <pre>
30      <code
31        className={`${className} hljs`}
32        dangerouslySetInnerHTML={{ __html: highlightedHtml }}
33      />
34    </pre>
35  );
36};
37
38...

In the Code component, we receive children — the code we want to highlight, and a className — the name of the class we'll set on the code element. We call highlight for children[0] and render it as HTML with React's dangerouslySetInnerHTML.

Now that we have the highlighting part, we need to import the styles needed to highlight languages. Luckily, highlight.js has some themes you can check out. We're going to load a Monokai theme in the _app.js like so:

1// pages/_app.js
2
3import "../styles/globals.css";
4import "../node_modules/highlight.js/styles/monokai.css";
5
6function MyApp({ Component, pageProps }) {
7  return <Component {...pageProps} />;
8}
9
10export default MyApp;

After these changes, we should see our blog post like this:

Highlighted blog post

Perfect, our blog posts are nice and readable now. Next, we need to figure out how to load images on our blog. Before that, let's discuss whether we could have gone the other way with rendering our blog content.

Markdown or MDX?

The hot topic for a while has been MDX. What is MDX, you might be asking? Its tagline is "Markdown for the component era." But in short, it allows you to import and render components alongside markdown content. It seems pretty nifty and helpful, but I like to keep my blog simple. For now, I don't need components inside my blog posts.

If you are interested in using .mdx instead of .md files, there are solutions out there. We won't discuss them in this blog post, maybe in the following ones. But just so that you know, people can use MDX with Next.js. One of the popular solutions is @next/mdx.

So we've decided to use plain Markdown for now. Let's figure out how to show images properly in the next section.

Migrating Images from Gatsby to Next.js

One of Gatsby's cool things is its automatic image gimmicks if you use the gatsby-image plugin in your setup. You can achieve similar results if you utilize next/image inside Next.js projects. Let's compare the two:

  • Gatsby automatically optimizes images for you during build time, whereas Next.js does this on-demand.
  • Gatsby integrates into the internal GraphQL you use to load images, while Next.js lets you define how you will load them.
  • Both allow you to have a blurred image preview, and they will keep the space needed for the image before it loads. Reserving space for the image reduces layout shift on the page.

So they have pretty similar features, but the way you use them is different. In Gatsby, you try to configure the image plugin from a higher level, whereas in Next.js, a lot of the configuration feels very manual and like you are in control.

One simple way to get images to load for our blog is to put them in the public directory in the root of our project. Let's put an image of a cat — cat.jpg - to the public/cat.jpg. Then, in our blog post, we can render it like this:

1![Cat](/cat.jpg)

When we open the blog post, it will render the cat picture, but not in the most efficient way. Also, I don't like storing images in the public directory. We could fine-tune this solution to load the image efficiently, but let's try another, more straightforward approach.

So, how do we load images properly in our Next.js blog? Instead of putting images in the public dir, we can create a content/images directory and put our cat.jpg (and future pics) there. Alongside the new directory, we can create a new component called Image that we will feed to ReactMarkdown. The code should look like this:

1// pages/[slug].js
2
3...
4
5const Post = ({ content, frontmatter }) => {
6  return (
7    <Layout>
8      <article>
9        <h1>{frontmatter.title}</h1>
10        <p>{frontmatter.date}</p>
11
12        <ReactMarkdown
13          components={{
14            code: ({ ...props }) => <Code {...props} />,
15            img: ({ ...props }) => <Image {...props} />,
16          }}
17        >
18          {content}
19        </ReactMarkdown>
20      </article>
21    </Layout>
22  );
23};
24
25const Image = ({ src, ...props }) => {
26  return (
27    <NextImage
28      src={require(`../content/images/${src}`)}
29      {...props}
30      placeholder="blur"
31      layout="responsive"
32    />
33  );
34};
35
36...

Now, we can render the cat in our blog post like so:

1# content/blog/first-post.md
2
3---
4title: My first post on Next.js blog
5description: I tried my best.
6date: 8 January 2021
7---
8
9Hey there, this is my first post.
10
11![Cat](/cat.jpg)
12
13Cats are great, I love dogs too.
14
15Wait a bit until the cat picture shows up above.

And we should get the blog post to look like this:

Blog with cat

Photo by Max Baskakov on Unsplash

Cool, but what just happened? We created a custom Image component, and we told ReactMarkdown to use it when it tries to render images. Then, in the Image component, we require a local image and tell next/image to blur it out before it gets loaded. Here's how it should look locally:

Loading cat blog post with blurring

Luckily, we don't need to set width and height in our scenario. Next.js will calculate it during build time since it has access to the picture we're trying to load. In other scenarios, like loading a remote image, you must set width and height attributes to the next/image component. Again, this is because Next.js doesn't have access (or at least doesn't try to have it) in the build time. The build procedure is what sets apart Gatsby and Next.js.

One more thing we did is set layout to responsive. You can take a look at different options that come with next/image here. The responsive layout will scale the image dimensions down for smaller viewports and scale up for larger viewports.

Now that we've loaded images properly let's do one more thing before deploying our website. Let's set up a sitemap so search engines are aware of the pages on our blog.

Setting Up the Sitemap for Your Next.js Blog

With Gatsby, you'd install a plugin, configure it a bit, and voila, you'd get a sitemap ready. As with everything else in Next.js, the process of forming a sitemap is a bit different. You are encouraged to do this on your own for more control. So this is exactly what we're going to do.

We will create a Node script that will run after the build is complete. Set up a scripts directory and add a file named generate-sitemap.mjs. Don't get freaked out by the .mjs extension — it is a way to define and use ECMAScript modules in a Node script. The logic inside the file should look like this:

1// scripts/generate-sitemap.mjs
2
3import { writeFileSync } from "fs";
4import { globby } from "globby";
5import prettier from "prettier";
6
7async function generate() {
8  const prettierConfig = await prettier.resolveConfig("./.prettierrc.js");
9  const pages = await globby([
10    "pages/*.js",
11    "content/**/*.md",
12    "!content/*.md",
13    "!pages/_*.js",
14    "!pages/[*.js",
15    "!pages/api",
16    "!pages/404.js",
17  ]);
18
19  const sitemap = `
20    <?xml version="1.0" encoding="UTF-8"?>
21    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
22        ${pages
23          .map((page) => {
24            const path = page
25              .replace("pages", "")
26              .replace("content/blog", "")
27              .replace(".js", "")
28              .replace(".md", "");
29            const route = path === "/index" ? "" : path;
30
31            return `
32              <url>
33                  <loc>${`https://DOMAIN_NAME_HERE${route}`}</loc>
34                  <changefreq>daily</changefreq>
35                  <priority>1.0</priority>
36              </url>
37            `;
38          })
39          .join("")}
40    </urlset>
41    `;
42
43  const formatted = prettier.format(sitemap, {
44    ...prettierConfig,
45    parser: "html",
46  });
47
48  writeFileSync("public/sitemap.xml", formatted);
49}
50
51generate();

In the script, we pick up the filenames of Markdown files inside content/ and filenames of certain files in pages/ using the globby library. We ignore files that start with [ and _ in pages/, together with the pages/api/ and the 404.js. Then, we remove excess data from the filenames to get paths (pages) on our website. Iterate over pages and add them as entries in the sitemap, with priority and change frequency attributes.

Finally, use prettier to format the sitemap properly, and write the sitemap to public/sitemap.xml so it's available there.

To run this script, we need to install two packages:

1npm install -D globby prettier

And, we will run this script after the build by defining it in the package.json:

1{
2  ...
3  "scripts": {
4    "dev": "next dev",
5    "build": "next build",
6    "postbuild": "node ./scripts/generate-sitemap.mjs",
7    "start": "next start",
8    "lint": "next lint"
9  },
10  ...
11}

So, when we run npm run build, our script will create a new file in public/sitemap.xml. We can add this file to the .gitignore so it doesn't get committed.

And that's it — we're ready to roll and publish our website!

Next.js Blog Deployment Options

What good is a blog if only you can read it? Probably not very good, so let's figure out how to deploy it and have other people read it. There are a few options for deployment — Netlify, Heroku, Vercel, and many others. In this post, we will look into Netlify and Vercel.

Both Netlify and Vercel offer free plans where you can deploy and host your website. But one important thing is that Vercel doesn't support commercial websites like eCommerce ones. If you plan to host something like this, you'd better check with Vercel if it's fine to do it on the free plan or you need to upgrade to their Pro plan.

Other than that, the two services are pretty similar in what they offer, but to me, it feels natural to deploy Next.js to Vercel since the company Vercel builds and supports Next.js. So, let's start with Vercel and see how we can deploy and host our website there.

Before we do it, let's push our website to GitHub to automate builds and deploys from there. I created a nextjs-blog-starter repo here that you can use. Then, you need to log in or sign up at Vercel. After that, you should be able to create a new project from GitHub and get a page similar to this one:

Ready to deploy

After clicking deploy and waiting for a bit, your blog should be online.

Confetti after successful deployment

Here's mine — https://nextjs-blog-starter-nikolalsvk.vercel.app/ — and you should get a similar URL when you deploy yours. Congrats for making it this far, yay!

Wrapping Up

Thank you for following along: now you should, hopefully, be able to migrate your blog post from Gatsby to Next.js. There was a lot of information covered in this blog post, and it might be too much to ingest in one sitting. The good thing is that you can always come back to it later.

But there's also a lot of ground we didn't manage to cover here. To 'complete' our blog, we need features like:

  • MDX (if we want components in Markdown)
  • auto-linking blog headers
  • scrolling to a page ID
  • an RSS feed
  • an email subscription box
  • a Tweet embedding solution for Twitter

and so on. If we did include all of these, this would be a mini-book instead of a blog post, so don't worry about it. We will cover these in future blog posts.

What is important is that you have a solid foundation to go and build whatever you want on your Next.js blog. You managed to get through the initial learning curve and how Next.js works. The things you add later should be a breeze.

I wish you good luck in building your blog with Next.js. If you have any questions or comments, feel free to reach me through the link in the bio below.

Cheers, and see you in the next one.

P.S. If you liked this post, subscribe to our JavaScript Sorcery list for a monthly deep dive into more magical JavaScript tips and tricks.

P.P.S. If you need an APM for your Node.js app, go and check out the AppSignal APM for Node.js.

Share this article

RSS
Nikola Đuza

Nikola Đuza

Nikola helps developers improve their productivity by sharing pragmatic advice & applicable knowledge on JavaScript and Ruby.

All articles by Nikola Đuza

AppSignal monitors your apps

AppSignal provides insights for Ruby, Rails, Elixir, Phoenix, Node.js, Express and many other frameworks and libraries. We are located in beautiful Amsterdam. We love stroopwafels. If you do too, let us know. We might send you some!

Discover AppSignal
AppSignal monitors your apps