sentry-javascript icon indicating copy to clipboard operation
sentry-javascript copied to clipboard

Next.js middleware support

Open iker-barriocanal opened this issue 2 years ago • 13 comments

The middleware is a new feature in Next.js v12, currently not supported by the Next.js SDK. The release blog post says it's in beta, but the middleware docs aren't that specific and don't say whether it's a prerelease.

what to cover: -[ ] Error tracking -[ ] Performance monitoring

iker-barriocanal avatar Dec 01 '21 11:12 iker-barriocanal

Anyone have a draft of how to make this happen while the feature gets developed?

mikestopcontinues avatar Jan 07 '22 18:01 mikestopcontinues

Sharing input from another channel here

If you can support Workers, should be a similar runtime as Middleware

image

https://nextjs.org/docs/api-reference/edge-runtime

smeubank avatar May 19 '22 07:05 smeubank

Given that middleware in Next.js is still in beta (now more explicitly displayed) and under rapid development, I will close this issue until their API is stable.

vladanpaunovic avatar Jun 08 '22 09:06 vladanpaunovic

@vladanpaunovic Is there a way to disable just the server part of @sentry/nextjs? Because while I understand that you don't want to support beta/middleware this also blocking any adoption of https://nextjs.org/docs/advanced-features/react-18/switchable-runtime now.

boennemann avatar Jun 12 '22 18:06 boennemann

@boennemann, you can't disable the server part in @sentry/nextjs at the moment, but what you can do is use @sentry/react in the meantime.

vladanpaunovic avatar Jun 13 '22 05:06 vladanpaunovic

Feedback from Vercel:

The Middleware API is now stable - we're planning for a stable release with 12.2 here very soon. (Stable on latest canary) here are the changes from when it was first introduced. https://github.com/vercel/next.js/blob/canary/errors/middleware-upgrade-guide.md

reopoening issue. @lforst & @vladanpaunovic let's continue our conversation around E2E tests, canary releases, and how/if it could help us with such features

smeubank avatar Jun 20 '22 07:06 smeubank

https://nextjs.org/blog/next-12-2#middleware-stable

With NextJs 12.2 there is stable middleware support

smeubank avatar Jun 28 '22 16:06 smeubank

Yeap, ideally not only middleware support but also the core of it, which is Edge Runtime ecosystem:

heldr avatar Jul 06 '22 00:07 heldr

I believe this and this issue is one in the same as Next.js uses cloudflare workers for it's edge infra. I know this is an older issue just wanted to link them.

I would also love this support. I was trying to use the new edge runtime but got the error below when trying to just use captureException.

TypeError: Cannot read properties of undefined (reading 'getElementById')
    at node_modules/@sentry/nextjs/esm/performance/client.js:22:26
    at node_modules/@sentry/nextjs/esm/performance/client.js:94:54
    at node_modules/@sentry/tracing/esm/browser/browsertracing.js:99:0
    at node_modules/@sentry/core/esm/integration.js:64:0
    at <anonymous>
    at node_modules/@sentry/core/esm/integration.js:60:0
    at node_modules/@sentry/core/esm/baseclient.js:217:27
    at node_modules/@sentry/hub/esm/hub.js:65:0
    at node_modules/@sentry/core/esm/sdk.js:32:0
    at node_modules/@sentry/browser/esm/sdk.js:110:2
    at node_modules/@sentry/react/esm/sdk.js:18:2
    at node_modules/@sentry/nextjs/esm/index.client.js:31:2
    at sentry.server.config.js:11:0

athammer avatar Aug 31 '22 00:08 athammer

Seeing the same error as @athammer.

Is there still no way to disable the sentry server-side error capturing in @sentry/nextjs?

ndom91 avatar Sep 10 '22 20:09 ndom91

@ndom91 For now you could try not calling Sentry.init() in sentry.server.config.js and removing all the imports there, or you could uninstall the Next.js package and use @sentry/browser instead. Both are a bit ugly but I would have to experiment a bit to find a potentially cleaner solution.

Also wanna give a small update on this issue: We're currently discussing internally what it would take to make Next.js middleware support and/or an SDK for additional runtimes happen. We're definitely excited that the demand for the SDK is high and are looking forward to making this a reality.

lforst avatar Sep 13 '22 16:09 lforst

I ended up rewriting my one runtime: experimental-edge route to just be a normal node style API route for now to keep server-side error capturing everywhere else as well. Thanks for the quick reply though and best of luck!

ndom91 avatar Sep 15 '22 08:09 ndom91

I ended up rewriting my one runtime: experimental-edge route to just be a normal node style API route for now to keep server-side error capturing everywhere else as well. Thanks for the quick reply though and best of luck!

Ended up doing the same - very excited about this support though, middleware is so much faster.

jokull avatar Sep 15 '22 10:09 jokull

Okay this is now going to stop us from using Sentry in our production app. What's the ETA to get this working?

bramski avatar Oct 26 '22 18:10 bramski

hi all

we did a write up on possible work we are considering: 2023 and the JavaScript SDK

One of those items is in regards to support for WinterCG, which is ultimately the blocker for full support of Vercel Middlewares. See Edge Middleware Limitations

As a solution to help people who are blocked until then we will adding an option that allows users to opt out.

PR here: https://github.com/getsentry/sentry-javascript/pull/6125 Original issue here: https://github.com/getsentry/sentry-javascript/issues/6119

smeubank avatar Nov 03 '22 13:11 smeubank

What's up everybody, we finally released version 7.31.0 of the SDK which adds support for Next.js middleware.

To get errors and transactions for your middleware, simply update the SDK to the newest version and add a sentry.edge.config.js or sentry.edge.config.ts file in the root of your project:

// sentry.edge.config.js or sentry.edge.config.ts

import * as Sentry from "@sentry/nextjs";

const SENTRY_DSN = process.env.SENTRY_DSN || process.env.NEXT_PUBLIC_SENTRY_DSN;

Sentry.init({
  dsn: SENTRY_DSN || "YOUR DSN HERE",
  tracesSampleRate: 1.0,
});

For more info on this see the docs: https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/

Let us know if you find any issues!

lforst avatar Jan 16 '23 16:01 lforst

What's up everybody, we finally released version 7.31.0 of the SDK which adds support for Next.js middleware.

To get errors and transactions for your middleware, simply update the SDK to the newest version and add a sentry.edge.config.js or sentry.edge.config.ts file in the root of your project:

// sentry.edge.config.js or sentry.edge.config.ts

import * as Sentry from "@sentry/nextjs";

const SENTRY_DSN = process.env.SENTRY_DSN || process.env.NEXT_PUBLIC_SENTRY_DSN;

Sentry.init({
  dsn: SENTRY_DSN || "YOUR DSN HERE",
  tracesSampleRate: 1.0,
});

For more info on this see the docs: https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/

Let us know if you find any issues!

After adding & configured the sentry.edge.config.js, I ran some test with the v7.36 on client / serverless & edge runtime (same environment) by:

  • throwing an error
  • manually call Sentry.captureMessage & Sentry.captureException

It's working properly on the client & serverless functions, but any Sentry request triggered from the edge runtime is ignored and won't show up in Sentry.

leo-cheron avatar Feb 05 '23 15:02 leo-cheron

@mrgnou Hi, can you share a small reproduction example? Thanks!

lforst avatar Feb 06 '23 08:02 lforst

What's up everybody, we finally released version 7.31.0 of the SDK which adds support for Next.js middleware.

To get errors and transactions for your middleware, simply update the SDK to the newest version and add a sentry.edge.config.js or sentry.edge.config.ts file in the root of your project:

// sentry.edge.config.js or sentry.edge.config.ts

import * as Sentry from "@sentry/nextjs";

const SENTRY_DSN = process.env.SENTRY_DSN || process.env.NEXT_PUBLIC_SENTRY_DSN;

Sentry.init({
  dsn: SENTRY_DSN || "YOUR DSN HERE",
  tracesSampleRate: 1.0,
});

For more info on this see the docs: https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/

Let us know if you find any issues!

@lforst Thanks for the feature! However I run my nextjs app in a personal instance not in Vercel. So I don't actually need this config file I think, but It keeps telling below message. How can I turn off this?

You are using Next.js features that run on the Edge Runtime. Please add a "sentry.edge.config.js" or a "sentry.edge.config.ts" file to your project root in which you initialize the Sentry SDK with "Sentry.init()"

codemilli avatar Apr 02 '24 11:04 codemilli

@lforst Thanks for the feature! However I run my nextjs app in a personal instance not in Vercel. So I don't actually need this config file I think, but It keeps telling below message. How can I turn off this?

@codemilli Even if you are not deploying to Vercel, Next.js will use the edge runtime for middleware and edge routes. That means, if you want Sentry to collect errors for middleware, you need sentry.edge.config.ts. If you don't want it to do anything, you can just make that file empty.

lforst avatar Apr 02 '24 11:04 lforst

@lforst Thanks for the comment! sorry for misunderstanding by just reading the name 🙏

codemilli avatar Apr 02 '24 13:04 codemilli