Skip to content

@sentry/next.js 7.31 autoinstrumentation breaks middleware with named export #6815

Description

@connor-baer

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

// next.config.jsconst{ withSentryConfig }=require('@sentry/nextjs');/** * @type {import('next').NextConfig} */constnextConfig={sentry: {hideSourceMaps: false,autoInstrumentServerFunctions: true,},};module.exports=withSentryConfig(nextConfig);
// sentry.edge.config.tsimport*asSentryfrom'@sentry/nextjs';Sentry.init({dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,environment: process.env.NEXT_PUBLIC_ENV,});
// sentry.server.config.tsimport*asSentryfrom'@sentry/nextjs';Sentry.init({dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,environment: process.env.NEXT_PUBLIC_ENV,integrations: [newSentry.Integrations.RequestData({include: {cookies: false,headers: false,ip: false,},}),],});

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 is undefined.

Problematic code in .next/server/src/middleware.js:

varorigModule=/*#__PURE__*/Object.freeze({__proto__: null,middleware: middleware});/* * This file is a template for the code which will be substituted when our webpack loader handles non-API files in the * `pages/` directory. * * We use `__SENTRY_WRAPPING_TARGET_FILE__.cjs` as a placeholder for the path to the file being wrapped. Because it's not a real package, * this causes both TS and ESLint to complain, hence the pragma comments below. */constuserPageModule=origModule;constpageComponent=userPageModule.default;// ⚠️ This is where the TypeError occurs: constorigGetInitialProps=pageComponent.getInitialProps;constorigGetStaticProps=userPageModule.getStaticProps;constorigGetServerSideProps=userPageModule.getServerSideProps;constgetInitialPropsWrappers={'/_app': _sentry_nextjs__WEBPACK_IMPORTED_MODULE_3__/* .wrapAppGetInitialPropsWithSentry */.$l,'/_document': _sentry_nextjs__WEBPACK_IMPORTED_MODULE_3__/* .wrapDocumentGetInitialPropsWithSentry */.B4,'/_error': _sentry_nextjs__WEBPACK_IMPORTED_MODULE_3__/* .wrapErrorGetInitialPropsWithSentry */.rO,};constgetInitialPropsWrapper=getInitialPropsWrappers['/../middleware']||_sentry_nextjs__WEBPACK_IMPORTED_MODULE_3__/* .wrapGetInitialPropsWithSentry */.O5;if(typeoforigGetInitialProps==='function'){pageComponent.getInitialProps=getInitialPropsWrapper(origGetInitialProps);}constgetStaticProps=typeoforigGetStaticProps==='function'
? _sentry_nextjs__WEBPACK_IMPORTED_MODULE_3__.wrapGetStaticPropsWithSentry(origGetStaticProps,'/../middleware')
: undefined;constgetServerSideProps=typeoforigGetServerSideProps==='function'
? _sentry_nextjs__WEBPACK_IMPORTED_MODULE_3__.wrapGetServerSidePropsWithSentry(origGetServerSideProps,'/../middleware')
: undefined;

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

Error [TypeError]: Cannot read properties of undefined (reading 'getInitialProps')
at <unknown> (evalmachine.<anonymous>:113)
at Module.4921 (evalmachine.<anonymous>:113:43)
at __webpack_require__ (evalmachine.<anonymous>:25:43)
at Module.3946 (evalmachine.<anonymous>:157:19)
at __webpack_require__ (evalmachine.<anonymous>:25:43)
at __webpack_exec__ (evalmachine.<anonymous>:306:48)
at <unknown> (evalmachine.<anonymous>:307:53)
at Function.__webpack_require__.O (evalmachine.<anonymous>:66:23)
at <unknown> (evalmachine.<anonymous>:308:56)
at webpackJsonpCallback (evalmachine.<anonymous>:176:39)
at <unknown> (evalmachine.<anonymous>:2:61)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions