Next.jsのbuild時のrobots.txtとsitemap.xmlのエラーの対応

今回はNext.jsのbuild時のrobots.txtとsitemap.xmlのエラーの対応紹介です。
- 1. robots.txtとは?
- 2. sitemap.xmlとは?
- 3. Error: export const dynamic = “force-static"/export const revalidate not configured on route “/robots.txt" with “output: export".
- 4.
- 5. Error: export const dynamic = “force-static"/export const revalidate not configured on route “/sitemap.xml" with “output: export".
- 6. robots.txtとsitemap.xmlのエラーの対応
robots.txtとは?

検索エンジンのクローラーに「どのページをクロールすべきか・すべきでないか」を指示するファイルです。
robots.txtはサイトのルート (/) に配置する必要がある ので、Next.jsでは public/robots.txt に置くのが一般的です。
sitemap.xmlとは?

検索エンジンに「このサイトのページはここにあるよ!」と伝えるファイルです。
robots.txt が「どこをクロールしていいか」を指示するのに対し、sitemap.xml は「どのページが存在するか」を伝えます。
SEO(検索エンジン最適化)に重要 で、Google Search Consoleでも登録できるです。
スポンサードサーチ
Error: export const dynamic = “force-static"/export const revalidate not configured on route “/robots.txt" with “output: export".
app/robots.txt/route.ts のような App Router の API ルートを使って robots.txt を動的生成しようとしいて、next export では API ルートが使えないため、エラーが発生します。
Error: export const dynamic = "force-static"/export const revalidate not configured on route "/robots.txt" with "output: export".
スポンサードサーチ
Error: export const dynamic = “force-static"/export const revalidate not configured on route “/sitemap.xml" with “output: export".
app/sitemap.xml/route.ts で sitemap.xml を動的生成しようとしいて、next export では API ルートを使えないため、エラーが発生すます。
Error: export const dynamic = "force-static"/export const revalidate not configured on route "/sitemap.xml" with "output: export".
robots.txtとsitemap.xmlのエラーの対応
robots.txtとsitemap.xmlのエラーの対応は「app/robots.ts」と「sitemap.ts」を削除します。
find src/ app/ pages/ -iname "sitemap.*" -o -iname "robots.*" -exec rm -rf {} + && echo -e "User-agent: *\nAllow: /\nDisallow: /api/\nDisallow: /admin/\nDisallow: /_next/\nDisallow: /private/\nSitemap: http://tech-innovation.jp/sitemap.xml" > public/robots.txt && echo '<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></urlset>' > public/sitemap.xml && rm -rf .next && npm run build && ls -l out/
sitemapとrobotsを削除
- sitemap.xml だけでなく robots.txt も削除対象にする
- src/, app/, pages/ の robots.txt や robots.ts などを削除
- 動的ルート (app/robots.txt/route.ts など) を削除し、next export エラーを防ぐ
find src/ app/ pages/ -iname "sitemap.*" -o -iname "robots.*" -exec rm -rf {} +
public/robots.txtを作成
- 静的なrobots.txt を public/ に作成
- 検索エンジンのクロールルールを指定
- Sitemap: のURLも設定
echo -e "User-agent: *\nAllow: /\nDisallow: /api/\nDisallow: /admin/\nDisallow: /_next/\nDisallow: /private/\nSitemap: http://tech-innovation.jp/sitemap.xml" > public/robots.txt
.next/ を削除
rm -rf .next
npm run build を実行
ビルドキャッシュをクリアし、クリーンな環境でビルド
npm run build
robots.txt & sitemap.xmlのファイル確認
ls -l out/ で robots.txt & sitemap.xml ができたか確認
ls -l out/
エラーなく robots.txt & sitemap.xml が out/ に生成されているかチェック
以上でbuildエラーが消えます。
余談ですが、exportしている場合はoutディレクトリを公開します。
next.config.js
import type { NextConfig } from "next";
const nextConfig: NextConfig = {
output: "export", // ✅ `next export` を有効化
images: {
unoptimized: true, // ✅ `next/image` の最適化を無効化(CDNなしで動作)
},
trailingSlash: true, // ✅ URL を `/` で終わるようにする(ロリポップ向け)
};
export default nextConfig;