Skip to content

Instantly share code, notes, and snippets.

@danielimmke
Created January 23, 2024 22:11
Show Gist options
  • Save danielimmke/a13c7a75d584d1a7901cdbb7373e8840 to your computer and use it in GitHub Desktop.
Save danielimmke/a13c7a75d584d1a7901cdbb7373e8840 to your computer and use it in GitHub Desktop.
Example of a dynamic robots.txt that is sensitive to .env
// routes/robots.txt/+server.ts
import { env } from "$env/dynamic/private";
export function GET() {
let rules = [
'User-agent: *'
]
if(env.ORIGIN === 'https://example.com') {
rules.push('Disallow: /*.js$')
rules.push('Disallow: /*.json')
rules.push('Sitemap: https://example.com/sitemap.xml')
} else {
// Disable all indexing for staging (and any other environment, just in case)
rules.push('Disallow: /')
}
const body = rules.join('\n').trim();
return new Response(body,
{
headers: {
'Content-Type': 'text/plain'
}
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment