Skip to content

A github app to automatically manage sanity cors for preview links from cloudflare workers.

nrodd/workers-sanity-cors

Repository files navigation

workers-sanity-cors

A Cloudflare Worker that automatically manages Sanity CORS origins for preview deployments from GitHub PRs.

What it does

  • On deployment success: Automatically adds the preview deployment URL to Sanity CORS allowlist
  • On PR close/merge: Automatically removes the preview URL from Sanity CORS allowlist
  • Webhook verification: Securely validates GitHub webhook signature

Setup

1. Clone and install

git clone https://github.com/YOUR_USERNAME/workers-sanity-cors.git
cd workers-sanity-cors
npm install

2. Configure environment variables

Copy the example environment file:

cp .env.example .env

Edit .env with your actual values:

# GitHub webhook secret (you'll create this in step 4)
GITHUB_WEBHOOK_SECRET=your_webhook_secret_here

# Sanity project configuration
SANITY_PROJECT_ID=your_project_id_here
SANITY_TOKEN=your_sanity_token_here

# Your Workers domain (replace with your actual domain)
WORKERS_DOMAIN=your-domain.workers.dev

3. Generate TypeScript definitions

npx wrangler types

4. Deploy to Cloudflare Workers

npx wrangler deploy

Note the deployment URL - you'll need this for the GitHub webhook.

5. Set up Wrangler secrets

Instead of using .env for production, set secrets directly with Wrangler:

npx wrangler secret put GITHUB_WEBHOOK_SECRET
npx wrangler secret put SANITY_PROJECT_ID  
npx wrangler secret put SANITY_TOKEN
npx wrangler secret put WORKERS_DOMAIN

You can also configure these manually through the cloudflare workers admin settings.

6. Configure GitHub webhook

  1. Go to your GitHub repository → Settings → Webhooks
  2. Click "Add webhook"
  3. Set Payload URL to: https://your-worker-url.workers.dev/webhook
  4. Set Content type to: application/json
  5. Set Secret to the same value you used for GITHUB_WEBHOOK_SECRET
  6. Select individual events:
    • Deployment statuses
    • Pull requests
  7. Ensure the webhook is Active
  8. Click "Add webhook"

7. Get your Sanity credentials

Sanity Project ID

Find this in your Sanity project dashboard URL: sanity.io/manage/personal/project/YOUR_PROJECT_ID

Sanity Token

  1. Go to Sanity Management Console
  2. Select your project
  3. Go to API → Tokens
  4. Create a new token with Editor permissions
  5. Copy the token value

Development

Local development

npm run dev

Type checking

npx wrangler types  # Regenerate types

License

MIT

About

A github app to automatically manage sanity cors for preview links from cloudflare workers.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •