Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions advanced/subpath/cloudflare.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: "Cloudflare"
description: "Host documentation at a /docs subpath using Cloudflare Workers"

Check warning on line 3 in advanced/subpath/cloudflare.mdx

View check run for this annotation

Mintlify / Mintlify Validation - vale-spellcheck

advanced/subpath/cloudflare.mdx#L3

Did you really mean 'subpath'?
---

## Create Cloudflare Worker
Expand All @@ -14,7 +14,7 @@
</Frame>

<Warning>
Keep in mind: If your DNS provider is Cloudflare you should not use proxying for the CNAME record

Check warning on line 17 in advanced/subpath/cloudflare.mdx

View check run for this annotation

Mintlify / Mintlify Validation - vale-spellcheck

advanced/subpath/cloudflare.mdx#L17

Did you really mean 'proxying'?
</Warning>

### Add custom domain
Expand Down Expand Up @@ -82,3 +82,56 @@

Click on `Deploy` and wait for the changes to propagate (it can take up to a few
hours).

## Webflow custom routing

Check warning on line 86 in advanced/subpath/cloudflare.mdx

View check run for this annotation

Mintlify / Mintlify Validation - vale-spellcheck

advanced/subpath/cloudflare.mdx#L86

Did you really mean 'Webflow'?
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.

Check warning on line 87 in advanced/subpath/cloudflare.mdx

View check run for this annotation

Mintlify / Mintlify Validation - vale-spellcheck

advanced/subpath/cloudflare.mdx#L87

Did you really mean 'Webflow'?

<Warning>
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.
</Warning>

1. In Webflow, set up a staging domain for your main site like `staging.yoursite.com`.

Check warning on line 93 in advanced/subpath/cloudflare.mdx

View check run for this annotation

Mintlify / Mintlify Validation - vale-spellcheck

advanced/subpath/cloudflare.mdx#L93

Did you really mean 'Webflow'?
2. Deploy your main site to the staging domain. This ensures that your main site remains accessible while you configure the Worker.
3. To avoid conflicts, update any absolute URLs in your main site to be relative.
4. In Cloudflare, select **Edit Code** and add the following script into your Worker's code.

<Tip> Replace `[SUBDOMAIN]` with your unique subdomain, `[YOUR_DOMAIN]` with your website's base URL, and `[STAGING_DOMAIN]` with your staging domain URL. </Tip>

```javascript
addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
try {
const urlObject = new URL(request.url);
// If the request is to the docs subdirectory
if (/^\/docs/.test(urlObject.pathname)) {
// Proxy to Mintlify
const DOCS_URL = "[SUBDOMAIN].mintlify.dev";
const CUSTOM_URL = "[YOUR_DOMAIN]";
let url = new URL(request.url);
url.hostname = DOCS_URL;
let proxyRequest = new Request(url, request);
proxyRequest.headers.set("Host", DOCS_URL);
proxyRequest.headers.set("X-Forwarded-Host", CUSTOM_URL);
proxyRequest.headers.set("X-Forwarded-Proto", "https");
return await fetch(proxyRequest);
}
// Route everything else to main site
const MAIN_SITE_URL = "[STAGING_DOMAIN]";
if (MAIN_SITE_URL && MAIN_SITE_URL !== "[STAGING_DOMAIN]") {
let mainSiteUrl = new URL(request.url);
mainSiteUrl.hostname = MAIN_SITE_URL;
return await fetch(mainSiteUrl, {
method: request.method,
headers: request.headers,
body: request.body
});
}
} catch (error) {
// If no action found, serve the regular request
return await fetch(request);
}
}
```
5. Select Deploy and wait for the changes to propagate, which can take up to a few hours.