Skip to content

Commit 686b9eb

Browse files
committed
add info on routing for webflow
1 parent 77bdb0d commit 686b9eb

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

advanced/subpath/cloudflare.mdx

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,56 @@ async function handleRequest(request) {
8282

8383
Click on `Deploy` and wait for the changes to propagate (it can take up to a few
8484
hours).
85+
86+
## Webflow custom routing
87+
If you use Webflow to host your main site and want to serve Mintlify docs at `/docs` on the same domain, you'll need to configure custom routing through Cloudflare Workers to proxy all non-docs traffic to your main site.
88+
89+
<Warning>
90+
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.
91+
</Warning>
92+
93+
1. In Webflow, set up a staging domain for your main site like `staging.yoursite.com`.
94+
2. Deploy your main site to the staging domain. This ensures that your main site remains accessible while you configure the Worker.
95+
3. To avoid conflicts, update any absolute URLs in your main site to be relative.
96+
4. In Cloudflare, select **Edit Code** and add the following script into your Worker's code.
97+
98+
<Tip> Replace `[SUBDOMAIN]` with your unique subdomain, `[YOUR_DOMAIN]` with your website's base URL, and `[STAGING_DOMAIN]` with your staging domain URL. </Tip>
99+
100+
```javascript
101+
addEventListener("fetch", (event) => {
102+
event.respondWith(handleRequest(event.request));
103+
});
104+
async function handleRequest(request) {
105+
try {
106+
const urlObject = new URL(request.url);
107+
// If the request is to the docs subdirectory
108+
if (/^\/docs/.test(urlObject.pathname)) {
109+
// Proxy to Mintlify
110+
const DOCS_URL = "[SUBDOMAIN].mintlify.dev";
111+
const CUSTOM_URL = "[YOUR_DOMAIN]";
112+
let url = new URL(request.url);
113+
url.hostname = DOCS_URL;
114+
let proxyRequest = new Request(url, request);
115+
proxyRequest.headers.set("Host", DOCS_URL);
116+
proxyRequest.headers.set("X-Forwarded-Host", CUSTOM_URL);
117+
proxyRequest.headers.set("X-Forwarded-Proto", "https");
118+
return await fetch(proxyRequest);
119+
}
120+
// Route everything else to main site
121+
const MAIN_SITE_URL = "[STAGING_DOMAIN]";
122+
if (MAIN_SITE_URL && MAIN_SITE_URL !== "[STAGING_DOMAIN]") {
123+
let mainSiteUrl = new URL(request.url);
124+
mainSiteUrl.hostname = MAIN_SITE_URL;
125+
return await fetch(mainSiteUrl, {
126+
method: request.method,
127+
headers: request.headers,
128+
body: request.body
129+
});
130+
}
131+
} catch (error) {
132+
// If no action found, serve the regular request
133+
return await fetch(request);
134+
}
135+
}
136+
```
137+
5. Select Deploy and wait for the changes to propagate, which can take up to a few hours.

0 commit comments

Comments
 (0)