Skip to content

Commit 00c2bb6

Browse files
committed
feat: add Cloudflare Workers utility package and demo
Add @convertcom/js-sdk-cloudflare with edge-specific helpers for running Convert experiments inside Cloudflare Workers: KV-backed DataStore adapter, edge config cache, cookie helpers, and variation-aware cache utilities. Includes a demo Worker (demo/cloudflare-workers/) demonstrating page-level A/B testing with HTMLRewriter, asset swaps, split URL redirects, and SPA injection patterns. Updates release-please config, publish workflows, and root build scripts to support the new package.
1 parent 53feea4 commit 00c2bb6

18 files changed

Lines changed: 1848 additions & 18 deletions

.github/workflows/publish-package.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ jobs:
8888
(cd packages/experience && npm publish --access public)
8989
env:
9090
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
91+
- if: ${{ startsWith(env.RELEASE_TAG, 'js-sdk-cloudflare-v') }}
92+
run: |
93+
yarn cloudflare:build
94+
(cd packages/cloudflare && npm publish --access public)
95+
env:
96+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
9197
- if: ${{ startsWith(env.RELEASE_TAG, 'js-sdk-v') }}
9298
run: |
9399
yarn sdk:build

.github/workflows/release-please.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,6 @@ jobs:
5656
packages/data) yarn data:build && (cd packages/data && npm publish --access public) ;;
5757
packages/experience) yarn experience:build && (cd packages/experience && npm publish --access public) ;;
5858
packages/js-sdk) yarn sdk:build && (cd packages/js-sdk && npm publish --access public) ;;
59+
packages/cloudflare) yarn cloudflare:build && (cd packages/cloudflare && npm publish --access public) ;;
5960
*) echo "Unknown path: $REL_PATH" && exit 1 ;;
6061
esac

.release-please-manifest.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
"packages/segments": "2.1.2",
1111
"packages/api": "2.1.4",
1212
"packages/data": "3.3.3",
13-
"packages/experience": "2.3.2"
13+
"packages/experience": "2.3.2",
14+
"packages/cloudflare": "1.0.0"
1415
}

demo/cloudflare-workers/README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Convert SDK — Cloudflare Workers Demo
2+
3+
A complete example of running Convert A/B tests at the Cloudflare edge with zero client-side flicker.
4+
5+
## What This Demonstrates
6+
7+
1. **Page-level A/B test** — HTMLRewriter modifies page content before delivery
8+
2. **Asset / image swap** — Replace images or stylesheets per variation
9+
3. **Split URL redirect** — Serve entirely different origin pages transparently
10+
4. **SPA injection** — Inject bucketing decisions as JSON for client-side SPAs
11+
5. **Edge caching** — Cache origin responses per variation
12+
13+
## Setup
14+
15+
### 1. Install Dependencies
16+
17+
```bash
18+
yarn install
19+
```
20+
21+
### 2. Create a KV Namespace
22+
23+
```bash
24+
wrangler kv namespace create CONVERT_KV
25+
```
26+
27+
Copy the output `id` into `wrangler.toml`.
28+
29+
### 3. Configure
30+
31+
Edit `wrangler.toml`:
32+
- Set your KV namespace ID
33+
- Set your Convert SDK key (`CONVERT_SDK_KEY`)
34+
35+
### 4. Update Experiment Keys
36+
37+
In `src/index.ts`, replace `'your-experience-key'` with your actual experience key from the Convert dashboard.
38+
39+
### 5. Run
40+
41+
```bash
42+
# Local development
43+
yarn dev
44+
45+
# Deploy to production
46+
yarn deploy
47+
48+
# View live logs
49+
yarn tail
50+
```
51+
52+
## How It Works
53+
54+
```
55+
Visitor → Cloudflare Edge → Worker
56+
├── Read config from KV (cached, ~1ms)
57+
├── Read visitor data from KV (~1ms)
58+
├── SDK: bucket visitor into variation
59+
├── Fetch origin page
60+
├── HTMLRewriter: modify HTML per variation
61+
├── Set visitor cookie
62+
└── Respond (total edge overhead: ~5-8ms)
63+
64+
└── Background (waitUntil):
65+
├── Save bucketing to KV
66+
└── Send tracking event to Convert
67+
```
68+
69+
## Documentation
70+
71+
Full guide: [Cloudflare Workers Edge Experimentation](https://github.com/convertcom/javascript-sdk/wiki/CloudflareWorkers)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "@convertcom/js-sdk-demo-cloudflare-workers",
3+
"version": "1.0.0",
4+
"author": "Convert Insights, Inc",
5+
"license": "Apache-2.0",
6+
"private": true,
7+
"scripts": {
8+
"dev": "wrangler dev",
9+
"deploy": "wrangler deploy",
10+
"tail": "wrangler tail"
11+
},
12+
"dependencies": {
13+
"@convertcom/js-sdk": "^4.3.4",
14+
"@convertcom/js-sdk-cloudflare": "^1.0.0"
15+
},
16+
"devDependencies": {
17+
"@cloudflare/workers-types": "^4.20241205.0",
18+
"typescript": "^5.9.3",
19+
"wrangler": "^3.99.0"
20+
}
21+
}

0 commit comments

Comments
 (0)