4.8 KiB
Shakespeare Iframe Proxy - Cloudflare Worker Setup
This Cloudflare Worker acts as a reverse proxy for the Shakespeare iframe-fetch-client, removing restrictive headers and adding CORS support to enable iframe embedding.
Quick Setup
Step 1: Deploy iframe-fetch-client to Surge
First, deploy your iframe-fetch-client files to Surge:
cd ../iframe-fetch-client
npx -y surge . your-unique-name.surge.sh
Replace your-unique-name with something unique.
Step 2: Update Worker Configuration
Edit worker.js and update the surgeDomain variable:
// Change this line
const surgeDomain = 'your-unique-name.surge.sh'; // TODO: Update this!
Use the same Surge domain you deployed to in Step 1.
Step 3: Deploy the Cloudflare Worker
Option A: Quick Setup (via Dashboard)
- Go to Cloudflare Dashboard
- Navigate to Workers & Pages → Create application
- Select "Create Worker"
- Copy the contents of
worker.jsinto the editor - Click "Save and Deploy"
- Note your worker's URL (e.g.,
shakespeare-iframe-proxy.your-account.workers.dev)
Option B: Using Wrangler CLI
-
Install Wrangler:
npm install -g wrangler -
Login to Cloudflare:
wrangler login -
Deploy from this directory:
cd cloudflare-worker wrangler deploy
Step 4: Set Up Custom Domain (Optional but Recommended)
Using Cloudflare Dashboard:
-
Go to your domain in Cloudflare DNS
-
Create a wildcard CNAME record:
- Type:
CNAME - Name:
*(or a specific subdomain likeiframe) - Target: Your worker URL (e.g.,
shakespeare-iframe-proxy.your-account.workers.dev) - Proxy status: Enabled (orange cloud)
- Type:
-
Add a route to your worker:
- In Workers & Pages, select your worker
- Go to Settings → Triggers → Routes
- Add route:
*.your-domain.com/*
Using Wrangler:
Update your wrangler.toml:
[env.production]
vars = { ENVIRONMENT = "production" }
[[env.production.routes]]
pattern = "*.your-domain.com/*"
zone_name = "your-domain.com"
Then deploy:
wrangler deploy --env production
Step 5: Update Your Environment
Update your .env file in the main project:
# Change from:
VITE_IFRAME_DOMAIN=local-shakespeare.dev
# To (using your custom domain):
VITE_IFRAME_DOMAIN=your-domain.com
# Or (using worker subdomain):
VITE_IFRAME_DOMAIN=your-account.workers.dev
Step 6: Test the Setup
-
Rebuild your main project:
cd .. npm run build # Or start development server npm run dev -
Open Shakespeare and create/load a project
-
Build the project
-
Switch to preview tab
-
Open browser console to see:
- Successful iframe loading
- Console messages being forwarded
- No CORS or security errors
Local Development
Test Worker Locally:
cd cloudflare-worker
wrangler dev --local-port 8787
Test with curl:
curl http://localhost:8787
Test with Local Shakespeare:
Update your .env temporarily:
VITE_IFRAME_DOMAIN=localhost:8787
Troubleshooting
Common Issues:
- CORS Errors: Make sure CORS headers are properly set in the worker
- X-Frame-Options Blocked: Ensure these headers are removed in the worker
- Mixed Content: Use HTTPS everywhere
- Service Worker Registration: Check MIME types for
.jsfiles
Debug Headers:
You can test what headers are being sent/received:
curl -I https://project123.your-domain.com/
Check Worker Logs:
Use Cloudflare Dashboard to view worker logs and debug requests.
Architecture
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Shakespeare App │ │ Cloudflare │ │ Surge.sh │
│ (Parent Window) │ │ Worker Proxy │ │ (File Hosting) │
│ │ │ │ │ │
│ - PreviewPane │───▶│ - Remove │───▶│ - iframe- │
│ - JSON-RPC │ │ X-Frame-Opt │ │ fetch-client │
│ - Console UI │ │ - Add CORS │ │ - sw.js │
│ │ │ - Route │ │ - index.html │
└─────────────────┘ └─────────────────┘ └─────────────────┘
The worker enables secure iframe embedding by:
- Removing headers that prevent iframe embedding
- Adding appropriate CORS headers for cross-origin communication
- Providing a stable domain for project subdomains
- Handling SSL/TLS automatically