Created
January 23, 2024 22:11
-
-
Save danielimmke/a13c7a75d584d1a7901cdbb7373e8840 to your computer and use it in GitHub Desktop.
Example of a dynamic robots.txt that is sensitive to .env
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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