Created
July 28, 2021 03:10
-
-
Save maraisr/e35dccf99edfa7b9ab847dc3b76e06d4 to your computer and use it in GitHub Desktop.
GeoRedirect CF Worker
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
const localeMap = { | |
AU: "/au", | |
US: "/us", | |
GB: "/uk", | |
NZ: "/nz", | |
MX: "/es-mx", | |
"*": "/au", | |
}; | |
addEventListener( | |
"fetch", | |
(event, request) => { | |
const url = new URL(request.url); | |
if (url.pathname === "/") { | |
const locale = request.cf?.country ?? "*"; | |
url.pathname = localeMap[locale] ?? localeMap["*"]; | |
url.protocol = "https"; // be good | |
return void event.respondWith(Response.redirect(url.href, 307)); | |
} | |
return void event.respondWith(fetch(request)); | |
}, | |
); | |
export {}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment