Skip to content

exporio/edge-sdk-hono

Repository files navigation

Logo

Exporio Edge SDK middleware for Hono framework

Exporio Edge SDK is a powerful tool for AB testing and Personalization, built on Cloudflare workers and for Cloudflare workers.

Test Release Package version Npm type definition License


ℹ️ Exporio is currently in Private Beta.

✉️ For early access, please contact us at tom@exporio.cloud.

🌎 Visit our website for more information.


🔥 Introduction

The scheme below illustrates the request-response lifecycle, showing how Exporio Middleware interacts with a separate Exporio-hosted Worker, which provides instructions for each request.

Request flow

🚀 Quick Start

The Edge SDK requires setting up a Cloudflare Worker with the Hono web framework.

Prerequisite: You need to sign-up for a Cloudflare account, and add your domain to Cloudflare so that Cloudflare is able to resolve your domain.

1. Get started with Cloudflare Workers by following this guide.

2. Install the Exporio Edge SDK in your worker project:

npm install @exporio/edge-sdk-hono

3. Update your worker code (index.ts) as follows:

import { Hono } from 'hono'
import { exporioMiddleware } from '@exporio/edge-sdk-hono'

type Variables = { contentUrl: string }

const app = new Hono<{ Variables: Variables }>()

app.use(
    '*',
    exporioMiddleware({
        url: 'https://edge-api.exporio.cloud',
        apiKey: 'EXPORIO_API_KEY',
    })
)

app.all('*', async (c) => {
    // https://developers.cloudflare.com/workers/runtime-apis/fetch-event/#passthroughonexception
    c.executionCtx.passThroughOnException()

    const contentUrl = c.get('contentUrl')
    const request = new Request(contentUrl, c.req)

    const response = await fetch(request)

    return new Response(response.body, response)
})

export default app

Key points to note:

  • Replace EXPORIO_API_KEY with your API Key from the Exporio UI (Website Settings > API Keys).
  • The Exporio middleware will set a new contentUrl to fetch from in the case of a Split URL test, otherwise, the current request URL will be returned. Retrieve it with c.get('contentUrl').
  • A new Response must always be returned at the end to allow the middleware to modify the response - return new Response(response.body, response).

4. Configure your wrangler.toml:

account_id = "..."  # Cloudflare account id
name = "..."

main = "./src/index.ts"
compatibility_date = "2022-11-22"

route = "https://your_domain.com/*"

5. Deploy your wroker:

wrangler publish

Minimal example source code.

🎉 Congratulations! You can now start running A/B tests and personalizations on your website!

⚠️ Important

Consider the following aspects of implementation that can affect your worker and website, although they are not directly related to Exporio Middleware.

Static routes

Be sure to disable static routes so the worker doesn't interfere with them. Create a new path without running services in the Workers Routes section of Cloudflare. More details can be found in this community topic.

📄 License

This project is licensed under the terms of the MIT license.