Worker: sending in url as argument to Reverse-Proxy?

Hello All,
I have a use case that requires a reverse-proxy to load websites from low powered devices which only support http. They need to load into an iFrame on a secure https site. As there can be many of these iot devices setting up one worker to proxy one device isn’t scalable. I am trying to pass in a url to proxy but after the initial page loads the css, images etc are throwing 404 errors because they are looking for those images on the proxy url and not the Origin.

If I hardcode the Origin everything works fine but that’s not scalable.
Below is my current worker code. Any help would be much appreciated!!
Thank you in advance.

Routes are setup as:

const DOMAIN = ''; // const PROXYPATH = 'controller'; // path to be proxied

let ORIGIN = ‘’; // where to fetch content from - this works when it’s hardcoded.

addEventListener(‘fetch’, event => {
var url = new URL(event.request.url);
const { searchParams } = url
/* Send website url to proxy in as an argument to support dynamic proxy of http sites -

  • not working for images/css or anything loaded in the initial page throwing 404error
    if(searchParams !== null){
    ORIGIN = searchParams.get(‘c’);

if (url.pathname.startsWith(’/’ + PROXYPATH + ‘/’) || url.pathname === ‘/’ + PROXYPATH) {
handleRequest(event, url, ORIGIN);
} else {
handleRelativeRequest(event, url, ORIGIN);

async function handleRelativeRequest(event, url, ORIGIN){
var originUrl = url.toString();
originUrl = originUrl.toString().replace(‘https://’ + DOMAIN, ‘http://’ + ORIGIN).replace(’/’ + PROXYPATH, “”);



async function handleRequest(event, url, ORIGIN) {

var originUrl = url.toString().replace(
‘https://’ + DOMAIN + ‘/’ + PROXYPATH,
‘http://’ + ORIGIN
).replace(DOMAIN, ORIGIN);