Skip to content

Instantly share code, notes, and snippets.

@fullstackNRJ
Created September 8, 2023 23:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fullstackNRJ/7c8a1a12612749f04dc08c6403fe4789 to your computer and use it in GitHub Desktop.
Save fullstackNRJ/7c8a1a12612749f04dc08c6403fe4789 to your computer and use it in GitHub Desktop.
reverse proxy logic for intercepting requests and serving the correct content
const ROOT_DOMAIN = "kanvasai.com"; // represents @ CNAME record
const SUBDOMAINS = ["medium", "devto"]; // respresents `medium` & `devto` CNAME records
const MAIN_SUBDOMAIN = "main"; // respresent our main website CNAME record
const MAIN_URL = `https://${ROOT_DOMAIN}`;
const hasTrailingSlash = (str: string) => /\/$/.test(str);
export default {
async fetch(
request: Request,
env: Env,
context: ExecutionContext
): Promise<Response> {
console.log(":request", request);
console.log("env::", env);
console.log("::ctx", context);
const requestUrl = new URL(request.url);
const { hostname, pathname, search, hash } = requestUrl;
console.log("Intercepted by worker function", {
pathname,
hostname,
search,
hash,
});
if (hostname !== ROOT_DOMAIN) {
const matchedSubDomain = SUBDOMAINS.find(
(subdomain) => subdomain === hostname.split(".")[0]
);
if (matchedSubDomain) {
//redirect to webflow subdirectory
const redirect_url = `${MAIN_URL}/${matchedSubDomain}${pathname}${search}`;
return Response.redirect(redirect_url, 301);
}
if (hostname.startsWith(MAIN_SUBDOMAIN)) {
//redirect to root domain
const redirect_url = `${MAIN_URL}${pathname}${search}`;
return Response.redirect(redirect_url, 301);
}
//handle non-matched subdomains cdn.example.com/abc (diff one)
console.log("subdomain did not match ::", requestUrl);
return fetch(request.url);
}
//handle trailing `/` in pathname
const paths = pathname.split("/").filter(Boolean);
if (paths.length && hasTrailingSlash(pathname)) {
const redirect_url = `${MAIN_URL}/${paths.join("/")}${search}`;
return Response.redirect(redirect_url, 301);
}
//path matches reverse proxy subdomain
//https://kanvasai.com/medium/about?query=yes -> https://medium.kanvasai.com/about?query=yes
const matchedSubRoute = SUBDOMAINS.find(
(subdomain) => subdomain === paths[0]
);
if (matchedSubRoute) {
const redirect_url = `https://${matchedSubRoute}.${ROOT_DOMAIN}/${paths
.slice(1)
.join("/")}${search}`;
const response = await fetch(redirect_url);
//handle 301 from actual site if defined any (webflow)
if (response.redirected && response.url.length) {
return Response.redirect(response.url, 301);
}
return response;
}
//no subdomain, no trailing slash, no subroute
//https://kanvasai.com/about?query=yes -> https://main.kanvasai.com/about?query=yes (fetch from main site)
const fetch_url = `https://${MAIN_SUBDOMAIN}.${ROOT_DOMAIN}${pathname}${search}`;
const response = await fetch(fetch_url);
//handle 301 from actual site (webflow)
if (response.redirected && response.url.length) {
return Response.redirect(response.url, 301);
}
return response;
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment