> ## Documentation Index
> Fetch the complete documentation index at: https://docs.aihubmix.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Cloudflare

> Host documentation at a /docs subpath using Cloudflare Workers

## Create Cloudflare Worker

Navigate to the `Workers & Pages > Create application > Create worker`. You
should be presented with the following screen where you can create a new
Cloudlfare worker.

<Frame>
  <img alt="Create a Cloudflare worker" src="https://mintcdn.com/aihubmix/2nT-vfHReUtOnhMv/images/cloudflare/worker.png?fit=max&auto=format&n=2nT-vfHReUtOnhMv&q=85&s=e00525097e073e425760dd05a61deca2" width="2986" height="1646" data-path="images/cloudflare/worker.png" />
</Frame>

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

### Add custom domain

Once the worker is created, click `Configure worker`. Navigate to the worker
`Settings > Triggers`. Click on `Add Custom Domain` to add your desired domain
into the list - we recommend you add both the version with and without `www.`
prepended to the domain.

<Frame>
  <img alt="Cloudflare worker custom domain" src="https://mintcdn.com/aihubmix/2nT-vfHReUtOnhMv/images/cloudflare/custom-domain.png?fit=max&auto=format&n=2nT-vfHReUtOnhMv&q=85&s=78f7070caa0974624d716cdf1ffe2d7e" width="2988" height="1634" data-path="images/cloudflare/custom-domain.png" />
</Frame>

If you have trouble setting up a custom subdirectory,
[contact our support team](mailto:sales@mintlify.com) and we'll walk you through
upgrading your hosting with us.

### Edit Worker Script

Click on `Edit Code` and add the following script into the worker's code.

<Frame>
  <img alt="Cloudflare edit code" src="https://mintcdn.com/aihubmix/2nT-vfHReUtOnhMv/images/cloudflare/edit-code.png?fit=max&auto=format&n=2nT-vfHReUtOnhMv&q=85&s=1ba1e930b79fccdd418b3be98ab0e96a" width="2980" height="1600" data-path="images/cloudflare/edit-code.png" />
</Frame>

<Tip>
  Edit `DOCS_URL` by replacing `[SUBDOMAIN]` with your unique subdomain and
  `CUSTOM_URL` with your website's base URL.
</Tip>

```javascript theme={null}
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)) {
      // Then 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);
    }
  } catch (error) {
    // if no action found, play the regular request
    return await fetch(request);
  }
}
```

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