Created
August 3, 2023 14:41
-
-
Save janyksteenbeek/6f9267ce63070c93f7fde7ca0bd6ee9f to your computer and use it in GitHub Desktop.
Cloudflare Worker function for shortener
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 redirects = [ | |
{ path: 'privacy', redirect: 'https://cdn-static.storewire.net/legal/privacybeleid.pdf' }, | |
{ path: 'tos', redirect: 'https://cdn-static.storewire.net/legal/leveringsvoorwaarden.pdf' }, | |
{ path: 'ig', redirect: 'https://instagram.com/storewire' }, | |
{ path: 'fb', redirect: 'https://facebook.com/storewire' }, | |
{ path: 'gh', redirect: 'https://github.com/storewire' }, | |
{ path: 'tw', redirect: 'https://twitter.com/storewirenl' }, | |
{ path: 'li', redirect: 'https://linkedin.com/company/storewire' }, | |
{ path: 'integration-bolcom-video', redirect: 'https://youtu.be/MTTp5x06bJI?t=30' } | |
]; | |
const defaultUrl = 'https://storewire.net'; | |
addEventListener("fetch", event => { | |
event.respondWith(handleRequest(event.request)) | |
}) | |
async function handleRequest(request) { | |
const url = new URL(request.url); | |
let path = url.pathname.slice(1); | |
if(path.charAt(path.length - 1) === '/') { | |
path = path.slice(0, -1); | |
} | |
let redirectUrl; | |
redirects.forEach(config => { | |
if(config.path === path) { | |
redirectUrl = config.redirect; | |
} | |
}); | |
return Response.redirect((redirectUrl || defaultUrl), 302); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment