// app/sitemap.ts
import type { MetadataRoute } from "next";
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
const posts = await getPosts();
return [
{ url: "https://example.com", lastModified: new Date() },
...posts.map((p) => ({ url: "https://example.com/blog/" + p.slug })),
];
}
Next.js has special files for sitemaps, robots rules, share images and icons.
app/sitemap.tsexports a function that returns a list of URLs. Each URL can have optionallastModified,changeFrequencyandpriority. Next.js serves it as/sitemap.xml. The function can fetch data, so dynamic pages like blog posts are included.app/robots.tsreturns crawler rules and the sitemap location. Next.js serves it as/robots.txt.opengraph-imageandtwitter-imagefiles in a segment set the share image for social previews. They can be static images, or.tsxfiles that generate an image withImageResponsefromnext/og.iconandapple-iconfiles work the same way for icons.
Static .xml and .txt files are also accepted for the sitemap and robots.
sitemap.tsgenerates/sitemap.xml.robots.tsgenerates/robots.txt.opengraph-imageandtwitter-imagefiles set social share images.- You can generate images in code with
ImageResponse.