Create app/sitemap.ts and app/robots.ts that return MetadataRoute objects, set metadataBase and alternates.canonical in the Metadata API, verify the domain in Search Console by DNS (or by verification.google for a URL-prefix property), then submit /sitemap.xml in the Sitemaps report. Next.js serves the two files at /sitemap.xml and /robots.txt automatically.
File to URL map
| File | Public URL | Notes |
|---|---|---|
app/sitemap.ts | /sitemap.xml | Returns MetadataRoute.Sitemap. Cached by default unless it uses request-time APIs |
app/robots.ts | /robots.txt | Returns MetadataRoute.Robots |
app/product/sitemap.ts with generateSitemaps | /product/sitemap/1.xml and siblings | For splitting past Google's 50,000 URL limit per sitemap |
public/llms.txt | /llms.txt | Static file, served from the base URL |
app/llms.txt/route.ts | /llms.txt | Route Handler, for generated content |
sitemap.ts
import type { MetadataRoute } from 'next'
export default function sitemap(): MetadataRoute.Sitemap {
return [
{ url: 'https://acme.com', lastModified: new Date() },
{ url: 'https://acme.com/about', lastModified: '2026-10-01' },
]
}
The type allows changeFrequency and priority too. Google's sitemap guide says it ignores both, and that it uses lastmod only when values are consistently and verifiably accurate. Writing new Date() for every page on every build teaches Google to distrust the field. Pull the date from your CMS or file history instead. Check the output with the sitemap checker.
robots.ts
import type { MetadataRoute } from 'next'
export default function robots(): MetadataRoute.Robots {
return {
rules: { userAgent: '*', allow: '/', disallow: '/private/' },
sitemap: 'https://acme.com/sitemap.xml',
}
}
This renders User-Agent: *, Allow: /, Disallow: /private/ and a Sitemap: line. Remember that robots.txt controls crawling, not indexing; see noindex vs robots.txt vs canonical.
Titles, canonical and noindex with the Metadata API
// app/layout.tsx
import type { Metadata } from 'next'
export const metadata: Metadata = {
metadataBase: new URL('https://acme.com'),
title: { template: '%s | Acme', default: 'Acme' },
robots: process.env.APP_ENV === 'production'
? { index: true, follow: true }
: { index: false, follow: false },
}
// app/pricing/page.tsx
export const metadata: Metadata = {
title: 'Pricing',
alternates: { canonical: '/pricing' },
}
Per the docs, alternates.canonical with metadataBase outputs <link rel="canonical" href="https://acme.com/pricing">, and robots: { index: false } outputs a robots meta tag. APP_ENV is our placeholder variable; use whichever your host provides to tell production from preview. Two traps from the docs: title.template does not apply to the segment that defines it, and nested fields such as robots are replaced, not merged, by a later segment. A page that sets its own robots discards the layout's preview rule. Verify with the noindex checker.
Verify the site in Search Console
- Prefer a Domain property and verify it with a DNS record. Google's help page says DNS is the only method for Domain properties, and the record must stay in place.
- If you only control the code, add a URL-prefix property and use the HTML tag method. In Next.js that is one field:
export const metadata: Metadata = {
verification: { google: 'your-token-from-search-console' },
}
This renders <meta name="google-site-verification" content="...">. Google requires the tag on a homepage that does not need login, and it must remain. Then open Sitemaps in Search Console and submit sitemap.xml. Google says submission is only a hint. If it fails, see Sitemap could not fetch. WordPress readers can use the WordPress guide.
Serving /llms.txt
The public folder serves files from the base URL, so public/llms.txt answers at /llms.txt. If the content is generated, a Route Handler can return it:
// app/llms.txt/route.ts
export async function GET() {
return new Response('# Acme\n\n- [Docs](https://acme.com/docs)\n', {
headers: { 'Content-Type': 'text/plain; charset=utf-8' },
})
}
The route.js docs use the same new Response(body, { headers }) pattern for non-UI output. To check what the file contains and whether it parses, use the llms.txt checker.
Questions people ask
Do I need priority and changeFrequency in sitemap.ts?
No. Next.js accepts them as optional fields, but Google's sitemap documentation says Google ignores priority and changefreq. Set lastModified only if it reflects a real content change.
Why does the build fail with a relative canonical?
Next.js documents that a relative path in a URL-based metadata field without metadataBase causes a build error. Set metadataBase in the root layout.
Which Search Console verification works for a Domain property?
Only DNS. Google's help page calls the domain name provider method the only way to verify a Domain property. The verification meta tag works for a URL-prefix property.
Should robots.txt go in the public folder?
The Next.js public folder page tells you to use the special metadata files inside the app folder for robots.txt, so use app/robots.ts or a static app/robots.txt. Keep public for files such as llms.txt.
Related
Sources
- Next.js docs: sitemap.xml file convention, accessed 2026-11-12.
- Next.js docs: robots.txt file convention, accessed 2026-11-12.
- Next.js docs: generateMetadata (alternates, robots, verification, metadataBase), accessed 2026-11-12.
- Next.js docs: public folder, accessed 2026-11-12.
- Next.js docs: route.js (Route Handlers), accessed 2026-11-12.
- Google Search Central: Build and submit a sitemap, accessed 2026-11-12.
- Search Console Help: Verify your site ownership, accessed 2026-11-12.