@@ -34,27 +34,33 @@ In your Cloudflare dashboard, select **Edit Code** and add the following script
3434</Tip >
3535
3636``` javascript
37- export default {
38- async fetch (request ) {
39- const url = new URL (request .url );
40-
41- // Route /docs traffic to Mintlify
42- if (url .pathname .startsWith (' /docs' )) {
37+ addEventListener (" fetch" , (event ) => {
38+ event .respondWith (handleRequest (event .request ));
39+ });
40+
41+ async function handleRequest (request ) {
42+ try {
43+ const urlObject = new URL (request .url );
44+ // If the request is to the docs subdirectory
45+ if (/ ^ \/ docs/ .test (urlObject .pathname )) {
46+ // Then Proxy to Mintlify
4347 const DOCS_URL = " [SUBDOMAIN].mintlify.dev" ;
4448 const CUSTOM_URL = " [YOUR_DOMAIN]" ;
45-
49+
50+ let url = new URL (request .url );
4651 url .hostname = DOCS_URL ;
47-
48- const proxyRequest = new Request (url, request);
52+
53+ let proxyRequest = new Request (url, request);
54+
4955 proxyRequest .headers .set (" Host" , DOCS_URL );
5056 proxyRequest .headers .set (" X-Forwarded-Host" , CUSTOM_URL );
51-
52- return fetch (proxyRequest);
57+ proxyRequest .headers .set (" X-Forwarded-Proto" , " https" );
58+
59+ return await fetch (proxyRequest);
5360 }
54-
55- // Route all other traffic to your main site
56- // Replace with your main site's URL
57- return fetch (` https://your-main-site.com${ url .pathname }${ url .search } ` );
61+ } catch (error) {
62+ // if no action found, play the regular request
63+ return await fetch (request);
5864 }
5965}
6066```
0 commit comments