'How would you create a reverse proxy using a Firebase function?

I'd like to create a reverse proxy for my analytics so that it doesn't get shut down by ad blockers.

Cloudflare has an excellent API for this using their web-workers but using them in conjuction with a CNAME (i.e. on your own host) is only available on their enterprise plan.

The cloudflare code to do the reverse proxy is simply:


addEventListener("fetch", event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
    request = new Request(request)
    var url = new URL(request.url)
    url.hostname = 'api-js.mixpanel.com'
    return await fetch(url, request)
}

How might one go about creating the same type of functionality using a Firebase function instead?



Solution 1:[1]

If you are specifically trying to proxy MixPanel requests to avoid Ad Blockers then you can do this via 2 methods

  1. You can use Cloudflare Zaraz + Cloudflare Worker
  2. You can use their official proxy, that can be hosted in Cloud Run or DigitalOcean droplet: https://github.com/mixpanel/tracking-proxy

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 CaptainZero