Skip to content

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

Closed
@connor-baer

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

[email protected]

Link to Sentry event

No response

SDK Setup

// next.config.js

const { withSentryConfig } = require('@sentry/nextjs');

/**
 * @type {import('next').NextConfig}
 */
const nextConfig = {
  sentry: {
    hideSourceMaps: false,
    autoInstrumentServerFunctions: true,
  },
};

module.exports = withSentryConfig(nextConfig);
// sentry.edge.config.ts

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

Sentry.init({
  dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
  environment: process.env.NEXT_PUBLIC_ENV,
});
// sentry.server.config.ts

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

Sentry.init({
  dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
  environment: process.env.NEXT_PUBLIC_ENV,
  integrations: [
    new Sentry.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:

var origModule = /*#__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.
 */

const userPageModule = origModule ;

const pageComponent = userPageModule.default;

// ⚠️ This is where the TypeError occurs: 
const origGetInitialProps = pageComponent.getInitialProps;
const origGetStaticProps = userPageModule.getStaticProps;
const origGetServerSideProps = userPageModule.getServerSideProps;

const getInitialPropsWrappers = {
  '/_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,
};

const getInitialPropsWrapper = getInitialPropsWrappers['/../middleware'] || _sentry_nextjs__WEBPACK_IMPORTED_MODULE_3__/* .wrapGetInitialPropsWithSentry */ .O5;

if (typeof origGetInitialProps === 'function') {
  pageComponent.getInitialProps = getInitialPropsWrapper(origGetInitialProps) ;
}

const getStaticProps =
  typeof origGetStaticProps === 'function'
    ? _sentry_nextjs__WEBPACK_IMPORTED_MODULE_3__.wrapGetStaticPropsWithSentry(origGetStaticProps, '/../middleware')
    : undefined;
const getServerSideProps =
  typeof origGetServerSideProps === '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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Package: nextjsIssues related to the Sentry Nextjs SDK

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions