Proxy websites with arbitrary links and support style

:vulcan_salute: Hey, Cloudflare Community!

I’m try to create a Worker with support arbitrary URL, to generate access for websites and I use this template:

"use strict";

if (typeof addEventListener === 'function') {
    addEventListener('fetch', (e) => {
        // work around as strict typescript check doesn't allow e to be of type FetchEvent
        const fe = e;
async function proxyRequest(r) {
    const url = new URL(r.url);
    const prefix = '/website/';
    if (url.pathname.startsWith(prefix)) {
        const remainingUrl = url.pathname.replace(new RegExp('^' + prefix), '');
        let targetUrl = decodeURIComponent(remainingUrl);
        if (!targetUrl.startsWith('http://') && !targetUrl.startsWith('https://')) {
            targetUrl = url.protocol + '//' + targetUrl;
        return fetch(targetUrl);
    else {
        return new Response('Bad Request', { status: 400, statusText: 'Bad Request' });

This script is work, but the websites a loaded incorrect, css, images not load. Next, I found this template:

async function handleRequest(request) {
    const url = new URL(request.url)
    if (url.hostname in ORIGINS) {
      const target = ORIGINS[url.hostname]
      url.hostname = target
      return fetch(url.toString(), request)
    return fetch(request)
  addEventListener('fetch', event => {
  const ORIGINS = {
    '': ''

This script fully loads the site, it supports styles and images, but not supports arbitrary URL, he generate access only for website from const ORIGINS code block. :frowning:

Please help me, how I can change the first or second template to combine their functionality in order to load sites correctly and support arbitrary URLs?

Hello, In regards to this question on Worker’s script solutions I recommend you reaching out to the Dev community on Discord here for further assistance.

You can also create a support ticket, feel free to reference myself in it, and we’ll have a further look on your request.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.