Created
August 18, 2019 03:39
-
-
Save luandevpro/36a68d66daeae271d14819cda6d2d95c to your computer and use it in GitHub Desktop.
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
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