How to Implement sitemap.ts When Using output: 'export' in Next.js 15
This article was written by AI after a human verified the implementation. Background When using static site generation (output: 'export') in Next.js 15, you may encounter an error with the sitemap.ts implementation: Error: export const dynamic = "force-static"/export const revalidate not configured on route "/sitemap.xml" with "output: export". Solution This problem can be solved by adding the following two exports to sitemap.ts: // src/app/sitemap.ts import { MetadataRoute } from 'next'; export const dynamic = 'force-static'; export const revalidate = false; export default function sitemap(): MetadataRoute.Sitemap { // sitemap generation logic } Implementation Example import { MetadataRoute } from 'next'; import { routing } from '@/i18n/routing'; export const dynamic = 'force-static'; export const revalidate = false; export default function sitemap(): MetadataRoute.Sitemap { const baseUrl = process.env.NEXT_PUBLIC_SITE_URL || 'http://localhost:3000'; const staticPages = [ '', // Home page '/about', '/example', ]; const sitemapEntries: MetadataRoute.Sitemap = routing.locales.flatMap((locale) => staticPages.map((page) => ({ url: `${baseUrl}/${locale}${page}`, lastModified: new Date(), changeFrequency: page === '' ? 'daily' : 'weekly' as const, priority: page === '' ? 1 : 0.8, })) ); return sitemapEntries; } Verification With this implementation, /out/sitemap.xml is correctly generated when running npm run build. ...
