Code bundle format

To create the EdgeWorkers bundle, you need a JavaScript source file called main.js that contains event handler definitions for when to execute the code:

  • onClientRequest. This event happens for every request as the request is received, before checking if a response is available in cache.

  • onOriginRequest. This event happens just before sending the request to the origin.

  • onOriginResponse. This event happens as the origin response is created.

  • onClientResponse. This event happens for every request just before the client response is sent.

  • responseProvider. This event happens if the response is not already found in cache. You can use responseProvider to generate dynamic responses from various content and API sources.

  • onBotSegmentAvailable. If your property has been configured to use Bot Manager Premier, this event happens after onClientRequest and before checking if a response is available in cache.

You also need to create a manifest file called bundle.json that includes meta information as described in the table below.

Setting nameTypeDescriptionRequired
edgeworker-versionStringUnique identifier for the EdgeWorkers version.
bundle-versionIntegerBundle format version.
api-versionStringVersion of JavaScript API when the EdgeWorkers code was created.
miscObjectInformation you can include in the manifest to identify the EdgeWorkers function.
descriptionStringPhrase describing the EdgeWorkers script function.
{
   "edgeworker-version":"4.22.3",
   "bundle-version":1,
   "api-version":"0.3",
   "description":"Language Redirect",
   "misc":{
      "author":"jsmith",
      "git-commit":"4ce52c8ff4fdd1771e5347297ea5691b55c4f6ed",
      "updated":"2027-09-23"
   }
}

You can also add support for subWorkers, Flexible composition, the True-Client-IP request header and set the JavaScript logging level in the bundle.json file.

ObjectSetting nameTypeDescription
configclient-ip-headerBooleanWhen set to true, it enables the True-Client-IP request header. By default, this configuration setting is set to false.
config.subrequestinvoke-for-edgeworker-clientsBooleanWhen set to true, it enables support for subWorkers. By default, this configuration setting is set to false.
config.loggingSupports thelevel, schema, and ds2id config settings.ObjectSet the JavaScript logging in the code bundle and deliver these logs using a DataStream 2 stream. By default, JavaScript logs are set to ERROR.
levelTrace, Debug, Info, Warn, or ErrorThe default JavaScript logging level applied to the EdgeWorkers code bundle. You can use this parameter to override the default log level, ERROR.
ds2idIntegerThe Data Stream 2 stream id to associate with the JavaScript logging data.
The stream needs to be active before you include it in the EdgeWorkers code bundle.
When you add the logging config setting to the bundle.json file, the ds2id is required.
schemaStringNot currently in use. Use v1 as a placeholder.
dependencies.childSupports the edgeWorkerId and version settings.ObjectAdd information about Flexible composition
edgeWorkerIdStringUnique identifier for the child EdgeWorkers version.
versionIntegerVersion number of the child EdgeWorker version.

Here's an example that includes all the supported settings in the bundle.json file.

{
   "edgeworker-version":"4.22.3",
   "bundle-version":1,
   "api-version":"0.3",
   "description":"Language Redirect",
   "misc":{
      "author":"jsmith",
      "git-commit":"4ce52c8ff4fdd1771e5347297ea5691b55c4f6ed",
      "updated":"2027-09-23"
    },
    "config": {
        "logging": {
            "level": "warn",
            "ds2id": 123234,
            "schema":"v1"
        },
        "subrequest": {
            "invoke-for-edgeworker-clients": false
        },
        "client-ip-header": false
    },
    "dependencies":{
        "child": {
            "edgeWorkerId":12345, 
            "version": "active"
        }
    }
}