Cloudflare Workers
Template hosted in GitHub for running Optimizely Feature Experimentation feature flags and experiments on Cloudflare Workers.
Cloudflare Workers provide a serverless execution environment that lets you create or augment existing applications without configuring or maintaining infrastructure.
Advantages of Cloudflare Workers
-
Automatic scaling – Traffic is automatically routed and load balanced across servers.
-
High-performance global network – Your code is powered by Cloudflare's global network.
-
Multiple programming languages – You can write in JavaScript, Rust, C, and C++.
-
No cold starts – Cloudflare supports 0 MS cold starts.
Note
Although you can write Cloudflare Workers code in various languages, Optimizely currently only provides a template for JavaScript.
Cloudflare Workers use cases
See Cloudflare Examples documentation.
Restrictions and limitations
Cloudflare Workers have different plan limits and restrictions based on your account. See the official Cloudflare Limit documentation.
Optimizely + Cloudflare Worker template
The Optimizely Feature Experimentation Cloudflare Workers template embeds and extends the Javascript Node SDK to provide a starting point for you to implement experimentation and feature flagging for your experiences at the edge. For a general guide to getting started with Optimizely, see the Javascript SDK Quickstart.
Caching with Cloudflare
This template uses Cloudflare cache API to provide performant caching for the datafile.
Identity management
Optimizely's Feature Experimentation SDKs require passing a user-provided identifier at runtime to drive experiment and feature flag decisions. This example generates a unique ID, stores it in a cookie, and reuses it to make the decisions sticky. Alternatively, you can use an existing unique identifier available within your application and pass it in as the value for the OPTIMIZELY_USER_ID cookie.
Bucketing
See How bucketing works.
How to use
Prerequisites
- You must install the Wrangler CLI for this template.
Get started
- Generate a project using the template hosted on GitHub:
wrangler generate projectname https://github.com/optimizely/cloudflare-worker-template
- Add
account_id
inwrangler.toml
. If you do not know the account ID, type inwrangler whoami
and you will see the account ID listed. - Install node nodules.
npm install
- Update your Optimizely Feature Experimentation
sdkKey
,flagKey
anduserId
insrc/index.js
. Your SDK keys are in the Optimizely application under Settings. - Test and debug the worker locally:
wrangler dev src/index.js
- Deploy the worker on Cloudflare:
wrangler publish src/index.js
- (Optional) Tail the logs for debugging when accessing workers deployed on Cloudflare:
wrangler tail --format "pretty"
Additional resources
Updated 6 months ago