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.
Notes
dynamic = 'force-static'alone is not sufficient;revalidate = falseis also required- Environment variables and imported modules can be used
- Dynamic values can be used as long as they are statically resolved at build time
Alternatives
If the above method does not work, the following alternatives are available:
- Generate sitemap.xml with a build script
- Place a static sitemap.xml in the public directory
Summary
Even when using output: 'export' in Next.js 15, sitemap generation using sitemap.ts is possible by adding the appropriate configuration. This method allows you to leverage Next.js standard features while maintaining type-safe sitemap generation.