Skip to content

Instantly share code, notes, and snippets.

@derpyherp
Created November 29, 2022 00:52
Show Gist options
  • Save derpyherp/43fc10f3f402d9dcd62bacb930ea15d2 to your computer and use it in GitHub Desktop.
Save derpyherp/43fc10f3f402d9dcd62bacb930ea15d2 to your computer and use it in GitHub Desktop.
addEventListener("fetch", event => {
event.respondWith(redirect(event.request))
})
//localized links to redirect to
const countryMap = {
US: "https://us.foobar.com",
GB: "https://gb.foobar.com",
DE: "https://de.foobar.com",
FR: "https://fr.foobar.com",
ES: "https://es.foobar.com",
}
function redirect(request) {
//get visitor's country from Cloudflare
const country = request.cf.country
if (country != null && country in countryMap) {
const url = countryMap[country]
return Response.redirect(url)
} else {
//redirect all other countries to a default link
const url = 'https://www.foobar.com'
return Response.redirect(url)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment