Sitemap and robots
What you need to know
A sitemap describes public URLs that a site wants crawlers to discover. robots.txt supplies crawler instructions; neither file protects confidential pages.
By the end, you can: list only public canonical routes; distinguish crawl instructions and access controls.
A public catalog sitemap
import type {MetadataRoute} from 'next';export default function sitemap():MetadataRoute.Sitemap { return ['river-walks','forest-notes'].map(slug=>({ url:`https://books.example/en/catalog/${slug}` }));}Expected behavior: app/sitemap.ts should serve two public absolute URLs at /sitemap.xml.
How it works
In App Router use a root sitemap.ts with a default export returning MetadataRoute.Sitemap and a root robots.ts returning MetadataRoute.Robots. Build sitemap entries from validated public canonical records, not from every database row. Use absolute URLs and include lastModified only when supported by real content revision dates. Exclude drafts, account pages and noncanonical tracking variants. The special metadata routes have framework caching behavior; verify freshness after publication changes and avoid promising immediate crawler updates. For large inventories, generateSitemaps is a separate sharding API whose exact signature and URL convention must be checked for the selected release. robots rules can guide compliant crawlers, not prevent visitors or other clients from requesting a URL. Never list secret routes in these files merely to hide them. The example uses a reserved fictional origin and must not be submitted to a search service. In production inspect XML, content type, status, unique URLs and representative page responses.
Crawler instructions, not access control
import type {MetadataRoute} from 'next';export default function robots():MetadataRoute.Robots { return {rules:{userAgent:'*',allow:'/'},sitemap:'https://books.example/sitemap.xml'};}Expected behavior: app/robots.ts advertises a sitemap and permits crawling. Private routes still require authentication.
Common mistake
Do not include drafts, query duplicates or fake modification timestamps. Do not treat a Disallow line as a permission check.
Practice and verification
Add a draft row and a duplicate tracking URL to a synthetic inventory. Generate only two canonical entries, then check XML and content type when the app is running. Check private access independently.
Scope: Proposed Next.js 16.x / React 19, Cache Components disabled unless noted. Framework and browser examples are staged; local standalone tests are reported separately. Exact dependency patches and platform contracts pending.
Further reading: Sitemap · Robots · Meta Files