Sitemap, Robots & OG Images
Next.js sitemap, robots and opengraph files - use app/sitemap.ts, app/robots.ts and ImageResponse to build dynamic OG images with simple examples.
On this page
1. What are sitemap, robots and OG images?
You write these files in TypeScript, and Next.js turns them into /sitemap.xml, /robots.txt and an OG image URL, and adds the needed head tags for you.
2. Why does it matter?
It is easy to forget to update a static sitemap.xml when new blog posts are added. Designing a separate share image in Figma for every post is not practical either.
| Hand-made files | Metadata files from code |
|---|---|
| sitemap.xml updated manually | The list comes from the database on each request or build |
| A new page is missing from the sitemap | Every new record is included automatically |
| A typo in robots.txt | A typed object, so a wrong key gives an error |
| A designer makes each post's OG image | Each post's image is generated from JSX |
| Write the og:image tag by hand | Next.js adds the tags itself |
3. Where is it used?
| File | URL | Purpose |
|---|---|---|
app/sitemap.ts | /sitemap.xml | List of all public pages |
app/robots.ts | /robots.txt | Which paths to crawl, and where the sitemap is |
app/opengraph-image.tsx | The route's OG image | Preview on WhatsApp, LinkedIn, X |
app/blog/[slug]/opengraph-image.tsx | A separate image for each post | A dynamic image with the title |
twitter-image.tsx | A separate image for X | Only if you need it |
4. How does it work?
These files are special Route Handlers. By default they are static and cached at build time, unless you use request-time APIs or uncached data.
5. Code example
app/sitemap.ts
import type { MetadataRoute } from "next";
import { getAllPosts } from "@/lib/posts";
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
const posts = await getAllPosts();
const postEntries = posts.map((post) => ({
url: `https://znpedia.com/blog/${post.slug}`,
lastModified: post.updatedAt,
changeFrequency: "weekly" as const,
priority: 0.7,
}));
return [
{ url: "https://znpedia.com", lastModified: new Date(), priority: 1 },
...postEntries,
];
}app/robots.ts
import type { MetadataRoute } from "next";
export default function robots(): MetadataRoute.Robots {
return {
rules: {
userAgent: "*",
allow: "/",
disallow: ["/admin/", "/api/"],
},
sitemap: "https://znpedia.com/sitemap.xml",
};
}app/blog/[slug]/opengraph-image.tsx
import { ImageResponse } from "next/og";
import { getPost } from "@/lib/posts";
export const alt = "ZnPedia blog post";
export const size = { width: 1200, height: 630 };
export const contentType = "image/png";
export default async function Image({ params }: { params: Promise<{ slug: string }> }) {
const { slug } = await params;
const post = await getPost(slug);
return new ImageResponse(
(
<div
style={{
width: "100%",
height: "100%",
display: "flex",
alignItems: "center",
padding: 80,
background: "#0f766e",
color: "white",
fontSize: 64,
}}
>
{post.title}
</div>
),
size
);
}How does the code work?
| Item | Meaning |
|---|---|
MetadataRoute.Sitemap | The return type, every entry needs a url |
lastModified | Tells the search engine when the page changed |
disallow | A request not to crawl these paths |
sitemap (in robots) | Tells the crawler where the sitemap is |
ImageResponse | Turns JSX and inline styles into a PNG |
size, alt, contentType | Next.js uses these to create tags like og:image:width and og:image:alt |
6. Common mistakes
Mistake: Using relative URLs in the sitemap
Why? The sitemap protocol requires absolute URLs, and search engines reject a URL like /blog/post.
Better: Give the full URL, keeping the domain in a constant or an environment variable.
Mistake: Treating robots disallow as security
Why? robots.txt is only a request to well-behaved crawlers, and any user can still open that path.
Better: Protect private pages with authentication, robots is only for crawling.
Mistake: Using normal CSS or grid in ImageResponse
Why? ImageResponse supports only flexbox and a subset of CSS, so Tailwind classes and display grid do not work.
Better: Use inline style objects and display flex, and keep the image at 1200 x 630.
Interview tip
"Can Google index a page that is blocked with disallow in robots.txt?" Yes, if other sites link to it. To keep it out of the index, the page needs a noindex meta tag (robots: { index: false } in the Metadata API), and the page must be allowed to be crawled so Google can read that tag.
7. What to learn before and after
| Stage | Chapters |
|---|---|
| Before | Metadata & SEO, Route Handlers (API), Dynamic Routes |
| Alongside | Image Optimization |
| After | Performance & Core Web Vitals, Deployment |
Final quick revision
| Point | Detail |
|---|---|
| Sitemap | /sitemap.xml from app/sitemap.ts |
| Robots | /robots.txt from app/robots.ts |
| OG image | opengraph-image.tsx and ImageResponse from next/og |
| Image size | 1200 x 630 recommended |
| URLs | Always absolute |