Skip to content

Instantly share code, notes, and snippets.

@tmf
Created July 10, 2019 14:02
Show Gist options
  • Save tmf/d032ab54c16b875b0de749cfd524c271 to your computer and use it in GitHub Desktop.
Save tmf/d032ab54c16b875b0de749cfd524c271 to your computer and use it in GitHub Desktop.
cloudflare worker
addEventListener("fetch", event => {
if (event.request.method === "GET") {
return event.respondWith(handleGet(event));
}
// fallback for non-GET requests
return event.respondWith(fetch(event.request));
});
async function fetchBucket(host, path) {
let lastSegment = path.substring(path.lastIndexOf('/'))
if (lastSegment.indexOf('.') === -1) {
path += 'index.html'
}
const url = `https://${host}${path}`;
// fetch by URL string
return fetch(url);
}
async function handleGet(event) {
const request = event.request;
const url = new URL(request.url);
if (url.hostname === "devbox-groups-2.kubernetes.doodle-test.com") {
const pathname = url.pathname;
const segments = pathname.split("/");
if (
pathname === "/" ||
pathname.startsWith("/static/") ||
pathname.startsWith("/product/") ||
(segments.length === 2 && segments[1].endsWith(".js"))
) {
const targetUrl = `https://static-site-staging.doodle-test.com${pathname}`
return fetch(targetUrl);
}
}
// fallback for other hosts
return fetch(event.request);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment