Code Examples

Icon Card background
A/B Testing

Randomly assigns a new user to a group for A/B testing. The assignment is stored in a cookie and passed to the origin in a query parameter. The A/B group can be forced via a query string parameter for easy testing. The group names, percentage split, cookie name, and query parameter name are configured through constants in the EdgeWorker JavaScript module.

View Case
Icon Card background
A/B Testing with EdgeKV

By moving the A/B test to the Akamai edge we can cache multiple variants of the same page close to the user making the decision of which variant to serve to the user without making the long round-trip to the origin web server or relying on client-side javascript.

View Case
Icon Card background
API Orchestration

Demonstrates how EdgeWorkers can be used to merge multiple internal APIs into a single API response from a user experience perspective.

View Case
Icon Card background
Autocomplete

Serves responses for popular search terms at the Edge. Autocomplete requests are typically long tail and frequently changing. Without an EdgeWorker, it is difficult to get up-to-date content from cache. Storing and serving the most popular search terms from the Edge will speed up responses significantly.

View Case
Icon Card background
Cachekey Device Type

This example includes the device type (tablet or mobile) from a PMUSER variable in the computed cache key for an Edge Server.

View Case
Icon Card background
Conference/Meeting Details

Implements a Conference Attendance Code API call that returns the meeting details of a conference as HTML if the user provides the correct code. With abc123 in the key GET parameter, the user is shown conference details. If the parameter is incorrect, an error is returned, keeping the information away from the browser to maintain confidentiality.

View Case
Icon Card background
Content Security Policy

A Content Security Policy (CSP) is a security feature implemented in web browsers to protect websites and web applications from attacks such as cross-site scripting (XSS) and data injection. CSP controls and limits the source of various types of content loaded and executed on a web page, including scripts, stylesheets, and images. EdgeWorkers provides numerous performance benefits for CSPs, which need to complement the security measures on the application server-side.

View Case
Icon Card background
Cookie Geolocation

Implements an EdgeWorker to add geolocation data to a cookie in the HTTP response. This cookie returns location information about the client where the request originates, including a lookup from custom data.

View Case
Icon Card background
Device Routing

Implements modification of the forward origin path of the URL to return device-specific content.

View Case
Icon Card background
Dynamic Callback

Demonstrates how an EdgeWorker can wrap a JSON response with a dynamic unique callback function leveraging Response Provider and Stream API for efficient content transformation. The EdgeWorker should be enabled on JSON requests containing a callback query parameter, which can be managed via Property Manager. When such a request comes in, this EdgeWorker removes the callback query parameter, makes a sub-request to fetch the JSON data, and serves it as a stream. The EdgeWorker code adds a prefix with the callback function name captured from the query parameter and a suffix. Both JSON data and transformed data can be cached using standard ““Caching”” behavior in Property Manager if caching is allowed.

View Case
Icon Card background
Ecommerce

This EdgeWorker tests for /commerce/categories in the URI path and responds to a GET parameter named search by running a case-insensitive regex against each title and description field, and a numeric comparison against each ID, returning an array of matching category entities, serialized as JSON. The response is generated at the Edge, so the origin server is not contacted, and the request is resolved at the first Edge server that answers it.

View Case
Icon Card background
EdgeKV Hello World

The Hello World example demonstrates how you can use EdgeWorkers and EdgeKV to implement a simple Dynamic Content Assembly use case whereby the HTML response is dynamically constructed on the edge based on the content of the Accept-Language header in the client request. It also shows how you could use the getText() helper method.

View Case
Icon Card background
Find Replace Stream

Demonstrates how an EdgeWorker can modify an HTTP response stream by performing a find & replace operation on the response. The example searches for specific text and replaces it with another across the entire response body. It accepts an optional parameter to specify the number of replacements. If not specified, the replacement occurs as many times as possible. The demo replaces ““This is the original string”” with ““This is the updated string””.

View Case
Icon Card background
Geolocation

This example implements a microservice GEO-Location API call that returns location information about the client where the request originates.

View Case
Icon Card background
Hello World

With this example you learn the basics of creating, deploying and debugging an EdgeWorker that generates a simple html page at the Edge and adds a response header.

View Case