Skip to content
Styling & OptimizationChapter 29 / 34Intermediate5 min read

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 filesMetadata files from code
sitemap.xml updated manuallyThe list comes from the database on each request or build
A new page is missing from the sitemapEvery new record is included automatically
A typo in robots.txtA typed object, so a wrong key gives an error
A designer makes each post's OG imageEach post's image is generated from JSX
Write the og:image tag by handNext.js adds the tags itself

3. Where is it used?

FileURLPurpose
app/sitemap.ts/sitemap.xmlList of all public pages
app/robots.ts/robots.txtWhich paths to crawl, and where the sitemap is
app/opengraph-image.tsxThe route's OG imagePreview on WhatsApp, LinkedIn, X
app/blog/[slug]/opengraph-image.tsxA separate image for each postA dynamic image with the title
twitter-image.tsxA separate image for XOnly if you need it

4. How does it work?

The journey of a crawler
The crawler reads /robots.txt
It gets the rules from robots.ts and the sitemap URL
It gets the list of pages from /sitemap.xml
Each page has an og:image tag in its head
The PNG image is generated by opengraph-image.tsx

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

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

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

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?

ItemMeaning
MetadataRoute.SitemapThe return type, every entry needs a url
lastModifiedTells the search engine when the page changed
disallowA request not to crawl these paths
sitemap (in robots)Tells the crawler where the sitemap is
ImageResponseTurns JSX and inline styles into a PNG
size, alt, contentTypeNext.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

Final quick revision

PointDetail
Sitemap/sitemap.xml from app/sitemap.ts
Robots/robots.txt from app/robots.ts
OG imageopengraph-image.tsx and ImageResponse from next/og
Image size1200 x 630 recommended
URLsAlways absolute