Free auditStart free
GuideNext.js · Crawling

Next.js robots.txt

Generate robots.txt in the Next.js App Router, add crawler-specific rules safely, and test the live route and indexing behavior.

4 min
read
5
sources
Oct 24, 2026
updated
Scaplet Editorial Team
author
In one line

For the Next.js App Router, put a static robots.txt in app/ or generate it with app/robots.ts returning MetadataRoute.Robots. The generated route is cached by default. Use robots.txt to manage crawl access, not to hide a page from Google: a blocked URL may still appear in results, while noindex requires the crawler to fetch the page.

30-second check

Where does your site stand?

Four quick questions, then a straight answer.

1/4Have you checked your own site for this?
Summarize this page withChatGPTClaudePerplexityGoogleAI Mode
01Check it

Choose static rules or a generated route

The App Router recognizes app/robots.txt as a static file and app/robots.ts as a generated metadata route. Use the static file when crawler rules are stable. Use TypeScript when the sitemap URL or rule set must be generated from application configuration. Next.js exposes the result at /robots.txt. The robots file convention was introduced in Next.js 13.3.0, and the other field for non-standard directives such as Yandex Clean-param arrived in 16.3.0.

Replace the example host with the production canonical host and keep paths as root-relative prefixes. MetadataRoute.Robots accepts one rules object or an array of bot-specific rules, plus sitemap and host fields. A generated robots.ts route is cached by default unless you opt into request-time behavior or dynamic configuration.

app/robots.ts
import type { MetadataRoute } from 'next'

export default function robots(): MetadataRoute.Robots {
  return {
    rules: {
      userAgent: '*',
      allow: '/',
      disallow: ['/private/', '/preview/'],
    },
    sitemap: 'https://example.com/sitemap.xml',
  }
}
02Worth knowing

Add specific crawler rules only when needed

Most sites can keep one wildcard group. Next.js supports per-user-agent rules when you have a clear reason to treat a crawler differently. Be careful: Google says only one group is valid for a crawler, the one with the most specific matching user agent, and other groups are ignored. In the example below, Googlebot follows its own group only, so /private/ is not blocked for it. Repeat shared rules in each group, and check the documentation of each crawler you target.

Do not disallow assets that a page needs to render

If Google cannot fetch important styles or scripts, it may not understand the page correctly.

Keep staging controls out of production by design

Use deployment configuration and access controls for private previews; a robots rule alone is not authentication.

Avoid listing redirecting URLs in the sitemap line

Point to the deployed canonical sitemap path.

Check the exact production hostname

Google says robots.txt rules apply only to the host, protocol, and port where the file is hosted, so rules at example.com do not govern a separate subdomain.

Keep the file small

Google enforces a 500 KiB limit and ignores content after it. It generally caches robots.txt for up to 24 hours, so a change is not instant.

Separate Googlebot access from another crawler
import type { MetadataRoute } from 'next'

export default function robots(): MetadataRoute.Robots {
  return {
    rules: [
      { userAgent: '*', allow: '/', disallow: '/private/' },
      { userAgent: 'Googlebot', allow: '/', disallow: '/preview/' },
    ],
    sitemap: 'https://example.com/sitemap.xml',
  }
}
03Use the control that matches your goal

Crawl access is not indexing control

Google states that robots.txt is not a mechanism for keeping a web page out of Google. A disallowed URL can still appear in results if other pages link to it, but the result will not have a description. If a public page must stay out of Google results, allow crawling and send a noindex directive, or protect the page with authentication.
Goal

Reduce crawler requests to a path

Use
robots.txt Disallow
Important limit
A URL can still be indexed
Goal

Keep a page out of Google results

Use
noindex meta tag or X-Robots-Tag
Important limit
Google must crawl the page to read the directive
Goal

Keep private content private

Use
Authentication or access control
Important limit
Robots rules are public and not access protection
Goal

Help crawlers find canonical URLs

Use
Sitemap and internal links
Important limit
Neither guarantees indexing
04How it works

Test the generated file after deployment

  1. 1

    Open the public endpoint

    Visit https://example.com/robots.txt on the production hostname. Confirm it returns the intended plain-text rules and sitemap URL.

  2. 2

    Check for conflicting sources

    Confirm which file produces the live output: app/robots.txt, app/robots.ts, or in a Pages Router app public/robots.txt. A CDN or proxy can also serve a different response, so inspect the actual response instead of trusting the source file alone.

  3. 3

    Test representative paths

    Check that an important page is allowed, a private or preview path is blocked if intended, and required assets remain available to crawlers.

  4. 4

    Check indexing separately

    Use URL Inspection in Search Console to see how Google treats a specific page. Crawl access and indexing are different questions: an allowed URL is not necessarily indexed.

  5. 5

    Retest after config changes

    If rules depend on environment variables or deployment mode, check production after publishing. The generated route's default cache behavior means runtime changes may not appear until its cache conditions are addressed.

For the corresponding sitemap route, see Next.js XML sitemap and the Next.js redirects guide. For Google-specific behavior, read the robots.txt glossary and Googlebot guide.

Questions

Asked a lot.

3 questions people ask next, answered from the sources on this page.

01Where do I put robots.txt in Next.js?

For the App Router, add app/robots.txt for static rules or app/robots.ts that returns MetadataRoute.Robots for generated rules.

02Why is my Next.js robots.ts change not live?

Next.js documents the generated robots route as cached by default. Check the production response and whether your implementation uses request-time APIs or dynamic configuration.

03Can robots.txt stop a Next.js page from being indexed?

Not reliably. Google can index a blocked URL it discovers elsewhere. To keep a public page out of results, let Google crawl it and read a noindex directive, or require authentication for private content.

Sources · 5

Every fact, linked and dated.

Official docs first. The date is when we last opened each page.
  • nextjs.org
  • google.com
  1. 01robots.txt | Next.js Documentationnextjs.org · checked 2026-10-24
  2. 02Introduction to robots.txt | Google Search Centraldevelopers.google.com · checked 2026-10-24
  3. 03How Google interprets the robots.txt specification | Google Search Centraldevelopers.google.com · checked 2026-10-24
  4. 04Block Search indexing with noindex | Google Search Centraldevelopers.google.com · checked 2026-10-24
  5. 05Googlebot | Google Search Centraldevelopers.google.com · checked 2026-10-24
Free · 2 minutes

Tools point. Agencies advise. This one ships.

Run the free audit, then approve the fixes. Every change can be undone.

Free, no card60 secondsNobody will call you
  • 46 fixes shipped without a ticket Brightpipe
  • 0→7 ChatGPT citations in six weeks Paylane