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.
- 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
- Redirect Lookup: On each request, the service checks the Fastly KV Store for matching redirects
- Path Matching: Matches both URI pathname (e.g.,
/old-page) and absolute URLs - Redirect or Pass-Through: If a redirect is found, returns the appropriate redirect response; otherwise forwards to the backend
- Webhook Updates: Exposes a webhook endpoint (
/api/fastly/update-redirects) that triggers redirect refresh from Agility CMS
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)
- 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-secretswithfetch-api-key
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"Create a KV Store named cbiredirects in your Fastly service.
- Create a Secret Store named
cbi-secrets - Add a secret with key
fetch-api-keycontaining your Agility CMS Fetch API key - For local development, create a file
.env.apikeywith your API key
Update the guid in src/getRedirects.js:
const guid = "your-agility-guid"# Install dependencies
npm install
# Or with yarn
yarn installnpm run startThis starts the Fastly local development server with the configured backends and KV stores.
npm run buildThis command:
- Runs webpack to bundle the JavaScript files
- Compiles the bundle to WebAssembly using
js-compute-runtime
Deploy to your Fastly Compute service:
npm run deployWebhook 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"
- src/index.js - Main request handler and routing logic
- src/getRedirects.js - Fetches redirects from Agility CMS API
- src/updateRedirects.js - Updates KV Store with latest redirects
- fastly.toml - Fastly service configuration
- webpack.config.js - Webpack bundling configuration
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
}
}- Client makes request to Fastly edge
- Service checks if request is POST to
/api/fastly/update-redirects- If yes, triggers redirect update from Agility CMS
- Service loads redirects from KV Store
- Service checks for matching redirect by pathname or full URL
- If found, returns redirect response with appropriate status code
- Service filters non-GET requests (returns 405 for POST/PUT/PATCH/DELETE)
- Service forwards GET request to backend (
host_1)
For local development, create .env.apikey with your Agility CMS Fetch API key:
your-api-key-here
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.
- API keys are stored securely in Fastly Secret Store
- The service filters potentially harmful HTTP methods
- Never commit
.env.apikeyto version control
- 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
See LICENSE file for details.
For issues or questions about:
- Fastly Compute - See Fastly Documentation
- Agility CMS - Contact Agility CMS support