# Dynamic SEO Files

> Generating XML sitemaps programmatically.

Subject: Next.js | Lesson 56 of 65 | 2 min read
Interactive lesson: https://codewithanimation.in/nextjs/dynamic-seo-files
Previous: [generateStaticParams](https://codewithanimation.in/nextjs/generate-static-params.md) | Next: [Image Metadata Files](https://codewithanimation.in/nextjs/image-metadata-files.md)

## sitemap.ts & robots.ts

Instead of manually maintaining static `.xml` or `.txt` files in your `public` folder, Next.js allows you to create special `.ts` files directly in your `app` directory.

For example, placing a `sitemap.ts` file at the root of your `app` directory tells Next.js to execute that function, take the returned array, and output a valid `sitemap.xml` file for search engines.

## Automating SEO

If you run a blog, you don't want to manually edit an XML file every time you publish a new article.

By using `sitemap.ts`, you can connect directly to your database, fetch all published articles, and map them into the required sitemap format. Next.js handles the XML generation.

## Implementation

```tsx
import { MetadataRoute } from 'next';

export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
  const posts = await fetchPosts();
  
  const postUrls = posts.map((post) => ({
    url: `https://acme.com/blog/${post.slug}`,
    lastModified: post.updatedAt,
  }));
  
  return [
    { url: 'https://acme.com', lastModified: new Date() },
    ...postUrls,
  ];
}
```

## Common mistake: The Absolute URL Trap

When using the standard Next.js `<Link>` component, you use relative paths like `/about`.

However, XML Sitemaps strictly require **Absolute URLs** (including the `https://` protocol and domain).

If you accidentally return relative paths from `sitemap.ts`, Next.js will generate the XML, but Google Search Console will reject it as invalid.

## Try it: TypeScript to XML

Examine how a standard TypeScript array of objects is automatically transformed into a valid XML sitemap by the Next.js compiler.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/nextjs/dynamic-seo-files

## Check yourself

### Question 1

What is a strict requirement for the URLs returned by `sitemap.ts`?

- A. They must be relative paths (e.g., '/blog').
- B. They must be absolute URLs (e.g., 'https://acme.com/blog').
- C. They must not include query parameters.
- D. They must be strings, not objects.

Correct answer: B

Unlike Next.js `<Link>` tags, XML Sitemaps are read by external crawler bots that don't know what domain they are on. Therefore, the specification strictly requires full absolute URLs.

- Why not A: Sitemaps require absolute URLs.
- Why not C: Query parameters are allowed if they point to canonical content.
- Why not D: sitemap.ts returns an array of objects.

## Remember this

- `sitemap.ts` automatically generates a `sitemap.xml` route.
- `robots.ts` automatically generates a `robots.txt` route.
- You MUST use absolute URLs (`https://...`) in sitemaps.
- You can dynamically fetch from your database inside these files.
