-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@sentry/next.js 7.31 autoinstrumentation breaks middleware with named export #6815
Comments
To be clear, there's a simple workaround: change the middleware function export to a default export. I filed the issue since I'm sure others will run into the same problem. |
Hi, thanks for writing in! A few questions:
|
I'm on unix (macOS).
import { NextRequest, NextResponse } from 'next/server';
import { Params } from 'server/services/auth/constants';
import { OIDC_COOKIES } from 'constants/cookies';
const ALLOW_LIST = [
// Public pages
new URLPattern({ pathname: '/:locale?/cookie-preferences' }),
// ...
];
export function middleware(req: NextRequest): Response {
const isAllowListed = ALLOW_LIST.some((pattern) => pattern.test(req.nextUrl));
if (isAllowListed) {
return NextResponse.next();
}
const isUnauthenticated = !req.cookies.get(OIDC_COOKIES.Token);
if (isUnauthenticated) {
// Middlewares run in an environment with only a subset of web APIs, therefore it is not possible to import the open-id client here.
// Instead, redirect the user to the login API route where the necessary APIs are available.
const rewriteUrl = req.nextUrl.clone();
const finalRedirectUrl = req.nextUrl.clone();
rewriteUrl.pathname = '/api/sso/login';
rewriteUrl.searchParams.set(Params.RedirectUrl, finalRedirectUrl.pathname);
return NextResponse.rewrite(rewriteUrl);
}
return NextResponse.next();
}
Yes, it's |
Ok, there was an obvious oversight which we hopefully fixed. A patch will go out in an hour or so. Thanks for reporting this! |
Hi, the new release is out: https://github.com/getsentry/sentry-javascript/releases/tag/7.31.1 Can you try it out and see if it resolves the issue for you? Thanks! |
Can confirm that this is fixed in v7.31.1. Thank you! |
@connor-baer Thanks for checking! |
I'm having similar issue with
Setting |
@Sija can you share the full error message? Thanks! |
Same problem here. Seems like defining a function with
|
@nf-projects Could it be that you're manually calling an api handler in |
Hi!
|
@a28dong can you share more about your setup?
Actually, it would be best if you opened a new issue. This issue is resolved. |
Is there an existing issue for this?
How do you use Sentry?
Sentry Saas (sentry.io)
Which package are you using?
@sentry/nextjs
SDK Version
7.31.0
Framework Version
next@12.2.5
Link to Sentry event
No response
SDK Setup
Steps to Reproduce
Using the new auto instrumentation of server functions alongside the new edge SDK in @sentry/nextjs 7.31 causes the middleware to crash (see the error below) when using a named export.
Sentry is injecting code into the middleware bundle that should probably only be included in page bundles. This code fails if the middleware is exported as a named function (which is supported and suggested by the Next.js docs). In this case, there is no default export, meaning the
pageComponent
isundefined
.Problematic code in
.next/server/src/middleware.js
:Expected Result
Sentry does not inject page-related code into the middleware bundle or gracefully handles the named export of the middleware function.
Actual Result
The text was updated successfully, but these errors were encountered: