async function handleRequest(req) {
// Set up the regular expression
const hostRegex = new RegExp("^(.+?\.){2}fiddle\.", "g");
// Check if the host matches the regular expression
const hostMatch = hostRegex.exec(req.headers.get("Host").toLowerCase());
// Check if the host has a subdomain
if (hostMatch) {
// This host has a subdomain
// Parse the URL from client request
let url = new URL(req.url);
// Alter the URL to a sub-folder
url.pathname = "/subcontent/" + url.pathname;
req = new Request(url, req);
}
const backendResponse = fetch(req, {
backend: "origin_0"
});
return backendResponse;
}
addEventListener("fetch", (event) => event.respondWith(handleRequest(event.request)))