Cloudflare Workers

Proxy your Jamdesk docs through Cloudflare Workers at a /docs subpath

Use Cloudflare Workers to proxy documentation requests from your domain to Jamdesk. Workers run at the edge, providing fast response times globally.

Prerequisites

  • A Cloudflare account with your domain configured
  • Wrangler CLI v3.0+ installed
  • Your Jamdesk subdomain (found in dashboard settings)

Quick Setup with CLI

The fastest way to set up your Cloudflare Worker:

npx jamdesk deploy cloudflare

This interactive command will:

  1. Check that wrangler 3.0+ is installed
  2. Verify your Cloudflare account and show available domains
  3. Auto-detect your project slug from docs.json
  4. Let you select your target domain from your Cloudflare zones
  5. Generate all required files
  6. Optionally deploy to Cloudflare

Account Selection

The CLI verifies you're logged into the correct Cloudflare account before proceeding. If you have access to multiple Cloudflare accounts (common for agencies or teams), you'll be prompted to choose:

Step 2: Verifying Cloudflare account...
✓ Logged in as: you@example.com
? Select the Cloudflare account to use:
❯ Your Personal Account
  Company Team Account
  [ Switch to different login ]

After selecting an account, only domains from that account will be available for zone selection:

✓ Using account: Company Team Account
✓ Domains: 3 available for Workers routing

Make sure you select the Cloudflare account that has the domain you want to deploy to. Each account has its own set of domains, and you can only create Workers routes for domains in the selected account.

CI/Scripted Deployment

For CI/scripting, use flags to skip prompts:

jamdesk deploy cloudflare --slug myproject --domain example.com --skip-deploy --yes
OptionDescription
--slugJamdesk project slug (skip auto-detection)
--domainTarget domain (e.g., yoursite.com)
--pathPath prefix (default: /docs)
--output-dirOutput directory (default: cloudflare-worker/)
--skip-deployGenerate files only, don't deploy
--forceOverwrite existing directory
--yesSkip all prompts (CI mode)

If you prefer manual setup, continue with the steps below.


Manual Setup

Step 1: Create a Worker

Create a new directory for your Worker and initialize it:

mkdir docs-proxy && cd docs-proxy
npm init -y

Step 2: Add the Worker Code

Create index.js with the following code:

index.js
const JAMDESK_HOST = "YOUR_SLUG.jamdesk.app";

export default {
  async fetch(request) {
    const url = new URL(request.url);

    // Only proxy /docs paths
    if (!url.pathname.startsWith("/docs")) {
      return fetch(request);
    }

    // Rewrite the request to Jamdesk
    const proxyUrl = new URL(request.url);
    proxyUrl.hostname = JAMDESK_HOST;

    // Clone headers and add proxy headers
    const headers = new Headers(request.headers);
    headers.set("Host", JAMDESK_HOST);
    headers.set("X-Forwarded-Host", url.hostname);
    headers.set("X-Forwarded-Proto", "https");
    // Required for domain verification
    headers.set("X-Jamdesk-Forwarded-Host", url.hostname);

    const proxyRequest = new Request(proxyUrl, {
      method: request.method,
      headers: headers,
      body: request.body,
    });

    return fetch(proxyRequest);
  },
};

Replace YOUR_SLUG with your actual Jamdesk subdomain (e.g., acme if your docs are at acme.jamdesk.app).

The X-Jamdesk-Forwarded-Host header is required for domain verification. Your domain must be verified in the Jamdesk dashboard before the Worker will serve content.

Step 3: Configure wrangler.toml

Create wrangler.toml to configure your Worker:

wrangler.toml
name = "docs-proxy"
main = "index.js"
compatibility_date = "2024-01-01"

# Route configuration - replace with your domain
routes = [
  { pattern = "yoursite.com/docs*", zone_name = "yoursite.com" }
]

Step 4: Deploy

Deploy your Worker to Cloudflare:

npx wrangler deploy

Step 5: Verify

Visit https://yoursite.com/docs to confirm your documentation is being served correctly.

Alternative: Service Worker Syntax

If you prefer the older Service Worker syntax (compatible with older Workers):

index.js
const JAMDESK_HOST = "YOUR_SLUG.jamdesk.app";

addEventListener("fetch", (event) => {
  event.respondWith(handleRequest(event.request));
});

async function handleRequest(request) {
  const url = new URL(request.url);

  if (!url.pathname.startsWith("/docs")) {
    return fetch(request);
  }

  const proxyUrl = new URL(request.url);
  proxyUrl.hostname = JAMDESK_HOST;

  const headers = new Headers(request.headers);
  headers.set("Host", JAMDESK_HOST);
  headers.set("X-Forwarded-Host", url.hostname);
  headers.set("X-Forwarded-Proto", "https");
  // Required for domain verification
  headers.set("X-Jamdesk-Forwarded-Host", url.hostname);

  return fetch(new Request(proxyUrl, {
    method: request.method,
    headers: headers,
    body: request.body,
  }));
}

Troubleshooting

The CLI shows your available domains before zone selection. If you see "No domains found":

  1. Verify you're logged into the correct Cloudflare account
  2. Check that your domain is added and active in the Cloudflare dashboard
  3. Run the CLI again and select "No" when asked to continue with the current account to switch accounts

If you have multiple Cloudflare accounts:

  1. Run jamdesk deploy cloudflare
  2. When prompted to select an account, choose the one with your domain
  3. If you need a completely different login, select "Switch to different login"
  4. The CLI will log you out and prompt you to log in with the correct credentials

This error means the selected zone doesn't match your Cloudflare account. Either:

  • You selected a zone that belongs to a different account
  • The zone was removed from Cloudflare

Fix: Re-run the CLI and select the correct zone from the list, or switch to the account that owns the zone.

Ensure your route pattern includes the wildcard: yoursite.com/docs* (not just yoursite.com/docs).

Check that the X-Forwarded-Host header is set correctly. This tells Jamdesk which domain to use for asset URLs.

If you see "Domain is not authorized to serve this content":

  1. Verify your domain is registered in the Jamdesk dashboard
  2. Complete DNS verification (TXT record) for your domain
  3. Ensure the X-Jamdesk-Forwarded-Host header is set in your Worker code
  4. Check that your domain maps to the correct project

The domain must be verified before the Worker can serve documentation.

Verify your domain is proxied through Cloudflare (orange cloud icon in DNS settings) and the route is correctly configured.

The CLI requires wrangler 3.0+. Update with:

npm install -g wrangler@latest