This repository houses the Cloudflare Workers Starter Kit which provides a quickstart for users who would like to use Optimizely Feature Experimentation and Optimizely Full Stack (legacy) with Cloudflare Workers.
Optimizely Feature Experimentation is an A/B testing and feature management tool for product development teams that enables you to experiment at every step. Using Optimizely Feature Experimentation allows for every feature on your roadmap to be an opportunity to discover hidden insights. Learn more at Optimizely.com, or see the developer documentation.
Optimizely Rollouts is free feature flags for development teams. You can easily roll out and roll back features in any application without code deploys, mitigating risk for every feature on your roadmap.
Refer to the Optimizely Cloudflare Workers Starter Kit documentation for detailed instructions about using this starter kit.
-
You will need an Optimizely Account. If you do not have an account, you can register for a free account.
-
You will need to have a Cloudflare Account with Workers. For more information, visit the official Cloudflare Workers product page here.
-
You will need to have Wrangler CLI installed. If you do not have it, you can install it by visiting the Cloudflare Wrangler CLI page here.
-
Generate a project using this sample template.
wrangler generate projectname https://github.com/optimizely/cloudflare-worker-template
-
Add
account_id
inwrangler.toml
. If you dont know the account ID, just dowrangler dev
and the CLI will prompt you with the account ID and the instructions to add it. -
Install node packages.
npm install
The Optimizely starter kit for Cloudflare Workers embeds and extends our Javascript (Node) SDK. For a guide to getting started with our platform more generally, you can reference our Javascript (Node) Quickstart developer documentation.
Note: This starter kit in particular makes use of the "Lite" version of our Javascript SDK for Node.js which explicitly excludes the datafile manager and event processor features for better performance. As a result, it is expected that you will provide the datafile manually to the Optimizely SDK either through a local file reference or by using the provided platform-specific
getDatafile()
helper to load in your Optimizely project's datafile.
Sample code is included in src/main.js
that shows examples of initializing and using the Optimizely JavaScript (Node) SDK interface for performing common functions such as creating user context, adding a notification listener, and making a decision based on the created user context.
Additional platform-specific code is included in src/optimizely_helper.js
which provide workarounds for otherwise common features of the Optimizely SDK.
-
Update your Optimizely
sdkKey
,flagKey
anduserId
insrc/index.js
. Your SDK keys can be found in the Optimizely application under Settings. -
Test and debug the worker locally.
wrangler dev
-
Deploy the worker on Cloudflare.
wrangler publish
-
Optionally, tail the logs for debugging when accessing worker deployed on Cloudflare.
wrangler tail -f pretty
This template uses Cloudflare cache API to provide performant caching for the Optimizely Datafile.
Out of the box, Optimizely's Feature Experimentation SDKs require a user-provided identifier to be passed in 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.
For more information on how Optimizely Feature Experimentation SDKs assign users to feature flags and experiments, see the documentation on how bucketing works.
For more information about Cloudflare Workers, you may visit the following resources:
- Cloudflare Workers
- Cloudflare Workers documentation
- Cloudflare Workers tutorials
- Cloudflare Workers with Optimizely documentation
Please see CONTRIBUTING.
-
Flutter - https://github.com/optimizely/optimizely-flutter-sdk
-
JavaScript - https://github.com/optimizely/javascript-sdk
-
Akamai EdgeWorkers - https://github.com/optimizely/akamai-edgeworker-starter-kit
-
AWS Lambda@Edge - https://github.com/optimizely/aws-lambda-at-edge-starter-kit
-
Fastly Compute@Edge - https://github.com/optimizely/fastly-compute-starter-kit
-
Vercel Functions - https://github.com/optimizely/vercel-examples/tree/main/edge-functions/feature-flag-optimizely