Course contentsShow
← Cloud and Infrastructure
Lesson 618 of 2,98911. CDN and Edge ComputingFree lesson

Edge Function Triggers and Events

Different event types that trigger edge functions: viewer requests, origin requests, responses, and their use cases.

Edge Function Triggers and Events

You'll learn how different event types trigger edge functions and when to use each one.

What Are Edge Function Triggers?

Edge functions don't run all the time—they activate when specific events occur during a request's journey through the CDN. Think of these triggers like motion sensors in different rooms of a house: each sensor (event type) detects activity at a specific point and triggers an action.

The Four Main Event Types

1. Viewer Request

Fires when the CDN receives a request from a user's browser, before checking the cache.

Use case: URL rewriting, authentication checks, A/B testing redirects, bot detection.

2. Origin Request

Fires when the CDN needs to fetch content from your origin server (cache miss), before contacting the origin.

Use case: Adding custom headers for origin authentication, modifying origin paths, dynamic origin selection.

3. Origin Response

Fires when the CDN receives a response from your origin, before caching it.

Use case: Header modifications, transforming responses before caching, error page customization.

4. Viewer Response

Fires just before sending the final response back to the user, whether from cache or origin.

Use case: Adding security headers, final response modifications, cookie manipulation.

Choosing the Right Event

  • Use viewer request for early decisions that don't need origin data
  • Use origin request/response when you need to interact with or modify origin behavior
  • Use viewer response for final touches applied to all responses

Key Takeaway: Edge functions trigger at four distinct points—viewer request, origin request, origin response, and viewer response—allowing you to intercept and modify requests and responses at precisely the right moment in the CDN flow.