Skip to content

agility/cbi-fastly-compute

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Fastly Compute URL Redirect Service

A high-performance URL redirection service built on Fastly Compute@Edge that dynamically manages URL redirects from Agility CMS. This edge compute function intercepts requests, checks for configured redirects, and either redirects users or passes requests through to the origin backend.

Features

  • Edge-based URL redirection - Process redirects at the edge for minimal latency
  • Dynamic redirect management - Fetch and update redirects from Agility CMS via webhook
  • KV Store caching - Store redirects in Fastly KV Store for fast lookup
  • Support for multiple redirect types - Handles both pathname and absolute URL redirects
  • Configurable status codes - Support for 301 (permanent) and other redirect status codes
  • Backend pass-through - Automatically forwards non-redirected requests to the origin

How It Works

  1. Redirect Lookup: On each request, the service checks the Fastly KV Store for matching redirects
  2. Path Matching: Matches both URI pathname (e.g., /old-page) and absolute URLs
  3. Redirect or Pass-Through: If a redirect is found, returns the appropriate redirect response; otherwise forwards to the backend
  4. Webhook Updates: Exposes a webhook endpoint (/api/fastly/update-redirects) that triggers redirect refresh from Agility CMS

Architecture

Client Request
     ↓
Fastly Edge (this service)
     ↓
Check KV Store for redirect
     ↓
Redirect? → Yes → Return 301/302 with Location header
     ↓
     No
     ↓
Forward to Backend (Azure Static Web App)

Prerequisites

  • Node.js 18.x or later
  • Fastly CLI (@fastly/cli)
  • A Fastly account with Compute@Edge enabled
  • Agility CMS instance with URL Redirection module
  • Fastly KV Store named cbiredirects
  • Fastly Secret Store named cbi-secrets with fetch-api-key

Configuration

Backend Configuration

Configure backends in fastly.toml:

[local_server.backends.host_1]
  url = "https://cbihealthgroupuat.z9.web.core.windows.net"
  override_host = "cbihealthgroupuat.z9.web.core.windows.net"

[local_server.backends.api-ca]
  url = "https://api-ca.aglty.io"
  override_host = "api-ca.aglty.io"

KV Store Setup

Create a KV Store named cbiredirects in your Fastly service.

Secret Store Setup

  1. Create a Secret Store named cbi-secrets
  2. Add a secret with key fetch-api-key containing your Agility CMS Fetch API key
  3. For local development, create a file .env.apikey with your API key

Agility CMS Configuration

Update the guid in src/getRedirects.js:

const guid = "your-agility-guid"

Installation

# Install dependencies
npm install

# Or with yarn
yarn install

Development

Local Development Server

npm run start

This starts the Fastly local development server with the configured backends and KV stores.

Building

npm run build

This command:

  1. Runs webpack to bundle the JavaScript files
  2. Compiles the bundle to WebAssembly using js-compute-runtime

Deployment

Deploy to your Fastly Compute service:

npm run deploy

API Endpoints

POST /api/fastly/update-redirects

Webhook endpoint to trigger redirect updates from Agility CMS.

Request Body:

{}

Note: The webhook ignores requests with contentID or pageID properties (content/page updates).

Response:

  • 200 OK - "Redirects Updated"
  • 200 OK - "Ignoring content or page update webhook"

Code Structure

Redirect Data Format

Redirects are stored in KV Store as a JSON dictionary:

{
  "/old-page": {
    "url": "https://example.com/new-page",
    "statusCode": 301
  },
  "https://old-domain.com/page": {
    "url": "https://new-domain.com/page",
    "statusCode": 302
  }
}

Request Flow

  1. Client makes request to Fastly edge
  2. Service checks if request is POST to /api/fastly/update-redirects
    • If yes, triggers redirect update from Agility CMS
  3. Service loads redirects from KV Store
  4. Service checks for matching redirect by pathname or full URL
    • If found, returns redirect response with appropriate status code
  5. Service filters non-GET requests (returns 405 for POST/PUT/PATCH/DELETE)
  6. Service forwards GET request to backend (host_1)

Environment Variables

For local development, create .env.apikey with your Agility CMS Fetch API key:

your-api-key-here

Monitoring and Debugging

The service includes console logging for key operations:

  • Redirect fetches from Agility CMS
  • Update operations
  • Redirect matches

View logs in Fastly's real-time log streaming or your configured log endpoint.

Security

  • API keys are stored securely in Fastly Secret Store
  • The service filters potentially harmful HTTP methods
  • Never commit .env.apikey to version control

Performance Considerations

  • Redirects are cached at the edge in KV Store
  • Redirect lookup is O(1) dictionary access
  • No origin requests needed for redirected URLs
  • Update operations use locking mechanism to prevent concurrent updates

License

See LICENSE file for details.

Support

For issues or questions about:

About

The Repo for CBI Health's Fastly Compute implementation

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published