What is the purpose of a robots.txt file in a Next.js project?
The robots.txt file tells search engines which pages or sections of your website they can or cannot visit and index. It helps control what appears in search results.
Click to reveal answer
beginner
How do you serve a robots.txt file in a Next.js app?
You can place a robots.txt file inside the public/ folder. Next.js will serve it automatically at /robots.txt when the site runs.
Click to reveal answer
beginner
What does this robots.txt content mean?
User-agent: *
Disallow: /admin
Allow: /
It means all search engines (*) are told not to visit or index any pages under /admin, but they can visit and index all other pages.
Click to reveal answer
intermediate
How can you dynamically generate a robots.txt file in Next.js?
You can create an API route (e.g., /api/robots.txt) that returns the robots.txt content with the correct text/plain header. Then rewrite /robots.txt to this API in next.config.js.
Click to reveal answer
beginner
Why is it important to test your robots.txt file after deployment?
Testing ensures search engines see the correct rules. Mistakes can block important pages from indexing or allow private pages to be indexed, affecting your site's visibility.
Click to reveal answer
Where should you place a static robots.txt file in a Next.js project?
AInside the <code>public/</code> folder
BInside the <code>pages/</code> folder
CInside the <code>components/</code> folder
DInside the <code>api/</code> folder
✗ Incorrect
Next.js serves static files from the public/ folder at the root URL.
What does User-agent: * mean in a robots.txt file?
AIt applies to no search engines
BIt applies only to Google
CIt applies only to Bing
DIt applies to all search engines
✗ Incorrect
* is a wildcard meaning all user agents (search engines).
How do you tell search engines not to index the /private folder?
ABlock: /private
BAllow: /private
CDisallow: /private
DNoindex: /private
✗ Incorrect
Disallow tells search engines not to visit or index that path.
Which HTTP header should be set when serving robots.txt dynamically?
AContent-Type: text/plain
BContent-Type: text/html
CContent-Type: application/json
DContent-Type: application/xml
✗ Incorrect
robots.txt is a plain text file, so text/plain is required.
What happens if you block all pages in robots.txt?
ASearch engines will index all pages
BSearch engines will not index any pages
CSearch engines will index only the homepage
DSearch engines will ignore the file
✗ Incorrect
Blocking all pages stops search engines from indexing your entire site.
Explain how to add a robots.txt file to a Next.js project and what it controls.
Think about where static files go and what search engines read.
You got /4 concepts.
Describe how to create a dynamic robots.txt in Next.js and why you might want to do that.
Consider when your rules might change based on environment or conditions.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a robots.txt file in a Next.js project?
easy
A. To manage server-side rendering
B. To tell search engines which pages to crawl or not crawl
C. To store user data securely
D. To style the website pages
Solution
Step 1: Understand the role of robots.txt
The robots.txt file 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 B
But when you visit /api/robots.txt, the browser downloads a file instead of showing text. What is the likely cause?
medium
A. The API route is correct; browser behavior depends on browser settings
B. The Content-Type header should be 'text/html' not 'text/plain'
C. The browser treats text/plain as a download, so you must add Content-Disposition header
D. Missing semicolons cause syntax error
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 A
Quick Check:
text/plain may download = A [OK]
Hint: text/plain may download depending on browser [OK]
Common Mistakes:
Assuming missing semicolons cause errors
Changing Content-Type to text/html incorrectly
Adding unnecessary headers to force display
5. You want to block all search engines from crawling any page except the homepage and the /blog section in your Next.js app. Which robots.txt content served via API route achieves this correctly?
hard
A. User-agent: *\nDisallow: /\nAllow: /\nAllow: /blog/
B. User-agent: *\nDisallow: /\nAllow: /\nAllow: /blog
C. User-agent: *\nDisallow: /\nAllow: /index.html\nAllow: /blog/
D. User-agent: *\nDisallow: /\nAllow: /$\nAllow: /blog/
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, use Allow: /$ 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 D
Quick Check:
Disallow all except root and /blog = B [OK]
Hint: Use Allow: /$ to allow only homepage [OK]
Common Mistakes:
Allowing '/' allows all pages, not just homepage
Using Allow: /blog without trailing slash misses subpages
Confusing homepage URL with /index.html
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.