Traffine I/O

日本語

2023-05-25

next-sitemapにおけるサイトマップの出力場所の指定

はじめに

デフォルトでは、next-sitemappublic/ディレクトリに直接サイトマップファイルを生成します。これは、標準のサイトマップの保存場所がhttps://<ウェブサイトの名前>/sitemap.xmlの形式であることを意味します。

サイトマップのプライバシー

サイトマップはウェブスクレイパーにとって宝の地図のような役割を果たし、ウェブサイト内のリンクの包括的なリストを提供します。ウェブスクレイパーがサイトマップを特定すると、サイトのほとんど全てのページにアクセスできます。

サイトマップの場所は、理想的にはGoogleなどの検索エンジンのみが知っているべきです。Googleがサイトマップを認識すると、Yahoo! JapanやBingなどの他の検索エンジンでもウェブサイトのSEOが向上します。スクレイパーにはサイトマップの場所を公開する必要はありません。デフォルトの場所である/sitemap.xmlのままにしておくと、ウェブスクレイパーによる無限のクローリングを招くことになります。このため、スクレイパーに簡単にアクセスされることを防ぐために、デフォルトのサイトマップの保存場所を変更することが重要です。

サイトマップディレクトリの変更

next-sitemapライブラリを使用すると、サイトマップの保存場所を変更することができます。これは、next-sitemap.config.jsファイルでoutDirを指定することで実現できます。この機能により、ユーザーは好きな場所にサイトマップを保存する柔軟性を持つことができます。

具体的には、next-sitemap.config.jsファイルでoutDirを以下のように指定します。これによりsitemap.xmlrobots.txtの出力がpublic/my-dirに向けられます。

next-sitemap.config.js
 /** @type {import('next-sitemap').IConfig} */
 module.exports = {
   siteUrl: 'https://io.traffine.com/',
   generateRobotsTxt: true,
   sitemapSize: 7000,
+  outDir: './public/my-dir'
 };

robots.txtとsitemap.xmlの誤ったディレクション

ユーザーが直面する問題に、robots.txtsitemap.xmloutDirで指定されたディレクトリを正しく指していないことがあります。生成されたエントリを見てみます。

public/mu-dir/robots.txt
# *
User-agent: *
Allow: /

# Host
Host: https://hoge.jp

# Sitemaps
Sitemap: https://io.traffine.com/sitemap.xml # here is the problem
public/mu-dir/sitemap.xml
<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap><loc>https://io.traffine.com/sitemap-0.xml</loc></sitemap>  <!-- Here is the problem -->
</sitemapindex>

これらのファイルが/publicに生成されたかのように、誤ったパスを指していることがわかります。

これらのファイルは、次のようになるべきです。

robots.txt
  # *
  User-agent: *
  Allow: /

  # Host
  Host: https://hoge.jp

  # Sitemaps
- Sitemap: https://io.traffine.com/sitemap.xml # Here is the problem
+ Sitemap: https://io.traffine.com/my-dir/sitemap.xml
sitemap.xml
  <?xml version="1.0" encoding="UTF-8"?>
  <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
- <sitemap><loc>https://io.traffine.com/sitemap-0.xml</loc></sitemap>  <!-- Here is the problem -->
+ <sitemap><loc>https://io.traffine.com/my-dir/sitemap-0.xml</loc></sitemap>
  </sitemapindex>

問題への解決方法

robots.txtsitemap.xmlのディレクションの問題を修正するために、JavaScriptファイルを作成して使用する解決方法を実装できます。

まず、sitemap-replace.jsという名前のJavaScriptファイルを準備します。このスクリプトは既存のrobots.txtsitemap.xmlファイルを読み込み、誤ったパスを正しいパスに置換し、修正されたデータをファイルに書き込みます。以下に手順を示します。

sitemap-replace.js
const replaceSitemap = async (fileName) => {
  const fs = require('fs/promises')
  const appRoot = require('app-root-path')
  const subDirectory = 'my-dir' // Change this
  const filePath = `${appRoot}/public/${subDirectory}/${fileName}`

  const original = await fs.readFile(filePath, 'utf8')
  const replacedData = original.replace(
    /https\:\/\/io\.traffine\.com\/sitemap/g, // Change this
    `https://io.traffine.com/${subDirectory}/sitemap` // Change this
  )

  await fs.writeFile(filePath, replacedData, 'utf8')
}

;(async () => {
  await replaceSitemap('robots.txt')
  await replaceSitemap('sitemap.xml')
})()

次に、package.jsonpostbuildコマンドを調整して、next-sitemapコマンドの後にこのスクリプトが実行されるようにします。これにより、プロジェクトをビルドするたびにスクリプトが実行され、サイトマップのパスが指定したディレクトリに正しく向けられるようになります。

package.json
 {
 ...

   "build": "next build",
-  "postbuild": "next-sitemap --config next-sitemap.config.js"
+  "postbuild": "next-sitemap --config next-sitemap.config.js && node sitemap-replace.js"

 ...
 }

このスクリプトを実行すると、robots.txtsitemap.xmlファイルの内容が正しく更新され、新しいディレクトリを指すようになります。更新されたファイルは次のようになります。

public/my-dir/robots.txt
# *
User-agent: *
Allow: /

# Host
Host: https://io.traffine.com

# Sitemaps
Sitemap: https://io.traffine.com/my-dir/sitemap.xml
sitemap.xml
<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap><loc>https://io.traffine.om/my-dir/sitemap-0.xml</loc></sitemap>
</sitemapindex>

この解決方法により、サイトマップの場所のプライバシーが保持されつつ、検索エンジンが正しくアクセスできるようになります。

参考

https://github.com/iamvishnusankar/next-sitemap
https://zenn.dev/masa5714/articles/b00f4ebffbbcd4

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!