-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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