Skip to content

Instantly share code, notes, and snippets.

@manuelmeurer
Created February 23, 2022 08:49
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 manuelmeurer/1001c75f53da262afd18efd7b21be65e to your computer and use it in GitHub Desktop.
Save manuelmeurer/1001c75f53da262afd18efd7b21be65e to your computer and use it in GitHub Desktop.
Uplink Cloudflare Workers
addEventListener('fetch', event =>
event.respondWith(handleRequest(event.request))
)
const sitemapUrl = "https://uplink-files.s3.eu-central-1.amazonaws.com/sitemap.xml"
const mediumBlogPostPaths = [
'/announcing-our-cooperation-with-expath-to-help-freelancers-get-started-in-germany-49edbd765992',
'/why-you-should-work-freelance-as-a-software-developer-in-germany-52de81082f6f',
'/why-are-many-it-recruiters-technically-completely-incompetent-e924f559ed0e',
'/announcing-our-new-partner-exali-5c9acfb34e1c',
'/debitoor-is-our-newest-partner-82883f67630f',
'/joining-forces-with-christa-weidner-6a234d97fb5a',
'/announcing-uplinks-first-partner-kontist-2484d587528e',
'/introducing-the-uplink-referral-program-72c3db1c8a8d',
'/lets-move-to-slack-86558368e6d8',
'/whats-in-a-pitch-cc80cefb7cb8',
'/our-new-pricing-4e088d7a90b4'
]
async function handleRequest(request) {
let url = new URL(request.url)
if (url.pathname === '/sitemap.xml') {
return fetch(sitemapUrl, request)
} else if ((url.hostname === 'uplink.tech') && (url.pathname.startsWith('/knowledge-base'))) {
url.hostname = 'uplinkhq.github.io'
let response = await fetch(url, request)
const location = response.headers.get('Location')
if (location) {
const locationUrl = new URL(location)
if (locationUrl.hostname === 'uplinkhq.github.io') {
locationUrl.hostname = 'uplink.tech'
response = new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: response.headers
})
response.headers.set('Location', locationUrl)
}
}
return response
} else if (url.hostname === 'kb.uplink.tech') {
url.hostname = 'uplink.tech'
url.pathname = '/knowledge-base' + url.pathname
return Response.redirect(url, 301)
} else if ((url.hostname === 'uplink.tech') && (url.pathname.startsWith('/blog'))) {
url.hostname = 'ghost.uplink.tech'
return fetch(url, request)
} else if (url.hostname === 'blog.uplink.tech') {
if (mediumBlogPostPaths.includes(url.pathname)) {
url.hostname = 'medium.com'
url.pathname = '/uplink-it-freelancer-network' + url.pathname
} else {
url.hostname = 'uplink.tech'
url.pathname = '/blog' + url.pathname
}
return Response.redirect(url, 301)
} else {
return fetch(request)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment