当サイトは、アフィリエイト広告を利用しています

【Next.js】Next.js製サイトでSEO対策をする

作成日:2023月05月07日
更新日:2023年05月07日

ReactのNext.jsで作成したホームページで簡単なSEO対策をしたので
その方法をメモ。

SEO対策方法としては色々あったが下記のライブラリを使用することにした

  • next-seo

next-seo

インストール

まずはnext-seoをインストールする

npm
npm install next-seo

または

yarn
yarn add next-seo

設定

next-seoをインストールできたら、seoの設定を行っていく。
設定は

  • デフォルトSEO設定
  • ページごとのSEOの設定

を行う。

デフォルトSEO設定

デフォルトSEOの設定を行う。
デフォルトSEOの設定はnext-seoのDefaultSeoコンポーネントをpages/_app.tsxにimportして行う。
SEO内容は「next-seo.config.js」を作成して記述する。 ※DefaultSeoコンポーネントのpropsに書くこともできるが長ったらしいので「next-seo.config.js」に外だしした。

next-seo.config.js作成

プロジェクト直下に「next-seo.config.js」を作成する

next-seo.config.js
export default {
titleTemplate: '%s | サイト名',
defaultTitle: 'タイトル',
additionalMetaTags: [
{
property: 'dc:creator',
content: '運営者名',
},
{
name: 'application-name',
content: 'サイト名',
},
],
openGraph: {
url: 'https://hogehoge.com/',
type: 'website',
locale: 'ja_JP',
site_name: 'サイト名',
},
// twitter: {
// handle: '@hogehoge',
// site: '@fugafuga',
// cardType: 'summary_large_image',
// },
};

内容を適宜、修正、追加する

pages/_app.tsx

next-seoのDefaultSeoコンポーネントをpages/_app.tsxにimportして
作成した「next-seo.config.js」をpropsで渡すようにする

pages/_app.tsx
import { AppProps } from "next/app";
import PropTypes from "prop-types";
import { ThemeProvider } from "@mui/material/styles";
import CssBaseline from "@mui/material/CssBaseline";
import { CacheProvider, EmotionCache, Global, css } from "@emotion/react";
import theme from "@/theme";
import createEmotionCache from "@/createEmotionCache";
import { Layout } from "@/components/layouts/layouts";
import { MDXProvider } from "@mdx-js/react";
import { MDXComponent } from "@/components/MDXcomponent/MDXComponent";
import { DefaultSeo } from "next-seo";
import SEO from "../../next-seo.config";
const clientSideEmotionCache = createEmotionCache();
interface MyAppProps extends AppProps {
emotionCache?: EmotionCache;
}
function MyApp(props: MyAppProps) {
const { Component, emotionCache = clientSideEmotionCache, pageProps } = props;
return (
<CacheProvider value={emotionCache}>
<DefaultSeo {...SEO} />
<ThemeProvider theme={theme}>
{/* CssBaseline kickstart an elegant, consistent, and simple baseline to build upon. */}
<CssBaseline />
<Global
styles={css`
html,
body {
margin: 0px;
font-family: "Noto Sans JP", Roboto, sans-serif;
}
&::-webkit-scrollbar {
width: 10px;
}
&::-webkit-scrollbar-track {
background-color: #e4e4e4;
border-radius: 100px;
}
&::-webkit-scrollbar-thumb {
background-color: #454a96aa;
border-radius: 100px;
}
`}
/>
<Layout>
<MDXProvider components={MDXComponent}>
<Component {...pageProps} />
</MDXProvider>
</Layout>
</ThemeProvider>
</CacheProvider>
);
}
MyApp.propTypes = {
Component: PropTypes.elementType.isRequired,
emotionCache: PropTypes.object,
pageProps: PropTypes.object.isRequired,
};
export default MyApp;

ちょっとEmotionやMDXをいれてるせいでごちゃごちゃしてますが
重要なのはハイライトしている部分だけです...

pages/_app.tsxに書くことでとすべてのページに反映されます。

ページごとのSEOの設定

デフォルトSEO設定をしただけだと全ページで同じSEOになってしまうので
各ページごとにも設定してやる必要がある。
各ページの設定ではNextSeoコンポーネントをimportして使う

ページの設定

各ページでnext-seoからNextSeoをimportして

  • title
  • description

をpropsで渡して上書きする

page.tsx
import { NextSeo } from "next-seo";
~
return (
<>
<NextSeo
title="xxxxxx"
description="xxxxxxxxxxxxxxxxx"
/>
~
</>

ページごとに設定する

まとめ

上記の対応をした上でデプロイして、ホームページを開き
F12でhtmlのheadタグ内を見れば設定されることが確認できると思います。

参考

新着記事

タグ別一覧
top