0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Next.jsのoutput: export環境下でサイトマップを動的生成したい

0
Posted at

結論

output: 'export'でのサイトマップ生成はなぜかNext.jsは対応していません。
なので、少々ハックな方法を使うことが必要です。

方法

/app/sitemap.xml/route.tsを追加する。

function getSitemap() {
  const map = [
    {
      url: 'https://my.app.url',
      lastModified: new Date(),
      changeFrequency: 'daily',
      priority: 1,
    }
  ];

  return `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
    ${map
      .map(
        (item) => `
            <url>
              <loc>${item.url}</loc>
              <lastmod>${item.lastModified.toISOString()}</lastmod>
              <changefreq>${item.changeFrequency}</changefreq>
              <priority>${item.priority}</priority>
            </url>
          `,
      )
      .join('')}
    </urlset>
  `;
}

export async function GET() {
  return new Response(getSitemap(), {
    headers: {
      'Content-Type': 'text/xml',
    },
  });
}

参考(ほぼ翻訳元)

お気持ち

Next.jsは便利だが無駄に複雑だったりこんな感じの「このモードだとサポートしていません。」みたいなことが良くあります。(というように感じます。)
みんながたくさん使うので機能が増えすぎているのでしょう。あまり好きではありません。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?