Skip to content

Commit b373cfe

Browse files
committed
cloudflare: routing multiple origins
1 parent ca91910 commit b373cfe

File tree

1 file changed

+59
-2
lines changed

1 file changed

+59
-2
lines changed

advanced/subpath/cloudflare.mdx

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Cloudflare worker.
1414
</Frame>
1515

1616
<Warning>
17-
Keep in mind: If your DNS provider is Cloudflare you should not use proxying for the CNAME record
17+
Keep in mind: If your DNS provider is Cloudflare you should not use proxying for the CNAME record.
1818
</Warning>
1919

2020
### Add custom domain
@@ -74,11 +74,68 @@ async function handleRequest(request) {
7474
return await fetch(proxyRequest);
7575
}
7676
} catch (error) {
77-
// if no action found, play the regular request
77+
// If no action found, play the regular request
7878
return await fetch(request);
7979
}
8080
}
8181
```
8282

8383
Click on `Deploy` and wait for the changes to propagate (it can take up to a few
8484
hours).
85+
86+
### Routing multiple origins
87+
88+
If you're using a website builder, like Webflow or Squarespace, or another hosting provider for your main site and want to host your docs at a `/docs` subdirectory:
89+
1. Set up a staging domain for your main site like `staging.yoursite.com`.
90+
2. Update any absolute URLs in your main site to be relative.
91+
3. Use the enhanced worker script below that routes between both origins.
92+
93+
<Warning>
94+
Make sure your main site is set up on a staging domain before deploying this worker, or visitors to your main site will see errors.
95+
</Warning>
96+
97+
```javascript
98+
addEventListener("fetch", (event) => {
99+
event.respondWith(handleRequest(event.request));
100+
});
101+
102+
async function handleRequest(request) {
103+
try {
104+
const urlObject = new URL(request.url);
105+
106+
// If the request is to the docs subdirectory
107+
if (/^\/docs/.test(urlObject.pathname)) {
108+
// Proxy to Mintlify
109+
const DOCS_URL = "[SUBDOMAIN].mintlify.dev";
110+
const CUSTOM_URL = "[YOUR_DOMAIN]";
111+
112+
let url = new URL(request.url);
113+
url.hostname = DOCS_URL;
114+
115+
let proxyRequest = new Request(url, request);
116+
117+
proxyRequest.headers.set("Host", DOCS_URL);
118+
proxyRequest.headers.set("X-Forwarded-Host", CUSTOM_URL);
119+
proxyRequest.headers.set("X-Forwarded-Proto", "https");
120+
121+
return await fetch(proxyRequest);
122+
}
123+
124+
// For multiple origins: route everything else to main site
125+
const MAIN_SITE_URL = "[STAGING_DOMAIN]"; // example: staging.yoursite.com
126+
if (MAIN_SITE_URL && MAIN_SITE_URL !== "[STAGING_DOMAIN]") {
127+
let mainSiteUrl = new URL(request.url);
128+
mainSiteUrl.hostname = MAIN_SITE_URL;
129+
130+
return await fetch(mainSiteUrl, {
131+
method: request.method,
132+
headers: request.headers,
133+
body: request.body
134+
});
135+
}
136+
137+
} catch (error) {
138+
// If no action found, play the regular request
139+
return await fetch(request);
140+
}
141+
}

0 commit comments

Comments
 (0)