A custom Next.js blog starter for use with create next app
. This starter design is based on the original blog starter provided by Next, but includes many extra features and performance improvements that are nice to have right out of the box.
Run this in your terminal to get started:
yarn create next-app --example https://github.com/sandypockets/nextjs-blog-starter/tree/main nextjs-blog-starter
Was this blog starter what you were looking for?
Preview the example live on StackBlitz:
Check out the live demo at blog-starter.sandypockets.dev, or see it in production as my actual blog at sandypockets.dev, generated using create-next-app
Deploy the example using Vercel:
This README will guide you through the basic set up. However, please refer to the blog posts in the live demo for more information. Don't worry, no lorem ipsums here. Each post contains real, actually helpful content.
Built and tested with Node 14
Bootstrap this starter using create next app
.
yarn create next-app --example https://github.com/sandypockets/nextjs-blog-starter/tree/main nextjs-blog-starter
Change into the new project directory.
cd nextjs-blog-starter
Start the development server.
yarn dev
Set up each of the constants, much like you would a .env
, in the lib/constants.js
file.
export const EXAMPLE_PATH = 'blog-starter'
export const CMS_NAME = 'Markdown'
export const HOME_OG_IMAGE_URL = 'https://og-image.vercel.app/Next.js%20Blog%20Starter%20Example.png?theme=light&md=1&fontSize=100px&images=https%3A%2F%2Fassets.vercel.com%2Fimage%2Fupload%2Ffront%2Fassets%2Fdesign%2Fnextjs-black-logo.svg'
export const BLOG_NAME = 'Next.js Blog Starter'
export const KEYWORDS = 'starter, blog, next.js, template'
export const DESCRIPTION = 'A starter blog template for Next.js'
export const AUTHOR = 'sandypockets'
export const LANG = 'en-CA'
export const GITHUB_REPO = 'https://github.com/sandypockets'
Sitemaps are an important part of SEO. This section walks through adding your base URL to the sitemap generator. The generator runs automatically after each build, generating a new sitemap each time you update your blog.
The generated sitemap can be found in public/sitemap.xml
. This command also generates a new public/robots.txt
file.
scripts/generate.sitemap.mjs
file.https://blog-starter.sandypockets.dev
with your own blog's website.en-CA
with your preferred locale (en-UK
, en-US
, etc.). This is used to determine the format of the date stamp in the sitemap.When you're ready to test it:
yarn build
public/
directory for the sitemap.xml
file and robots.txt
file.yarn start
http://localhost:3000/sitemap.xml
If you see the xml sitemap, then it was successful.
An RSS feed is available for the blog at /feed.xml
. However, you must first configure the RSS generator to use your own URL.
scripts/generate-rss.mjs
file.https://blog-starter.sandypockets.dev
value of BLOG_URL
with your own.When you're ready to test it:
yarn build
public/
directory for the feed.xml
file.yarn start
http://localhost:3000/feed.xml
If you see the xml RSS feed, then it was successful.
You will need to have your Google tag ID. If you do not have one, or do not have a Google Analytics account, you can sign up at analytics.google.com
.env.local.example
file, and name it .env.local
. To do so in the terminal, run:cp .env.local.example .env.local
G-XXXXXXXXXX
in the new .env.local
file you just created.You will need a free SendGrid account, which allows you to send up to 100 emails each day.
.env.local
file that you created when setting up Google Analytics.REPLACE-WITH-YOUR-API-KEY
with your actual API key from SendGrid.When you use inline code or codeblocks on your blog, they'll be highlighted with the Dracula theme style, like this:
However, you can choose from over 38 other themes that are ready to go right out of the box.
pages/_app.js
file.'prism-themes/themes/prism-dracula.css'
prism-dracula.css
portion with the name of the new theme file as shown on this page.For example, if you want to use the prism-duotone-sea
theme, then adjust the import statement on Line 5 to be import 'prism-themes/themes/prism-duotone-sea.css'
^1.4.0
^1.0.4
^7.4.6
^0.4.0
^0.24.0
2.2.6
2.16.1
4.0.2
latest
^1.9.0
^17.0.2
^17.0.2
13.0.0
13.0.1
^1.3.6
7.15.0
^6.3.7
^6.3.7
^6.3.7
^6.3.7
^10.4.0
^8.2.2
^8.11.0
12.1.0
^13.1.1
^8.4.5
^2.5.1
^1.2.2
3.0.5
Blog posts are stored in the /_posts
directory as Markdown files. Each post must include the appropriate front matter.
To create the blog posts we use remark
and remark-html
to convert the Markdown files into an HTML string, and then send it down as a prop to the page. The metadata of every post is handled by gray-matter
and also sent in props to the page.
An example of the required front matter:
Note: The formatting of the front matter is important. Ensure the indentation, and quotes remain the same.
---
title: 'A blog starter you actually want to use'
excerpt: 'There are hundreds of different blog starters out there. But none felt quite right. So I built my own. Based off the basic Next.js Blog Starter, but now with several handy features like dark mode (using local storage) or Google Analytics. It comes with Storybook too.'
coverImage: '/assets/blog/a-nextjs-blog-starter-you-actually-want-to-use/tree-minimal.jpg'
date: '2021-08-24T05:35:07.322Z'
author:
name: sandypockets
picture: '/assets/blog/authors/sandypockets_avatar.jpg'
ogImage:
url: '/assets/blog/a-nextjs-blog-starter-you-actually-want-to-use/tree-minimal.jpg'
---
Adjust the value of each key as needed.
There are two ways to create a new article: manually, or using the included script to scaffold one out.
First, you should replace the placeholder sandypockets
information in the scripts/generate-post.js
script with your own info. Then, it's as simple as running the script:
yarn new your-post-title
The script will generate a new post in the /_posts
directory with the title used in the command above, and a default front matter.
.md
) to the /_posts
directory. /public/assets/blog
directory. It is recommended you create a new folder for each blog post to prevent the images files from growing unwieldy. Note: The slug is the path that will be displayed, and is based on the title of your
.md
file. For example,kobe.md
becomeslocalhost:3000/posts/kobe
Contributions are what make the open source community such an amazing place to be, learn, inspire, and create. Any contributions you make are greatly appreciated.
git checkout -b feature/AmazingFeature
or git checkout -b fix/ContactForm
) git commit -m 'Add some AmazingFeature'
)git push origin feature/AmazingFeature
)Want more themes for your syntax highlighting? Check out Customize code syntax highlighting.