LogoLogo
  • Welcome to the Spec Docs
  • Getting Started
    • DNS Integration
  • Service Workers
    • AWS Edge@Lambda
    • Cloudflare Worker
    • Fastly Compute
  • Concepts
    • Processing Modes
    • Testing
    • Regional Deployments
    • Data Collection
Powered by GitBook
On this page
  • Prerequisites
  • Instructions
  • Configuring Worker Route
  • Spec Worker Library
  • Worker Example
  1. Service Workers

Cloudflare Worker

This document describes integrating with the Spec Platform through a Cloudflare worker.

PreviousAWS Edge@LambdaNextFastly Compute

Last updated 7 months ago

Prerequisites

Please review the following and ensure that that your development environment has the relevant dependencies installed with the latest version:

  1. Follow Cloudflare’s “” to install Wrangler in your development environment.

    1. This is the Cloudflare Workers command-line interface, which allows you to create, edit, and publish Workers projects to your Cloudflare account.

    2. Following installation, .

  2. The for Cloudflare

  3. Check out the configuration options for the Spec Platform in .

Instructions

  1. , or open an existing project.

  2. Install into project:

    1. npm i –save @specprotected/spec-proxy-cloudflare-worker

  3. Open src/index.ts and copy the Spec Worker Library code from the .

  4. . Open a terminal window and run wrangler deploy

Configuring Worker Route

With the worker published, we can now use the Cloudflare admin console to configure a route that will use the Spec Worker code.

  1. Click "Workers & Pages" from the Cloudflare sidebar

  2. Select the newly published worker and navigate to "Settings".

  3. Next to "Domains and Routes", click "Add" to add a new route for the worker.

    1. Select "Custom domain" or "Route" and fill out the relevant fields

    2. Confirm your changes by clicking "Add domain" or "Add route"

  4. The Spec Cloudflare Worker is now live!

Spec Worker Library

If no existing workers exist, the following is Spec's recommended worker code. If there are existing workers, Spec's Product Success will assist in merging the following code with your existing code.

Worker Example

// Import the Spec Proxy library function
import { specProxyProcess } from "@specprotected/spec-proxy-cloudflare-worker";

// Normal Edge Worker event listener
addEventListener("fetch", (event) => {
  // If we don't catch the exception, fail open to original traffic path
  event.passThroughOnException();
  
  // Config object that may be hardcoded or use Cloudflare environment variables
  const config = {
    inlineMode: true,
    disableSpecProxy: false
  };
  
  // Spec library processes the request and returns a new, modified one back to you.
  // Please ensure that you use the returned request as the destination request
  // of your worker script. This will ensure that traffic is routed appropriately.
  let request = specProxyProcess(event, config);

  // This is the simplest form where the worker script doesn't do anything else
  event.respondWith(request);
});

Check page for configuration options.

Get started guide
authenticate wrangler
Configure a new project with Wrangler
NPM Spec Proxy Cloudflare Worker
Publish the worker
Spec Worker Library
Prerequisites
Config
Edge Workers