Building a universal, fast redirect (301, 302, domain independent)

Hi community,

I cobbled this redirect worker together but it’s missing a few features.
Since we only have 30 workers available, I want to reuse this one across my domains.

How to adapt it so

  1. I can have it check the paths after it has checked which domain it is on?
    If it’s on site 2, I don’t want it to check all of the cases for site 1.
    This should speed things up and also make sure that we can truly use it across different domains, even when f.i. blog should be redirected on site 2, but not on site 1.

  2. I liked the simplicity of https://developers.cloudflare.com/workers/examples/bulk-redirects
    Is there a way to get all of this information in (host check, then path, target url, 301 or 302) in a similar more compact format?

  3. Is there a way to cache results to speed up resolution?

  4. Finally, is it possible to add this data to a workers KV so it can be sped up?

I’m just a beginner doing this by trial and error and learning what I can, and know very little JS (at the moment).

All help appreciated.


addEventListener('fetch', event => {
event.respondWith(fetchAndApply(event.request))
})

async function fetchAndApply(request) {
var url = new URL(request.url);
var path = url.pathname;
var site1	= "https://site1.com";
var site2	= "https://site2.com";
var site9	= "https://external.com";

switch(path) {

/* Site 1 */

  case "":
	return Response.redirect(site1 + '/index.php', 301);
	break;

  case "/contact-us":
  case "/partners":
	return Response.redirect(site_1 + '/contact', 301); 
	break;

  case "/promo":
	return Response.redirect(site1 + '/promo-2021-01', 302); 
	break;

  case "/oldstuff1":
  case "/oldstuff2":
  case "/oldstuff2":
	return Response.redirect(site9 + '', 301);
	break;

/*
/blog should still be served as /blog on site 1
*/

/* Site 2 */

/* Can't as it overlaps with the first site
case "":
return Response.redirect(site2 + '/home.php', 301);
break;
*/

case "/blog":
case "/articles":
return Response.redirect(site_2 + '/news', 301); 
break;


  default:
	const response = await fetch(request)
	return response
	break;
}
}

(1) SOLVED.
I was able to make this one worker handle multiple different domain names.

Still hoping for input about (2-3-4)