Static crawler policy
A site has stable crawler policy and a fixed sitemap URL.
Next.js 16+ · Directive
Next.js can serve robots.txt from app/robots.txt or generate it with app/robots.ts or app/robots.js. Generated files return a Robots object describing crawler rules and optional sitemap or host metadata.
Place robots.ts or robots.js at the app root and return a Robots object. Next.js generates the /robots.txt response from its rules, sitemap, and optional host fields; the route is cached unless request-time APIs or dynamic configuration make it dynamic.
export default function robots(): MetadataRoute.Robots| Name | Type | Description |
|---|---|---|
rules | RobotsRule | RobotsRule[] | A rule object or array of rule objects controlling crawler access; each rule may include userAgent, allow, disallow, crawlDelay, and other. |
sitemap | string | URL | (string | URL)[] | The sitemap URL or URLs advertised to crawlers. |
host | string | URL | The preferred host URL, when applicable. |
A Robots object that Next.js serializes and serves as /robots.txt.
import type { MetadataRoute } from 'next'
export default function robots(): MetadataRoute.Robots {
return {
rules: {
userAgent: '*',
allow: '/',
disallow: '/private/',
},
sitemap: 'https://acme.com/sitemap.xml',
}
}Save this as app/robots.ts; Next.js exposes the generated robots file at /robots.txt.
A site has stable crawler policy and a fixed sitemap URL.
A site needs rules that differ by search-engine user agent.
A site must add crawler-specific directives supported outside the standard.
A site derives crawler rules or sitemap values from application configuration.
Create app/robots.ts or app/robots.js and export a default function returning MetadataRoute.Robots. Next.js turns that object into the site's robots.txt file.
Follow-up: How would you configure different crawlers? Return rules as an array, assigning each entry its own userAgent value and allow or disallow paths.
The rules property accepts either one rule object or an array of rule objects. A rule can specify userAgent, allow, disallow, and crawlDelay; sitemap and host are top-level fields.
Follow-up: How do you emit a non-standard directive for one crawler? Put it in that rule's other field; keys retain their casing and array values produce separate lines.
A static app/robots.txt file can directly contain directives such as User-Agent, Allow, Disallow, and Sitemap. Use robots.ts or robots.js when the response must be generated from code.
Follow-up: What changed in the current API? Next.js 16.3.0 added the other field for non-standard per-agent directives.
MetadataRoute.RobotsMetadataRoute.RobotsRuleapp/robots.txtapp/robots.jsapp/robots.ts