---
title: "Digging better tunnels: keeping Sentry events from getting lost to ad blockers"
description: "Ad blockers drop Sentry requests. Here's how we rebuilt the JavaScript SDK tunnel with a DSN-derived path and runtime-level interception."
url: "https://blog.sentry.io/sentry-tunnel-ad-blockers/"
canonical_url: "https://blog.sentry.io/sentry-tunnel-ad-blockers/"
authors: ["Abdelrahman Awad"]
published: "2026-12-01"
category: "Engineering"
---

# Digging better tunnels: keeping Sentry events from getting lost to ad blockers

Ad blockers drop requests to Sentry, so errors and traces from some of your users never arrive. A tunnel fixes that by sending events through your own server first, but our tunnels were fragile and only worked out of the box in a couple of frameworks. As of version 11.13 of the JavaScript SDKs, it takes `tunnel: true` in the browser and one integration on the server, in any JavaScript runtime we support. This post is about how we got there, and the bugs we met along the way.

One of the first things I worked on in the SDK, before I’d even officially joined Sentry, was [a report](https://github.com/getsentry/sentry-javascript/pull/18257) that the random Next.js tunnel route was “not working”. It turned out to be too random. The server expected one path, and the client was posting to another.

## Why dig at all

I personally use ad blockers, because the web is unbearable without them. Many browsers now ship with one built in, and users never turn it on… or off.

The problem with ad blockers is they block anything resembling an “ad”. That’s why we had a rule at my previous job: never put the word “ad” or “campaign” in a component, as their assets usually got blocked. This was particularly inconvenient because we were building an ad campaign manager.

Ad blockers also drop requests sent to Sentry, so errors and traces from those end users never arrive. Popular block lists include Sentry’s ingest domain, our old Next.js tunnel path, and some of our users’ own custom tunnel paths. The thing is, we can’t even tell how many events we lose to ad blockers, because the client reports that would tell us get blocked too.

We need a way to route Sentry’s requests to our ingest endpoint. A tunnel sends the browser SDK’s envelopes (the format our SDKs use to send events) to the app’s own origin instead, and the app’s server forwards them to Sentry. Ad blockers let requests to a site’s own origin through, because blocking them would break the site.

This isn’t about tracking people. Sentry events are error and performance data about the app itself, and they go only to the team that runs it, not across sites or to advertisers. The tunnel forwards just the event and leaves the visitor’s cookies behind.

## How a tunnel works

There are two parts. The browser SDK needs to route its traffic to the tunnel URL, and there has to be a server handler that forwards those tunnel requests to Sentry.

The first part is easy. The browser SDK and the SDKs built on it have had a `tunnel` option in `init` for a long time:

```javascript
Sentry.init({
  tunnel: "/something",
  // ...
});
```

The first challenge is the tunnel URL. We tried `/monitoring` before and it was added to the block list, so the tunnel path can’t be a static value. It has to be random. Ad blockers mostly match URLs, not headers or payloads, so a random path that could just as well belong to any other app can’t be blanket blocked as easily.

All of our SDKs could send envelopes through a tunnel, but only two frameworks could handle those requests on a random path:

-   **Next.js:** the `tunnelRoute` config adds a rewrite to `sentry.io`.
-   **TanStack Start:** `tunnelRoute: true` picks a random path per build and injects a handler for it.

```javascript
// The path is generated in the build
sentryTanstackStart({ tunnelRoute: true }); // -> '/a8f3k2xq'
```

We exposed some utilities so users could build their own tunnels, but those couldn’t handle random paths either, and even then we had a few problems.

## Why a random path per build breaks

### Two builds, one process

Apps usually have two build pipelines, one for the browser and one for the server. Our implementation “hoped” both ran in the same process so they could share environment variables. When the server and client build in separate steps, each step rolls its own path, then 404s ensue. That’s what was happening with the Next.js bug I mentioned.

![With one build process, server and client share a random path and get a 200. Separate build steps roll two paths and get a 404.](https://blog.sentry.io/_astro/split-builds.DK2q6Hoy.svg)

### Asset drift

With continuous deployment, the browser’s bundle and the server drift apart, since each deploy gets a new path. A browser still running last deploy’s bundle posts to a path the new server no longer serves, and those events are lost until the page reloads.

![A tab running the deploy 1 bundle keeps posting to the old path after deploy 2 ships, losing events until the tab reloads.](https://blog.sentry.io/_astro/asset-drift.HHfoiQrK.svg)

We needed a stable path, but not one that fits a fire-and-forget regex pattern.

## Tunnels as a core SDK feature

Our approach was flawed because it was framework-first. Instead, we decided to treat tunnels as a core capability our SDKs offer for any setup. That meant giving users better tools, and setting the tunnel up for them outright where possible.

![A spoon, a shovel, and a tunnel boring machine, one for each stage of the Sentry JavaScript SDK tunnel tooling.](https://blog.sentry.io/_astro/dig-tools.D0jtRwy3.svg)

Roughly, the tools went like this:

-   **The spoon** (before 10.40.0): Next.js rewrites only.
-   **The shovel** (10.40.0 to 11.10.0): a [framework-agnostic tunnel handler](https://github.com/getsentry/sentry-javascript/pull/18892) you could wire up yourself, plus the [TanStack Start tunnel route](https://github.com/getsentry/sentry-javascript/pull/20264).
-   **The tunnel boring machine** (11.13): automatic tunnels everywhere.

### At a glance

Both sides derive the tunnel from the DSN (the public URL that identifies your Sentry project), so the browser only opts in with `tunnel: true`:

```javascript
// Browser
Sentry.init({
  dsn: "https://public@o1.ingest.sentry.io/1337",
  tunnel: true,
});
```

The server adds `serveTunnelIntegration`, which answers the tunnel requests for it:

```javascript
// Server
Sentry.init({
  dsn: "https://public@o1.ingest.sentry.io/1337",
  integrations: [Sentry.serveTunnelIntegration()],
});
```

![Browser SDK posts to a DSN-derived path; a runtime patch on the server hands it to the tunnel handler, which forwards it to Sentry.](https://blog.sentry.io/_astro/tunnel-request.C0SFxcLP.svg)

## How random should a random URL be?

Random enough that a filter list can’t target it, but not so random that we end up relying on a single build process. That means a hash of something the user already provides, so they don’t need new options.

A hash of the DSN seemed like a good candidate:

-   If there’s no DSN, the SDKs are effectively disabled anyway.
-   In meta-frameworks, the DSN is the same for the browser and the server.
-   If the DSNs differ, we can let the user configure which DSNs to let through.

![Fixed paths get ad-blocked, random per-build paths drift between deploys, and a hash of the DSN passes filters and stays stable.](https://blog.sentry.io/_astro/random-paths.NeJgXRet.svg)

| Approach | Deploy 1 | Deploy 2 | Filter list | Result |
| --- | --- | --- | --- | --- |
| Fixed path (no build step) | `/monitoring` | `/monitoring` | Blocked | Ad-blocked |
| Random per build (needs a build step) | `/a8f3k2xq` | `/q7m2c9wd` | Passes | Drifts |
| Hash of the DSN (no build step) | `/16bejtj` | `/16bejtj` | Passes | Works |

```javascript
const dsn = "https://public@o1.ingest.sentry.io/1337";

getTunnelPath(dsn); // -> '/16bejtj'
```

We picked the 32-bit [FNV-1a hash](https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function) because the path is needed synchronously inside `init`, and it runs in every runtime with no dependencies. It doesn’t need to be cryptographic, since the DSN is already public.

### The catch

Ad blockers can still work out which popular sites use which hashes and block those specifically. The only way out of that today is to rotate the DSN every now and then.

We have an idea for that. On every page load, the browser picks a random nonce and tags it with a short hash of the DSN and that nonce, and the server recomputes the tag to check it:

```plaintext
// first page load
POST https://app.example.com/x7k2m91hqn

// second page load
POST https://app.example.com/q4n8b212ly
```

We shipped the simple version, and haven’t needed the nonce so far.

## Closing the can of worms

Great, the build problems are gone. But we have a saying in Egypt that goes “disasters don’t come in one”. Owning the tunnel means the SDK now owns a backend, and it brought friends.

### No free rides

A tunnel that forwards anything is an open proxy, so the handler only forwards envelopes for DSNs in `allowedDsns`. The server’s own DSN is always allowed, and apps with more than one client list the others:

```javascript
Sentry.init({
  dsn: "https://public@o1.ingest.sentry.io/1337",
  integrations: [
    Sentry.serveTunnelIntegration({
      // The server's own DSN is always allowed
      allowedDsns: ["https://other@o1.ingest.sentry.io/4242"],
    }),
  ],
});
```

Rotating a DSN changes the path too, so during a rotation you can list both DSNs in `allowedDsns` until the old builds are gone.

### Papers, please

With a tunnel, every event comes from the app’s server. That means for every visitor, [Relay](https://github.com/getsentry/relay) (Sentry’s ingestion service) sees the same IP address and the same user agent.

The tunnel has to forward what describes the visitor: their IP, their browser, and where the request came from. Cookies and the rest stay behind. Rate limits make it back to the browser too, so the SDK still backs off when Sentry tells it to.

### Don’t crash

The handler takes whatever the runtime hands us, either a fetch `Request` or a Node-style `req, res` pair. This sounded easy, [until it wasn’t](https://github.com/getsentry/sentry-javascript/pull/25783):

-   On HTTP/2, Node adds pseudo-headers like `:authority`. Fetch doesn’t like them, so copying them brings the whole server down.
-   Some middleware calls `req.setEncoding()`, and now we can’t read the body.
-   Someone sends a `Host` of `a b`. Node is fine with it, `new URL` isn’t. Also crashes the server.
-   The keep-alive connections just sit there.
-   Sentry answers with `connection` and friends. HTTP/2 forbids those. You guessed it: crashes the server.
-   Bun has no `ReadableStream.from`.

### Don’t slow the traffic

Adding request handlers to our users’ apps means overhead, so we need to do as little as possible, and only do more when we should.

First, we check if the request is a `POST` to the tunnel path. Otherwise we don’t bother reading the body.

Second, we don’t need the whole payload, just the first line of the envelope, which carries the DSN. If the DSN isn’t allowed, we answer with a 403 and the rest of the body is never downloaded.

![The tunnel reads only the envelope header line to check the DSN before deciding whether to forward the rest of the body.](https://blog.sentry.io/_astro/header-peek.B6mrba8M.svg)

### Answering early

Running our interception early is critical. If the app has an auth middleware, it may reject envelopes sent before the user logs in, and that data is lost. Same thing for CSRF tokens: without them, most `POST` requests would fail.

So it’s important to get ahead of the app’s own guards. Where a framework used an order-based middleware stack, we needed to make sure ours ran first.

![Auth middleware rejects the anonymous tunnel POST and the event is lost, unless the Sentry tunnel hook runs first.](https://blog.sentry.io/_astro/answer-order.C0HFBmSY.svg)

### Answering even earlier

That’s what we shipped in v11.10, but we didn’t really like it. Being first on the middleware stack can’t be guaranteed, and the many edge cases we found made the whole thing brittle.

We still had another trick we wanted to explore. What if we answer at the runtime level? The request never reaches the user’s code or their framework at all, so there is no middleware order to win and no encoding to figure out. It treats all apps exactly the same, because they run on the same runtimes.

Every JavaScript server runtime we support has a consistent entry point for requests:

-   **Node:** the `http` server’s request event, so anything built on `node:http` is covered.
-   **Bun and Deno:** the same, plus their native `serve` APIs.
-   **Cloudflare Workers:** we already have a `withSentry` wrapper around the worker’s `fetch` entry point.
-   **AWS Lambda:** a shim in front of the user’s handler.

Auth, CSRF checks, and framework routing all run after that, so none of them can get in the way. It also let us [delete the framework hooks](https://github.com/getsentry/sentry-javascript/pull/26001) we shipped in v11.10, so every framework now gets the same behavior from the same code. That’s 623 lines gone, and no special cases anymore.

## Next.js gets its own section

I lied, because Next.js has its own rules.

We’ve [built workarounds before](https://github.com/getsentry/sentry-javascript/pull/18700) to fit Next.js and Vercel’s conventions, and tunnels are no different. Next.js has no way to inject a route, which is where the runtime-level idea came from in the first place. It worked flawlessly locally, so I deployed an actual app, and it didn’t work.

On Vercel, routes that don’t exist in a Next.js app’s build output are answered with a 404 before the request ever reaches the app, which is where our runtime interception runs. That’s reasonable platform behavior; it just means we need a route Vercel knows about. Rewrites, which powered the old tunnel, are one way to get that. But a rewrite isn’t a route handler. It forwards the whole request to `sentry.io`, cookies included. So a teammate built [a two-hop tunnel](https://github.com/getsentry/sentry-javascript/pull/25456) that rewrites requests to a real route file in the user’s app, which forwards only the event payload.

```typescript
// app/tunnel/route.ts
export { POST } from "@sentry/nextjs/tunnel";
```

It’s not exactly the “just works™” we’d been chasing, since the user still needs to create a file. Then we thought, why not write the file for them? It’s a one-liner, and the SDK writes it out at build time only when the app is deployed on Vercel. Everywhere else, the runtime interception just works.

## Bring your own framework

Some people run “exotic” setups, like a handcrafted artisanal framework used by dozens. Up until v11.10, they had to add a middleware by hand. Now `serveTunnelIntegration` is all it takes.

You can also turn interception off and call the handler yourself. The integration is still useful in that case because it filters the tunnel traffic out of your traces, but you can do that yourself too and drop the integration entirely:

```javascript
Sentry.init({
  integrations: [
    Sentry.serveTunnelIntegration({ intercept: false }),
  ],
});

app.use((req, res, next) => {
  return Sentry.handleTunnelRequestIfMatched(req, res) ?? next();
});
```

Every server SDK exports the handler, so any browser-only SDK can be paired with any JavaScript server. Next.js users can do the same in `proxy.ts` or an API route handler.

## What we learned

-   **Derive shared values from inputs both sides already have.** A random path per build forced the browser and server to coordinate through the build process, and that coordination broke with split builds and rolling deploys. A hash of the DSN needs no coordination, because both sides already know the DSN.
-   **Intercept at the lowest layer everyone shares.** Framework hooks meant one integration per framework, each with its own middleware order and body-parsing quirks. Every app runs on a handful of runtimes, so intercepting there gave us one code path and let us delete the per-framework ones.
-   **A forwarding endpoint is an allowlist, not a pipe.** Forward only for known DSNs, forward only the headers that describe the visitor, and leave cookies behind.
-   **Read the least you can before deciding.** The DSN is on the envelope’s first line, so a request we won’t forward costs us one line of reading, not a full upload.

## Is your app losing events to ad blockers?

Probably, if your users are on the web. To check, turn on an ad blocker in your own browser, trigger an error, and watch the network tab for a blocked request to `ingest.sentry.io`.

To set up the managed tunnel:

1.  Upgrade your Sentry JavaScript SDKs to 11.13.0 or later.
2.  Set `tunnel: true` in your browser `Sentry.init`.
3.  Add `Sentry.serveTunnelIntegration()` to your server `Sentry.init`.
4.  If your browser uses a different DSN than your server, list it in `allowedDsns`.
5.  If you used `tunnelRoute` in Next.js or TanStack Start, remove it. If you wrote your own tunnel at a fixed path, consider switching, since fixed paths are exactly what ends up on block lists.

The [tunneling docs](https://docs.sentry.io/platforms/javascript/configuration/tunneling/) cover the full setup, including fixed paths and handling tunnel requests yourself, and the [`serveTunnelIntegration` reference](https://docs.sentry.io/platforms/javascript/guides/node/configuration/integrations/servetunnel/) lists every option. The managed tunnel first shipped in [v11.10](https://github.com/getsentry/sentry-javascript/releases/tag/11.10.0), runtime interception followed in [v11.12](https://github.com/getsentry/sentry-javascript/releases/tag/11.12.0), and the Next.js rework landed in [v11.13](https://github.com/getsentry/sentry-javascript/releases/tag/11.13.0). If something breaks in your setup, [open an issue on sentry-javascript](https://github.com/getsentry/sentry-javascript/issues).

None of this started with me; teammates laid the groundwork with the first framework-agnostic handler and the TanStack Start tunnel route. Thanks to everyone on the JavaScript SDK team who reviewed two stacks of 24 PRs.
