Optimizely will be sunsetting Full Stack Experimentation on July 29, 2024. See the recommended Feature Experimentation migration timeline and documentation.

Dev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideLegal TermsGitHubDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

Cloudflare Workers

Template hosted in GitHub for running Optimizely Full Stack feature flags and experiments on Cloudflare Workers.

Cloudflare Workers

Cloudflare Workers provides a serverless execution environment that allows you to create new applications or augment existing ones 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 0ms 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

For example use cases of Cloudflare Workers, check out their documentation with various examples.

Restrictions and Limitations

Cloudflare Workers have different plan limits and restrictions based on your account. Please refer to the official Cloudflare Limit documentation for more information.

The Optimizely worker template for Cloudflare worker embeds and extends our JavaScript SDK to provide a starting point for you to implement experimentation and feature flagging for your experiences at the edge. For a guide to getting started with our platform more generally, this can be combined with the steps outlined in our JavaScript Quickstart.

Optimizely + Cloudflare Worker Template

The Optimizely Cloudflare Workers template embeds and extends our Javascript Node SDK to provide a starting point for you to implement experimentation and feature flagging for your experiences at the edge. For a guide to getting started with our platform more generally, this can be combined with the steps outlined in our Javascript Quickstart.

Caching with Cloudflare

This template uses Cloudflare cache API to provide performant caching for the Optimizely Datafile.

Identity Management

Out of the box, Optimizely's Full Stack 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.

Bucketing

For more information on how Optimizely Full Stack SDKs assign users to feature flags and experiments, see the documentation on how bucketing works.

How to use

Prerequisites

You will need to have these prerequisites to use this template:

Get started

  1. Generate a project using the template hosted on GitHub:
wrangler generate projectname https://github.com/optimizely/cloudflare-worker-template
  1. Add account_id in wrangler.toml. If you do not know the account ID, just do wrangler dev and the CLI will prompt you with the account ID and the instructions to add it.
  2. Install node nodules.
npm install
  1. Update your Optimizely sdkKey, flagKey and userId in src/index.js. Your SDK keys can be found in the Optimizely application under Settings.
  2. Test and debug the worker locally:
wrangler dev
  1. Deploy the worker on Cloudflare:
wrangler publish
  1. Tail the logs for debugging when accessing worker deployed on Cloudflare (optional):
wrangler tail -f pretty

Additional resources