Deployment guide for Cloudflare Workers
Instructions on deploying Optimizely Edge Agent to Cloudflare Workers.
BetaOptimizely Edge Agent is in beta. Apply on the Optimizely beta signup page or contact your Customer Success Manager.
Prerequisites
You must install the Wrangler CLI to manage Cloudflare Workers.
Get started
Generate a project
Generate a project using the template hosted on GitHub:
wrangler generate {projectname} REPOSITORY PLACEHOLDERConfigure wrangler.toml file
wrangler.toml file- Rename the
wrangler.toml.templatefile in the root of the project towrangler.toml. - Update the variables inside by adding your
account_idandkv_namespace(s)if you plan on using the KV Store for datafile and flag keys management.
wrangler.toml.template file example:
#:schema node_modules/wrangler/config-schema.json
name = "optly-hybrid-agent"
main = "src/index.js"
compatibility_date = "2024-05-02"
compatibility_flags = ["nodejs_compat"]
account_id = "YOUR ACCOUNT ID HERE"
workers_dev = true
route = "YOUR ROUTE GOES HERE"
kv_namespaces = [
{
binding = "OPTLY_HYBRID_AGENT_KV",
id = "KV NAMESPACE ID GOES HERE",
preview_id = "KV PREVIEW NAMESPACE ID GOES HERE"
},
{
binding = "OPTLY_HYBRID_AGENT_UPS_KV",
id = "KV USER PROFILE NAMESPACE ID GOES HERE",
preview_id = "KV USER PROFILE PREVIEW NAMESPACE ID GOES HERE"
}
]
[vars]
# Supported: "debug, debugExt, info, error"
LOG_LEVEL = "debug"
# TESTING_FLAG_DEBUG = "INDIVIDUAL FX FLAG KEY FOR TESTING GOES HERE"
Update Project Configuration
Update the project configuration settings in the requestConfig.js file located in the config directory. See the requestConfig.js documentation for information.
Install node modules
Install the necessary Node.js modules using the following command:
npm installTest and debug locally
Using Wrangler CLI, you can test and debug the worker locally. This supports local debugging and lets you set breakpoints.
wrangler devVisual Studio (VS) Code launch configuration
The following is an example VS Code launch configuration that attaches the VS Code debugger to the local development Cloudflare Edge Worker for debugging purposes.
{
"version": "0.2.0",
"configurations": \[
{
"type": "pwa-node",
"request": "attach",
"name": "Attach to Cloudflare Worker",
"skipFiles": ["<node_internals>/**"],
"port": 9229,
"localRoot": "${workspaceFolder}",
"remoteRoot": "/src"
}
]
}Deploy the Cloudflare Worker
To deploy the Worker to Cloudflare, run the following:
wrangler publishTail logs for debugging (optional)
To debug and tail the logs when accessing Workers deployed on Cloudflare, run the following:
wrangler tail -f prettyUpdated 2 days ago