---
title: "Next.js on Cloudflare"
description: "Learn how to configure Sentry for your Next.js application deployed on Cloudflare Workers."
url: https://docs.sentry.io/platforms/javascript/guides/cloudflare/frameworks/nextjs/
---

# Next.js on Cloudflare

The Sentry Next.js SDK supports Next.js applications deployed on Cloudflare Workers, but requires additional configuration to run in this environment.

## [Prerequisites](https://docs.sentry.io/platforms/javascript/guides/cloudflare/frameworks/nextjs.md#prerequisites)

Before you begin, make sure you have:

* A Sentry [account](https://sentry.io/signup/) and [project](https://docs.sentry.io/product/projects.md)
* A [Cloudflare Workers project](https://dash.cloudflare.com/workers/overview)
* A Next.js application configured for Cloudflare Workers with [OpenNext](https://opennext.js.org/cloudflare)
* The Sentry SDK [installed and configured](https://docs.sentry.io/platforms/javascript/guides/nextjs.md) in your Next.js application

If you haven't deployed a Next.js app to Cloudflare Workers before, follow Cloudflare's [Next.js deployment guide](https://developers.cloudflare.com/workers/framework-guides/web-apps/nextjs/) to get started.

## [Configure Wrangler](https://docs.sentry.io/platforms/javascript/guides/cloudflare/frameworks/nextjs.md#configure-wrangler)

Once you've set up Sentry in your Next.js application following the [installation guide](https://docs.sentry.io/platforms/javascript/guides/nextjs.md), you'll need to configure [Wrangler](https://developers.cloudflare.com/workers/wrangler/) with the required compatibility settings.

First, add the `nodejs_compat` [compatibility flag](https://developers.cloudflare.com/workers/runtime-apis/nodejs/) to configure the Cloudflare Workers runtime to provide the Node.js APIs required by the Sentry SDK.

```json
{
  "compatibility_flags": ["nodejs_compat"]
}
```

*Other available variations of the above snippet: wrangler.toml*

Then, set the [compatibility date](https://developers.cloudflare.com/workers/configuration/compatibility-dates/) to `2025-08-16` or later. This is required to introduce `https.request` to the Cloudflare Workers runtime, which the Sentry SDK needs to send data.

Your final configuration should look like this:

```json
{
  "compatibility_flags": ["nodejs_compat"],
  "compatibility_date": "2025-08-16"
}
```

*Other available variations of the above snippet: wrangler.toml*

## [Wrap the Worker Entry](https://docs.sentry.io/platforms/javascript/guides/cloudflare/frameworks/nextjs.md#wrap-the-worker-entry)

Available since: `v11.6.0`

On Cloudflare Workers, your Next.js app runs inside the Worker entry that OpenNext generates (`.open-next/worker.js`). Wrap this entry with `withSentry` from `@sentry/nextjs/cloudflare` to get the same Next.js spans as on Node.js, plus Cloudflare data such as spans for D1 queries. Each request gets one `http.server` span with the name of the Next.js route, and the Next.js spans become its children.

`@sentry/nextjs` depends on `@sentry/cloudflare`, so you don't need to install another package.

First, create a [custom Worker](https://opennext.js.org/cloudflare/howtos/custom-worker) that imports the handler of OpenNext and wraps it with `withSentry`. Pass your Sentry options to `withSentry`:

```typescript
import * as Sentry from "@sentry/nextjs/cloudflare";
// @ts-ignore `.open-next/worker.js` is generated at build time
import handler from "./.open-next/worker.js";

// Re-export the Durable Objects your app uses, for example for the DO Queue and DO Tag Cache of OpenNext
// @ts-ignore `.open-next/worker.js` is generated at build time
export { DOQueueHandler, DOShardedTagCache } from "./.open-next/worker.js";

export default Sentry.withSentry(
  () => ({
    dsn: "https://<key>@o<orgId>.ingest.sentry.io/<projectId>",

    // Set tracesSampleRate to 1.0 to capture 100% of spans for tracing.
    // Learn more at
    // https://docs.sentry.io/platforms/javascript/guides/cloudflare/configuration/options/#tracesSampleRate
    tracesSampleRate: 1.0,
  }),
  handler,
);
```

Then, point `main` in your Wrangler configuration to this file:

```json
{
  "main": "./worker.ts"
}
```

*Other available variations of the above snippet: wrangler.toml*

### [Server and Edge Configuration in the Worker](https://docs.sentry.io/platforms/javascript/guides/cloudflare/frameworks/nextjs.md#server-and-edge-configuration-in-the-worker)

In the Worker, `withSentry` creates the Sentry client. `Sentry.init()` in `sentry.server.config.ts` and `sentry.edge.config.ts` still runs, but it creates no client and only applies the `release` option. Set all other options in `withSentry`.

On Workers, these two files are optional. Keep them if you use `next dev`, which runs on Node.js and doesn't use the Worker entry. They also pass on the release that `withSentryConfig` sets at build time.

Keep `instrumentation.ts`, because Next.js reports server errors, for example from Server Components and route handlers, through its `onRequestError` hook.

The SDK takes the release from the first of these sources that has a value:

1. The `release` option of `withSentry`
2. The `SENTRY_RELEASE` environment variable
3. The ID of the `CF_VERSION_METADATA` binding
4. The `release` option of `Sentry.init()` in `sentry.server.config.ts` or `sentry.edge.config.ts`, or else the release that `withSentryConfig` sets at build time

On Node.js, the build release comes before `SENTRY_RELEASE`.

## [Deploy](https://docs.sentry.io/platforms/javascript/guides/cloudflare/frameworks/nextjs.md#deploy)

Build and deploy your Next.js application to Cloudflare Workers. You should start seeing error events, traces, and logs appear in Sentry.
