Skip to content

Instantly share code, notes, and snippets.

@judge2020
Last active May 3, 2019 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save judge2020/dd7b5b92e7ce6526338a39a93e8a361c to your computer and use it in GitHub Desktop.
Save judge2020/dd7b5b92e7ce6526338a39a93e8a361c to your computer and use it in GitHub Desktop.
DO NOT USE THIS. This breaks a lot of things, including the cart.
// Adapted from https://community.cloudflare.com/t/host-product-blog-as-blog-subdirectory-and-proxy-it-from-the-edge/20873?u=judge
addEventListener('fetch', event => {
var url = new URL(event.request.url);
if (url.pathname.startsWith('/shop/') || url.pathname === '/shop') {
event.respondWith(handleShop(event, url));
return;
} else {
event.respondWith(fetch(event.request));
}
})
async function handleShop(event, url) {
var originUrl = url.toString().replace(
'https://judge.sh/shop',
'https://shop.hak5.org');
console.log(originUrl)
var response = await fetch(originUrl, {
headers: event.request.headers,
});
var oldBody = await response.text();
oldBody = oldBody
.replace('shop.hak5.org', 'judge.sh/shop')
.replace('"/', '"https://judge.sh/shop/');
var newResponse = new Response(oldBody,{
status: 200,
statusText: "OK",
headers: response.headers
});
return newResponse;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment