Skip to content

Instantly share code, notes, and snippets.

@louisbarclay
Created February 23, 2021 10:33
Show Gist options
  • Save louisbarclay/2e9dc382f512b1641b9191d0e2763f83 to your computer and use it in GitHub Desktop.
Save louisbarclay/2e9dc382f512b1641b9191d0e2763f83 to your computer and use it in GitHub Desktop.
// Change this to suit your case!
// IMPORTANT: make sure there is no '/' at the end of either URL
const config = {
originPage: "view.flodesk.com/emails",
}
// Function that processes requests to the URL the worker is at
async function handleRequest(request) {
// Grab the request URL's pathname, we'll use it later
const url = new URL(request.url)
const targetPath = url.pathname
// Change request URLs to go through to the subdomain
let response = await fetch(`https://${config.originPage}${targetPath}`)
// We don't need to change these requests at all
// So we return the response of the fetch request from above
// immediately.
return appendHtml(response)
}
// Change HTML response to fix favicon
async function appendHtml(res) {
return (
new HTMLRewriter()
.on("head", new HeadRewriter())
.transform(res)
)
}
// Head rewriter to handle favicon
class HeadRewriter {
element(element) {
element.append(
`<link rel="icon" href="https://api.super.so/asset/super.so/55b95b71-eb85-4643-9315-8e0af7598a2f.svg">`,
{
html: true,
}
)
}
}
addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment