Skip to content

Instantly share code, notes, and snippets.

@luandevpro
Created August 18, 2019 03:39
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 luandevpro/36a68d66daeae271d14819cda6d2d95c to your computer and use it in GitHub Desktop.
Save luandevpro/36a68d66daeae271d14819cda6d2d95c to your computer and use it in GitHub Desktop.
addEventListener('fetch', event => {
event.respondWith(fetchAndApply(event.request))
})
async function fetchAndApply(request) {
const country = request.headers.get('Cf-Ipcountry').toLowerCase()
let url = new URL(request.url)
const target_url = 'https://' + url.hostname + '/' + country
const target_url_response = await fetch(target_url)
if(target_url_response.status === 200) {
return new Response('', {
status: 302,
headers: {
'Location': target_url
}
})
} else {
return fetch(request)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment