The robots.txt file tells search engines which pages to look at or ignore on your website. It helps control what shows up in search results.
Robots.txt configuration in NextJS
User-agent: [name] Disallow: [path] Allow: [path] Sitemap: [url]
User-agent specifies which search engine bot the rule applies to.
Disallow tells bots not to visit certain pages or folders.
/private/ folder.User-agent: * Disallow: /private/
/no-google/ folder.User-agent: Googlebot Disallow: /no-google/
/public/ folder for all bots.User-agent: * Allow: /public/ Disallow: /
This Next.js route returns a robots.txt file that blocks all bots from the /admin/ folder but allows crawling everywhere else. It also provides the sitemap URL.
import { NextResponse } from 'next/server'; export function GET() { const robots = `User-agent: * Disallow: /admin/ Allow: / Sitemap: https://example.com/sitemap.xml`; return new NextResponse(robots, { status: 200, headers: { 'Content-Type': 'text/plain' } }); }
Place the robots.txt file at the root of your website for search engines to find it.
Use Next.js API routes or middleware to serve robots.txt dynamically if needed.
Test your robots.txt using Google Search Console to ensure it works as expected.
Robots.txt controls what search engines can see on your site.
You can block or allow specific pages or bots.
In Next.js, you can serve robots.txt using an API route returning plain text.
Practice
robots.txt file in a Next.js project?Solution
Step 1: Understand the role of robots.txt
Therobots.txtfile is used to communicate with search engines about which parts of the website they can access.Step 2: Identify its purpose in Next.js
In Next.js, it is used the same way as in other web projects: to allow or block search engine crawlers from certain pages.Final Answer:
To tell search engines which pages to crawl or not crawl -> Option BQuick Check:
robots.txt controls crawler access = D [OK]
- Confusing robots.txt with CSS or styling files
- Thinking robots.txt stores user data
- Assuming robots.txt manages rendering
robots.txt file using an API route in Next.js?Solution
Step 1: Check the content type for robots.txt
Therobots.txtfile must be served as plain text, so the header should betext/plain.Step 2: Verify the response content format
The content must be plain text lines, not JSON or HTML, so the string with newlines is correct.Final Answer:
export default function handler(req, res) { res.setHeader('Content-Type', 'text/plain'); res.send('User-agent: *\nDisallow: /private'); } -> Option CQuick Check:
robots.txt served as plain text = A [OK]
- Using JSON or HTML content type instead of text/plain
- Sending JSON object instead of plain text string
- Forgetting to set Content-Type header
robots.txt:
export default function handler(req, res) {
res.setHeader('Content-Type', 'text/plain');
res.send(`User-agent: *\nDisallow: /admin\nAllow: /public`);
}
What will search engines do when they read this robots.txt?Solution
Step 1: Read the robots.txt rules
The file saysUser-agent: *(all bots),Disallow: /admin(block /admin), andAllow: /public(allow /public).Step 2: Understand the effect on crawling
Search engines will not crawl /admin but will crawl /public and other pages not disallowed.Final Answer:
They will block crawling of /admin but allow /public and other pages -> Option AQuick Check:
Disallow /admin, Allow /public = C [OK]
- Thinking Allow overrides Disallow globally
- Assuming all pages are blocked
- Confusing syntax causing ignored file
robots.txt:
export default function handler(req, res) {
res.setHeader('Content-Type', 'text/plain')
res.send('User-agent: *\nDisallow: /secret')
}
But when you visit /api/robots.txt, the browser downloads a file instead of showing text. What is the likely cause?Solution
Step 1: Check the code syntax
The code is valid JavaScript; semicolons are optional in this context and do not cause errors.Step 2: Understand browser behavior for text/plain
Browsers may display or download text/plain files depending on settings; this is normal and not an error in the API.Final Answer:
The API route is correct; browser behavior depends on browser settings -> Option AQuick Check:
text/plain may download = A [OK]
- Assuming missing semicolons cause errors
- Changing Content-Type to text/html incorrectly
- Adding unnecessary headers to force display
robots.txt content served via API route achieves this correctly?Solution
Step 1: Understand the goal
Block all pages except homepage and /blog section. Homepage is usually root path '/' only.Step 2: Use correct syntax for homepage allowance
To allow only the homepage, useAllow: /$which matches exactly the root URL, not all paths starting with '/'.Step 3: Allow /blog/ section
Allowing/blog/lets crawlers access all pages under /blog.Final Answer:
User-agent: *\nDisallow: /\nAllow: /$\nAllow: /blog/ -> Option DQuick Check:
Disallow all except root and /blog = B [OK]
- Allowing '/' allows all pages, not just homepage
- Using Allow: /blog without trailing slash misses subpages
- Confusing homepage URL with /index.html
Ready to prove it?
Pass the test and this pattern moves unknown
