async function handleRequest(req) {
  // Get the user agent from the request header and make it lowercase
  const userAgent = req.headers.get("user-agent").toLowerCase();

  // Check if that user agent is in the list of lowercased bad user agents
  const badUserAgents = ["badbot", "spambot"];
  if (badUserAgents.some(basUserAgent => userAgent.includes(basUserAgent))) {
    // Bad user agent found, return a 403 error
    return new Response("Forbidden", {
       status: 403
      }
    );
  }

  // If not blocked, let the request pass through to the origin
  const backendResponse = fetch(req, {
    backend: "origin_0"
  });

  return backendResponse;
}

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