Cloudflare Astro Framework Guide

Learn how to add Cloudflare instrumentation to your Astro app.

If you're running your Astro app on Cloudflare Pages, you can use the Sentry Astro SDK in combination with the Sentry Cloudflare SDK to add Sentry instrumentation.

First, install the Sentry Astro SDK in your application. We recommend setting up the Astro SDK manually and manually initializing the SDK. Make sure you have a sentry.client.config.js file created, but do not add a sentry.server.config.js file.

After installing the Sentry Astro SDK, you can now install the Sentry Cloudflare SDK. First, install the SDK with your package manager:

Copied
npm install @sentry/cloudflare --save

To use the SDK, you'll need to set either the nodejs_compat or nodejs_als compatibility flags in your wrangler.toml. This is because the SDK needs access to the AsyncLocalStorage API to work correctly.

wrangler.toml
Copied
compatibility_flags = ["nodejs_compat"]
# compatibility_flags = ["nodejs_als"]

Then create a functions directory and add a _middleware.js file to it with the following code:

functions/_middleware.js
Copied
import * as Sentry from "@sentry/cloudflare";

export const onRequest = [
  // Make sure Sentry is the first middleware
  Sentry.sentryPagesPlugin((context) => ({
    dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
    // Set tracesSampleRate to 1.0 to capture 100% of spans for tracing.
    // Learn more at
    // https://docs.sentry.io/platforms/javascript/configuration/options/#traces-sample-rate
    tracesSampleRate: 1.0,
  })),
  // Add more middlewares here
];
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").