Pattern · TypeScript
robots.txt and robots metadata in Next.js App Router
Static robots via app/robots.ts and per-route noindex via generateMetadata.
Level: beginnerTime estimate: ~25 min
Global rules live in robots.ts; draft or utility pages opt out via route metadata.
- Do not send conflicting signals between robots.txt and meta robots
- the sitemap URL in robots must be canonical
- preview deployments often need full noindex
Crawlers read both
robots.txtand<meta name="robots">. Split responsibilities: the file for site-wide rules, metadata for pages that must not be indexed.
Code
app/robots.ts in this project:
import type { MetadataRoute } from 'next'
import { getSiteUrl } from '@/lib/site'
const host = getSiteUrl().replace(/\/$/, '')
export default function robots(): MetadataRoute.Robots {
return {
rules: [
{ userAgent: '*', allow: '/' },
{ userAgent: 'GPTBot', allow: '/' },
{ userAgent: 'Google-Extended', allow: '/' },
{ userAgent: 'CCBot', allow: '/' },
{ userAgent: 'anthropic-ai', allow: '/' },
{ userAgent: 'Claude-Web', allow: '/' },
{ userAgent: 'ClaudeBot', allow: '/' },
{ userAgent: 'PerplexityBot', allow: '/' },
{ userAgent: 'Applebot-Extended', allow: '/' },
],
sitemap: `${host}/sitemap.xml`,
host,
}
}
Per-route noindex for a draft:
import type { Metadata } from 'next'
export async function generateMetadata(): Promise<Metadata> {
return {
title: 'Draft',
robots: { index: false, follow: false },
}
}
Verification
GET /robots.txtreturns plain text withUser-agentrules, optionalDisallow, and a singleSitemap:line with the canonical HTTPS sitemap URL.- Excluded pages expose
noindexin HTML and are omitted from the sitemap when the policy is full exclusion.
Sources
More patterns
- Next.js
Canonical URL and page metadata in Next.js App Router
A generateMetadata pattern with canonical and Open Graph — baseline SEO hygiene.
Open pattern - Indexing
Dynamic sitemap.xml in Next.js (app/sitemap.ts)
One sitemap with static and content URLs; aligned with collectIndexableUrls and canonical URLs.
Open pattern
Blog posts
- SEO
SEO architecture: why a site does not sell without structure
How the right site structure affects sales and rankings. Why design comes second and semantics first.
Read article - Process
Site handoff checklist: speed, accessibility, and baseline tech SEO before you sign
Why to look at PageSpeed Insights and WAVE before sign-off, how lab metrics differ from field data, and a minimum tech SEO bar that reduces post-launch surprises.
Read article
Need this implemented for your domain and stack?
Short form: name, phone, and site. After you submit, we reply with next steps and a phase outline; details are refined on a call.