iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🌷

A Guide to Building a Personal Blog with Next.js

に公開
2023/05/09

Introduction

My name is yajium (@yajium_), and I am a new graduate engineer.

I am currently in my first year of professional life and started programming this April. Since I use C# at work, I studied Next.js and React from scratch for this project.

I created a personal blog using React + Next.js + Tailwind CSS, so I hope this serves as a helpful reference for others starting from zero!

I began my blog creation after completing the Next.js tutorial, so I recommend starting with the tutorial as your first step.

↓ GitHub
https://github.com/yajium/yajium-blog


Environment Setup

Setting up blog-starter

For my blog, I used the standard create next-app like in the Next.js tutorial, but this blog-starter seems like a better option.
I will proceed with this blog-starter for this explanation.

https://github.com/vercel/next.js/tree/canary/examples/blog-starter

  1. Install yarn
    While you can proceed with npm, yarn is reportedly faster (it's like an enhanced version of npm), so we will use yarn.
  • This assumes Node.js is already installed. Skip if you already have yarn.
cmd
npm install --global yarn
  1. Create the blog-starter repository
cmd
// After moving to the directory where you want to create the repository
yarn create next-app --example blog-starter [your-project-name]
  1. Move to the [your-project-name] directory created in step 2 and run yarn dev
cmd
cd [your-project-name]
yarn dev
  1. Access http://localhost:3000/ and if the page below is displayed, you're good to go!
    blog-starter home

🌟 Prettier installation
Prettier is a code formatter that allows you to maintain a consistent coding style across your source code.

  1. You can run Prettier in real-time by installing a VSCode extension.
    Install the following extension in VSCode:
    https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode

  2. Open the project created during the environment setup in VSCode, and create a .vscode directory and a setting.json configuration file directly under the project directory.

  3. Add the following code inside setting.json.

setting.json
{
    "editor.formatOnSave": true,
    "editor.defaultFormatter": "esbenp.prettier-vscode"
}

  1. Enable Format On Save
    Open VS Code settings and type onsave in the search bar. Then, check the box for Format On Save.

  2. Set Prettier as the Default Formatter
    Type format in the same settings search bar as step 4, select Prettier in the Default Formatter item, and turn on settings sync using the button in the upper right.

  3. Now your code will be formatted whenever you save!

🌟 ESLint installation
*This is optional since we are creating a personal blog, so you can skip it.
ESLint is what is known as a linter—a tool that statically analyzes programs to find bugs and issues. For example, if you run ESLint while there is a console.log in your source code, it can output an error like "console.log remains! Please fix it." This is helpful when you want to unify coding styles in team development.

↓ For those who want to know more
https://typescriptbook.jp/tutorials/eslint

Like Prettier, you can use a VSCode extension.
https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint

Add the following code to your setting.json.

setting.json
{
	"editor.codeActionsOnSave": {
	  "source.fixAll": true
	},
	"eslint.alwaysShowStatus": true,
}

Installing Tailwind CSS

It's very simple.
Tailwind CSS is a CSS framework; you might be familiar with Bootstrap as a well-known CSS framework.
The basic way to use Tailwind CSS is, for example, when you want to write CSS for font-size, you can easily apply it by writing something like className="text-xl".
https://tailwindcss.com/docs/font-size

  1. Move to the repository you created and run the following commands:
cmd
yarn add -D tailwindcss postcss autoprefixer
yarn tailwindcss init -p
  1. In the content array within tailwind.config.js, specify the directories and file paths where you plan to apply Tailwind CSS.
tailwind.config.js
module.exports = {
  content: [
+   "./pages/**/*.{js,ts,jsx,tsx}",
+   "./src/components/**/*.{js,ts,jsx,tsx}",
  ],
  ...
}

By default, pages and components are already added, so if they are already there, this step is unnecessary.

  1. Add Tailwind CSS imports to index.css (or globals.css)
    In this blog-starter, it is named index.css instead of globals.css, so add the following imports to it (skip if they already exist).
index.css
+ @tailwind base;
+ @tailwind components;
+ @tailwind utilities;

What are globals.css or index.css? They are CSS files applied to every page. For example, if you write CSS for the p tag in this file, since this CSS file is imported in _app.tsx, that CSS will be applied to the p tags on every page.

  1. Try using it
    You can use it by writing it in the following format.
index.tsx
<h1 className="text-3xl font-bold underline">
  Hello world!
</h1>

🌟 Extra Edition
If you have things that will be reused frequently (like li tags), writing classes like <li className="text-3xl font-bold underline"></li> every time would be a lot of work. There is a way to use Tailwind CSS similar to writing regular CSS.

You can use the Tailwind CSS style by using @apply as shown below.

xxx.tsx
import styles from "../styles/yyy.module.css";

<div className={styles["profile-detail"]}>...</div>
yyy.module.css
.profile-detail {
  @apply mx-10 my-16 grid grid-cols-1 gap-14 text-left;
}

.profile-detail h2 {
  @apply border-b border-b-gray-300 pb-3 text-center text-lg font-semibold;
}

In VS Code, an error might occur with @apply. In that case, you can resolve it by installing an extension called PostCSS Language Support.


Font Settings

Now, let's enable your favorite fonts in Tailwind CSS.

  1. Copy the @import and font-family of your favorite font from Google Fonts or similar sites.

https://fonts.google.com/

  1. Add the @import to index.css (or globals.css).

  2. Add the font-family to tailwind.config.js.

tailwind.config.js
theme: {
    extend: {
+     fontFamily: {
+       fancy: ["Kiwi Maru"],
+     },
    },
  },

You can use any name for fancy. Enter the font-family name inside the [].

  1. Use it in practice
    As shown below, use it in the format font-[the name you set in step 3].
<div className="font-fancy">
   <main>{children}</main>
</div>

☃ Little Tips & Tricks ☃

By this point, you should be able to handle basic development. Here, I'll list the libraries I used for my personal blog and the points where I got stuck.

Making Markdown look like Zenn

When converting blog posts into Markdown format, it takes a lot of effort to output them as-is or create custom CSS. For this project, I used Zenn's library to give it a Zenn-like appearance.
https://zenn.dev/team_zenn/articles/intro-zenn-markdown
It was very easy to apply, so I highly recommend it!

Icon Settings

React Icons is very convenient when you want to display icons like the one in the image below.

https://zenn.dev/taichifukumoto/articles/how-to-use-react-icons
This article summarizes it well, so please take a look.

SNS Share Buttons

For example, these are Twitter buttons for sharing a post on social media (as seen in the image below). There is also a useful library for this called react-share.
The usage is described in the Readme.

https://github.com/nygardk/react-share

Automatic OGP Image Generation

If you create SNS share buttons, you should definitely have OGP settings as well.
I used @vercel/og. You should be able to implement it by following this reference.
https://zenn.dev/hiromu617/articles/c03fef6f4d6c6e

However, I got stuck quite a bit with these OGP image settings, so I'll leave some notes here as precautions.

  • Apply Japanese fonts

If you want to dynamically change the OGP image using the post title and that title is in Japanese, it's better to include Japanese fonts before the initial deployment.
The reason for doing this before deployment is that if there are Japanese characters or full-width symbols, vercel/og might not handle them correctly, generating a completely white image. If you deploy in that state, the cache will remain on Twitter's side, and even if you upload corrected code, the cached white image might continue to appear for a year. Although it seems possible to shorten the cache duration, I wasn't able to go that far. If anyone has expertise in this, please let me know in the comments!

Images inside md files

When inserting images within a .md file, you need the path where the image is located. However, on localhost or after deployment, the images couldn't be retrieved, resulting in a Not Found error.

It seems that after converting Markdown to HTML, you need to transform the images into React component <Image> tags.
Following the steps below might work, but when combined with zenn-markdown, it became confusing, so I decided to load them from Google Drive instead.
https://blog.ryou103.com/post/next-js-markdown-image/

However, image display is currently quite slow, so I feel like I want to switch to the method above or change it in some way.
If you know a better method, I'd be happy if you could let me know in the comments 😃


Finally

That summarizes what I did when creating a personal blog using Next.js.
I hope you find it helpful!


Reference Articles
https://zenn.dev/var/articles/616b6c6ae78c02
https://tailwindcss.com/docs/guides/nextjs

Discussion