Skip to content

Cloudflare Workers

A Worker with static assets serves the built site and answers the annotation API on the same origin. A complete example is in examples/cloudflare-worker.

worker.js
import { createAnnotationsHandler, github } from 'starlight-annotations/server';
export default {
async fetch(request, env) {
if (new URL(request.url).pathname.startsWith('/_annotations/')) {
const options = {
repo: env.ANNOTATIONS_REPO,
token: env.GITHUB_TOKEN,
clientId: env.GITHUB_CLIENT_ID,
clientSecret: env.GITHUB_CLIENT_SECRET,
sessionSecret: env.ANNOTATIONS_SECRET,
};
// Until every value is set, answer 503: the pages then hide the annotation UI.
if (Object.values(options).some((value) => !value)) {
return Response.json({ error: 'Annotations are not configured.' }, { status: 503 });
}
const response = await createAnnotationsHandler(github(options))(request);
if (response) return response;
}
return env.ASSETS.fetch(request);
},
};
wrangler.jsonc
{
"name": "my-docs",
"main": "./worker.js",
"compatibility_date": "2026-10-29",
"assets": { "directory": "./dist", "binding": "ASSETS", "run_worker_first": ["/_annotations/*"] },
"vars": { "ANNOTATIONS_REPO": "owner/docs", "GITHUB_CLIENT_ID": "Ov23…" }
}

run_worker_first sends only the API paths to the Worker; pages and assets are served directly.

Set the secrets once:

Terminal window
npx wrangler secret put GITHUB_CLIENT_SECRET
npx wrangler secret put GITHUB_TOKEN
openssl rand -hex 32 | npx wrangler secret put ANNOTATIONS_SECRET

Then npm run build && npx wrangler deploy. The OAuth app, the token and the session secret are explained step by step in Store notes on GitHub.

If your Worker already checks a password or Cloudflare Access, call the handler after that check: notes are then readable only by people who can read the pages.