Code Examples
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.
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.
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.
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.
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.
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.
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.
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””.