Skip to content

Instantly share code, notes, and snippets.

@janyksteenbeek
Created August 3, 2023 14:41
Show Gist options
  • Save janyksteenbeek/6f9267ce63070c93f7fde7ca0bd6ee9f to your computer and use it in GitHub Desktop.
Save janyksteenbeek/6f9267ce63070c93f7fde7ca0bd6ee9f to your computer and use it in GitHub Desktop.
Cloudflare Worker function for shortener
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