← Back

Set Up Robots.txt Categories for Your Fastly Website

Overview

Use a Fastly Compute service to append Robots.txt Categories to your existing robots.txt file. Please contact us if you need help getting set up.

Step 1: Create a Compute Service

mkdir dark-visitors-robots-txt && cd dark-visitors-robots-txt
fastly compute init
/// <reference types="@fastly/js-compute" />

const FASTLY_CDN_SERVICE_BACKEND_NAME = "cdn_service"
const FASTLY_DARK_VISITORS_API_BACKEND_NAME = "dark_visitors_api"

const DARK_VISITORS_ACCESS_TOKEN = "YOUR_ACCESS_TOKEN" // TODO: Swap in your access token
const ROBOTS_TXT_DISALLOW_PATH = "/"
const ROBOTS_TXT_AGENT_TYPES = [
    // TODO: Add blocked agent types
]

async function handleRequest(request) {
    const url = new URL(request.url)

    if (url.pathname === "/robots.txt") {
        const thisHeaders = new Headers(request.headers)
        thisHeaders.delete("accept-encoding")
        const thisRequest = new Request(request, {
            headers: thisHeaders
        })

        const [thisResponse, thatResponse] = await Promise.all([
            fetch(thisRequest, {
                backend: FASTLY_CDN_SERVICE_BACKEND_NAME
            }),
            fetchRobotsTXT()
        ])

        const [thisRobotsTXT, thatRobotsTXT] = await Promise.all([
            thisResponse.ok ? thisResponse.text() : "",
            thatResponse.ok ? thatResponse.text() : ""
        ])

        const robotsTXT = [
            thisRobotsTXT.trim(),
            "# BEGIN Dark Visitors Managed Content",
            thatRobotsTXT.trim(),
            "# END Dark Visitors Managed Content",
        ].join("\n\n")
    
        return new Response(robotsTXT, {
            headers: {
                "Content-Type": "text/plain"
            },
        })
    }

    return fetch(request, {
        backend: FASTLY_CDN_SERVICE_BACKEND_NAME
    })
}

async function fetchRobotsTXT() {
    return fetch("/robots-txts", {
        backend: FASTLY_DARK_VISITORS_API_BACKEND_NAME,
        method: "POST",
        headers: {
            "Authorization": `Bearer ${DARK_VISITORS_ACCESS_TOKEN}`,
            "Content-Type": "application/json",
        },
        body: JSON.stringify({
            agent_types: ROBOTS_TXT_AGENT_TYPES,
            disallow: ROBOTS_TXT_DISALLOW_PATH,
        }),
    })
}

addEventListener("fetch", (event) =>
    event.respondWith(handleRequest(event.request))
)
fastly compute publish

Step 2: Set Up Service Chaining

Service chaining is how Fastly services connect to each other. This section assumes you don't already have any service chaining set up. If you do, you'll need to make adjustments to account for your existing services. You can see another example in the Fastly tutorial.

First, we need to add the Fastly backends referenced in the code.

fastly backend create --name=dark_visitors_api --address=api.darkvisitors.com --version=latest --autoclone --use-ssl
fastly backend create --name=cdn_service --address=YOUR_CDN_SERVICE_DOMAIN --override-host=YOUR_CDN_SERVICE_DOMAIN --version=latest --autoclone --use-ssl

Next, we need to update the external domain to point to the Compute service, rather than your CDN service.

Step 3: Test Your Integration

If your website is correctly connected, you should see the new rules in your website's robots.txt.