Skip to content

robots.txt

robots.txt is a file that tells search engine crawlers which pages they can and cannot access. It’s placed at the root of your site and is checked by crawlers before they index your pages.

Without robots.txt, crawlers will try to index every page on your site, including:

  • Admin pages (/admin, /dashboard)
  • API routes (/api/*)
  • Staging or test pages
  • Search results pages (which could be infinite)

robots.txt prevents wasted crawl budget on pages that shouldn’t appear in search results.

Next.js can serve a static robots.txt file from the public directory:

public/robots.txt
User-agent: *
Allow: /
Disallow: /admin/
Disallow: /api/
Disallow: /dashboard/
Sitemap: https://yourdomain.com/sitemap.xml

For more control, use a Route Handler:

app/robots.ts
import type { MetadataRoute } from 'next'
export default function robots(): MetadataRoute.Robots {
return {
rules: {
userAgent: '*',
allow: '/',
disallow: ['/admin/', '/api/', '/dashboard/'],
},
sitemap: 'https://yourdomain.com/sitemap.xml',
}
}
export default function robots(): MetadataRoute.Robots {
const baseUrl = process.env.NEXT_PUBLIC_BASE_URL || 'https://yourdomain.com'
// Block all crawlers on preview deployments
if (process.env.VERCEL_ENV === 'preview') {
return {
rules: {
userAgent: '*',
disallow: '/',
},
}
}
return {
rules: {
userAgent: '*',
allow: '/',
disallow: ['/admin/', '/api/'],
},
sitemap: `${baseUrl}/sitemap.xml`,
}
}
DirectiveExampleEffect
AllowAllow: /public/Allow crawling a specific path
DisallowDisallow: /admin/Block crawling a path
User-agentUser-agent: GooglebotApply rules to specific crawlers
SitemapSitemap: https://...Point to your sitemap
Crawl-delayCrawl-delay: 10Request delay between crawls
Terminal window
curl https://yourdomain.com/robots.txt

Also test in Google Search Console’s robots.txt tester.

  • Blocking CSS and JS files — This prevents crawlers from rendering your page properly.
  • Using Disallow: / on production — This blocks all crawling. Only use on staging/preview.
  • Forgetting the sitemap URL — Include Sitemap: directive to help crawlers find your sitemap.
  • Not handling preview deployments — Preview URLs should block crawling to prevent duplicate content.
  • Always include a robots.txt file
  • Block admin, API, and internal pages from crawling
  • Include the sitemap URL in robots.txt
  • Block crawlers on preview/staging environments
  • Don’t block CSS, JS, or image files

robots.txt controls which parts of your site search engines can crawl. Block admin and API routes, include your sitemap URL, and use environment-specific rules for preview deployments. Test with curl and Google Search Console.