Skip to content

robots.txt

A /robots.txt endpoint with your sitemap's address and the paths to keep out of search.

Native .astro · no React · MIT · free, no account

Install

Terminal
npx shadcn@latest add https://www.themes.dev/astro/r/robots-txt.json

The CLI needs a components.json (from npx shadcn init), which also writes the tokens these files use. No CLI? Copy the files below to the same paths.

Files

src/pages/robots.txt.tsTypeScript
// /robots.txt with the sitemap's address (from @astrojs/sitemap) and the paths to keep out of search.import type { APIContext } from 'astro'; const disallow = ['/thanks/', '/404']; export function GET(context: APIContext) {  const lines = ['User-agent: *', ...disallow.map((p) => `Disallow: ${p}`), '', `Sitemap: ${new URL('/sitemap-index.xml', context.site).href}`, ''];  return new Response(lines.join('\n'), { headers: { 'Content-Type': 'text/plain; charset=utf-8' } });}