DEV Community

Cover image for Building a Blog with Next.js

Building a Blog with Next.js

zach profile image Zach Sherman Originally published at ・5 min read

Originally published on

Next.js is an awesome new framework for building universal React applications. In simple terms, that means you can use React to render templates on the server, as well frontend components the way you're most likely used to. The advantages of this are numerous (shared components, faster rendering, great tooling), but getting it all to work properly is generally a pain.

Next.js makes this process easy, and with the release of V3 I thought I'd whip up a blog to learn and demonstrate how it works. In this tutorial we'll be using the following:

  • next (3.X)
  • styled-components (phenomenal css-in-js solution)
  • next-routes (middleware for expressive routes in next)
  • express (for serving our pages, although you can also do static exports)

I'd highly recommend you follow along with the github repo here, as some components are left out for the sake of brevity.

1. Getting Started

Getting set up is simple, you can follow along in the docs but the gist of it is to install next, react, and react-dom, add a simple build script and create your index file.

yarn add next@beta react react-dom --save

Add the following scripts to your package.json

  "scripts": {
    "dev": "next",
    "build": "next build",
    "start": "next start"

Then create an index.js file inside of a pages folder in the root

// ./pages/index.js

export default () => (
  <div>Welcome to next.js!</div>

Then you can just run yarn dev and you should be up and running on localhost:3000. Hot reloading is baked in by default, which you can peek at if you inspect the .next folder in your root directory.

2. Adding Some Style

Next up we'll configure styled-components to style our blog.

First run yarn add styled-components.

Then create a custom _document.js file in the root with the following:

import Document, { Head, Main, NextScript } from 'next/document'
import { ServerStyleSheet } from 'styled-components'
import 'styles/global-styles';

export default class SiteDocument extends Document {
  render () {
    const sheet = new ServerStyleSheet()
    const main = sheet.collectStyles(<Main />)
    const styleTags = sheet.getStyleElement()
    return (
          <meta charSet="utf-8" />
          <meta name="viewport" content="initial-scale=1.0, width=device-width" />
          <div className="root">
          <NextScript />

The custom _document.js allows us to override the default page layout and inject our own styles and markup surrouding our react app.

3. Creating a Layout

Now let's create a main layout which all of our blog views will use, put the following in layouts/Main.js:

/* layouts/Main.js */

import Head from 'next/head'
import Wrapper from './Wrapper'
import Nav from 'components/Nav'
import Footer from 'components/Footer'

export default ({ children, title = 'This is the default title' }) => (
      <title>{ title }</title>
      <Nav />

      { children }


We'll use this layout to wrap our pages, which can override the <Head> tags and render content into the { children } block.

4. Rendering Posts

Now that we have our layout set up, let's modify our index.js page to take advantage of it, and also render some posts.

Update pages/index.js with the following:

import React from 'react'
import Layout from 'layouts/Main';
import { getPosts } from 'api/posts'
import { Link } from 'routes'

import Post from 'components/Post'

const IndexPage = ({ posts }) => (
      { => (
        <Post key={p.title} post={p} />

IndexPage.getInitialProps = async ({ req }) => {
  const res = await getPosts()
  const json = await res.json()
  return { posts: json }

export default IndexPage

The key here is the getInitialProps on our IndexPage component, which fetches all of the required data needed for this page to render. When this page is accessed directly at localhost:3000, Next will take care of fetching the data before the page is rendered. If we're navigating to this page from another one, no additional page reloads will happen, Next's clientside routing will take over and fetch the data for us before rendering the component thanks to the Link component. You can even add the prefetch property to tell Next to prefetch that page for blazing fast page loads.

Now we'll use some sample json and place the api in api/posts/index.js:

import fetch from 'isomorphic-fetch'

export function getPosts() {
  return fetch('')

export function getPost(slug) {
  return fetch(`${slug}`)

And add our Post component in components/Post/index.js:

import React from 'react'
import { Link } from 'routes'
import Wrapper from './Wrapper'

const PostItem = ({ post }) => (
    <Link route='post' params={{ slug: post.title }}>

export default PostItem

When you reload the page you should see a list of posts getting rendered by our index page like so (you can see the styles in the github repo

Next.js Blog Preview

5. Post Pages

Now that we have a list of posts, lets add a route to view each individual post. Create a new page in pages/Post.js like so:

import React from 'react'
import Link from 'next/link'
import styled from 'styled-components'
import Layout from 'layouts/Main';
import { getPost } from 'api/posts'

const PostPage = ({ post }) => (

PostPage.getInitialProps = async ({ query }) => {
  const res = await getPost(query.slug)
  const json = await res.json()
  return { post: json[0] }

export default PostPage

This page is reponsible for fetching and rendering individual posts, so let's add a route to show it. We'll be using next-routes for some nice expressive route definitions, so simply run:

yarn add next-routes

and add a routes.js folder in the root with the following:

const nextRoutes = require('next-routes')
const routes = module.exports = nextRoutes()

routes.add('index', '/')
routes.add('post', '/blog/:slug')

Then make sure to add this middleware in ./server.js

const express = require('express')
const next = require('next')
const routes = require('./routes')

const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
const handler = routes.getRequestHandler(app)

.then(() => {
  const server = express()

  server.get('*', (req, res) => {
    return handle(req, res)

  server.listen(3000, (err) => {
    if (err) throw err
    console.log('> Ready on http://localhost:3000')

Now our <Link route='post' params={{ slug: post.title }}> components in pages/index.js will map to this page with the proper params and if you click on one you should see something like this:

Next.js Post

That's it! You can easily sub in your own endpoints in api/posts/index.js to fetch from your API or CMS of choice.

You can see a working demo at and view the code at You can also learn more about Next at

Discussion (0)

Editor guide