Running on the edge with Flagship

📘 Javascript SDK - Engine compatibility

Our Javascript SDK is compatible with :

  • V8 Engine

  • Node.js runtime

  • Deno

Flagship JS SDK can be used in most edge environments provided by cloud platforms and CDNs

This example will guide you to implement Flagship JS SDK in CloudFlare Edge worker, but the rule is the same for any other Edge service using Javascript runtime.

Follow this link to setup a cloudFlare worker project

Once your project is set up, you need to install and initialize the Flagship JS SDK

yarn install @flagship.io/js-sdk

To properly use Flagship in an Edge service, you must follow these steps:

  1. Download the bucketing file from the the Flagship CDN

  2. Start the SDK at the beginning of your function and Play with it

  3. Close the SDK before to return a response

  4. Managing visitor cache

1) Download the bucketing file

The bucketing file is a JSON file containing all the data used by SDK to make a decision.

You need to download and import it in your script to initialize the SDK with.

To know how and when to download the bucketing file, use this following steps here

2. Start the SDK

You should start the Flagship SDK at the beginning of your function with decisionMode property to BUCKETING_EDGE. The initialBucketing property should be set with the data from the bucketing file, the advantage is that no network calls will be made when you call the FetchFlags method because all the flags are already there.

Here we use the cookie to store the visitorID, if there is no cookie, the SDK will generate one for the visitor.

We recommend using Flagship JS SDK lightweight bundle @flagship.io/js-sdk/dist/edge.js, it has small size and is optimized for Edge service.

3. Close the SDK

Flagship.close() should be called before the end of the script, this function will batch and send all hits generated when running the script. We recommend calling it in the background if possible

4. Managing visitor cache

On BUCKETING_EDGE mode, assignation is made on local so changing campaigns allocation in the platform would make the visitors to see different campaigns. Visitor cache will help you to always keep visitor in variation where he was allocated first, in the case of traffic allocation modifications (made by you or by the dynamic allocation). See more

Here we use Workers KV to store visitor cache

Full code

How to download bucketing file

Downloading The bucketing file is done by calling the Flagship CDN https://cdn.flagship.io/{FLAGSHIP_ENV_ID}/bucketing.json replacing FLAGSHIP_ENV_ID with your Environment ID from Flagship and place the JSON file where you can import it easily in your script.

You can manually download it in your code and deploy them together or you can use your CI/CD pipeline.

Here is an example of Github action workflow that downloads bucketing file and deploys application.

When does the bucketing file is required ?

The bucketing file must be downloaded the first time you deploy your app and each time you update your campaigns from Flagship.

Flagship has Webhooks Integrations that can help us to trigger our CI/CD pipeline when a campaing is updated.

When your campaign is updated, a hook called [environment] synchronized will be triggered. and we will use this event to trigger our CI/CD pipeline which will download the updated bucketing file and deploy our app.

\

Webhooks Integrations

\

Follow this link to know how to manually trigger a GitHub Actions workflow.

From Flagship plateforme go to settings -> integrations and webhooks tab

Choose [environment] synchronized event and set your Github api

Any feedback? Want another tutorial? Contact us

See full code here : github

Last updated

Was this helpful?