<!-- Cloudflare frameworks — Heystack docs. Rendered: https://heystack.dev/docs/cloudflare-frameworks · Index: https://heystack.dev/llms.txt -->
# Cloudflare frameworks

How to wire Heystack into a framework that runs on Cloudflare Workers. In every case the Worker deploys with Cloudflare's native trace and log export enabled, and the SDK adds business context on top. Import only from `@heystack/otel/workers` (or `@heystack/otel/next` for Next.js); never import `@heystack/otel/node` in code that runs on the Workers runtime. Install `@heystack/otel` version 0.19 or newer.

All sections use the same observability block in `wrangler.toml` (or the equivalent `wrangler.jsonc`), with the destination names you created in Cloudflare (see [Cloudflare Workers](https://heystack.dev/docs/cloudflare-workers) for creating them):

```toml
[observability.traces]
enabled = true
destinations = ["heystack-traces"]
head_sampling_rate = 1
persist = false

[observability.logs]
enabled = true
destinations = ["heystack-logs"]
head_sampling_rate = 1
persist = false
```

Where a framework hides the Worker entry, the safest place to call `instrument()` is the file your `main` setting points at. Coverage of native spans follows the [coverage table](https://heystack.dev/docs/cloudflare-workers#signal-coverage); verify delivery by making a request after deploying.

## Next.js on OpenNext

Entry file: `instrumentation.ts` at the project root.

```ts
export async function register() {
  const { registerHeystack } = await import("@heystack/otel/next");
  await registerHeystack({ service: "my-app" });
}
```

`registerHeystack` detects Cloudflare automatically and uses the Workers path. Add the `observability` block above to the Wrangler config that OpenNext deploys. See the [Next.js guide](https://heystack.dev/docs/nextjs) for flushing and the browser SDK.

**Need `routes` or the other `instrument()` options?** `registerHeystack` does not take them. Use OpenNext's [custom worker](https://opennext.js.org/cloudflare/howtos/custom-worker) instead: create a worker file that imports the generated `./.open-next/worker.js`, wraps its `fetch` with `instrument()`, re-exports the rest, and point Wrangler `main` at that file.

```ts
// custom-worker.ts
// @ts-expect-error `.open-next/worker.js` is generated at build time
import handler from "./.open-next/worker.js";
import { instrument } from "@heystack/otel/workers";

export default {
  fetch: instrument(handler, { service: "my-app", routes: ["/products/:id"] }).fetch,
};

// Re-export the Durable Object classes your OpenNext config uses (names depend on your setup).
// @ts-expect-error generated at build time
export { DOQueueHandler, DOShardedTagCache } from "./.open-next/worker.js";
```

Do not also call `registerHeystack` in that setup.

## React Router v7 and Remix

Entry file: the Worker file named by `main` in Wrangler (commonly `workers/app.ts`), which creates the request handler and exports `{ fetch }`.

```ts
import { createRequestHandler } from "react-router";
import { instrument } from "@heystack/otel/workers";

const handler = createRequestHandler(
  () => import("virtual:react-router/server-build"),
  import.meta.env.MODE,
);

export default instrument(
  {
    async fetch(request: Request, env: Env, ctx: ExecutionContext) {
      return handler(request, { cloudflare: { env, ctx } });
    },
  },
  { service: "my-app", routes: ["/projects/:id", "/projects/:id/*"] },
);
```

For Remix on Workers, wrap the exported `fetch` the same way. Add the `observability` block to the Wrangler config.

## Astro

Version note: this section describes Astro 6 with `@astrojs/cloudflare` v13. Earlier adapter versions used the `workerEntryPoint` option and `Astro.locals.runtime.ctx`; those were removed in v13.

Entry file: a custom Worker file. The adapter's default Wrangler `main` is `@astrojs/cloudflare/entrypoints/server`, which cannot be wrapped, so point `main` at your own file (for example `./src/worker.ts`) and do not also use the adapter's default handler:

```ts
import { handle } from "@astrojs/cloudflare/handler";
import { instrument } from "@heystack/otel/workers";

export default instrument(
  { fetch: (request: Request, env: Env, ctx: ExecutionContext) => handle(request, env, ctx) },
  { service: "my-site" },
);
```

In server code, add business spans with `Astro.locals.cfContext` as the context: `withCloudflareSpan(Astro.locals.cfContext, "page.render", {}, () => ...)`. Add the `observability` block to the Wrangler config.

## SvelteKit

Entry file: the Worker produced by `@sveltejs/adapter-cloudflare`. Adapter output has no hook for wrapping the Worker, so rely on native export for platform spans and add business spans from `hooks.server.ts`:

```ts
import { withCloudflareSpan } from "@heystack/otel/workers";
import type { Handle } from "@sveltejs/kit";

export const handle: Handle = ({ event, resolve }) => {
  const ctx = event.platform?.ctx; // undefined while prerendering
  if (!ctx) return resolve(event);
  return withCloudflareSpan(ctx, "sveltekit.request", { "http.route": event.route.id ?? "" }, () => resolve(event));
};
```

`event.platform` is undefined during prerender, so the code above skips the span there. Check that your adapter version exposes `ctx` on `platform` (older versions called it `context`).

Add the `observability` block to `wrangler.toml`.

## Nuxt

Entry file: Nitro's Cloudflare preset generates the Worker. Keep native export on and add business spans from a server route using `withCloudflareSpan` with the request's Cloudflare context.

Nuxt 3/4 (Nitro 2):

```ts
import { withCloudflareSpan } from "@heystack/otel/workers";

export default defineEventHandler((event) => {
  const ctx = event.context.cloudflare?.context;
  if (!ctx) return loadOrders(); // not running on Cloudflare (local dev, prerender)
  return withCloudflareSpan(ctx, "nuxt.api.orders", {}, async () => loadOrders());
});
```

Nitro 3 exposes the Cloudflare runtime as `event.req.runtime.cloudflare` instead. Check the execution context field on that object for your Nitro version before using it, and guard against it being absent as above.

Add the `observability` block to `wrangler.toml`.

## Hono

Entry file: the file that creates the `Hono` app and exports it as the Worker default.

```ts
import { Hono } from "hono";
import { instrument } from "@heystack/otel/workers";

const app = new Hono<{ Bindings: Env }>();
app.get("/users/:id", (c) => c.json({ id: c.req.param("id") }));

export default instrument({ fetch: app.fetch }, {
  service: "my-api",
  routes: ["/users/:id"],
});
```

`instrument()` wraps any object with a `fetch` method, so pass Hono's `app.fetch`. Add the `observability` block to `wrangler.toml`.

## Notes that apply everywhere

- Pages Functions use a different integration: see [Cloudflare platforms](https://heystack.dev/docs/cloudflare-platforms).
- If a framework build bundles server code for Node, make sure it resolves `@heystack/otel/workers`, not `/node`.
- Native mode needs no `HEYSTACK_API_KEY` in the Worker. Direct mode (`nativeTracing: "direct"`) does, as a Wrangler secret.
