Skip to content

Cannot set properties of undefined (setting '_sentryTraceData') #14571

Closed
@Grohden

Description

@Grohden

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/nextjs

SDK Version

8.35.0

Framework Version

Next 14.2.11

Link to Sentry event

https://trusted-health.sentry.io/issues/5833907467/events/651aec0e738f4a8592762072abecec34/

Reproduction Example/SDK Setup

We've been having this issue for a while AFAIK, so here's what we've investigated

From the linked stack trace, these are the lines:

const {
data: initialProps,
baggage,
sentryTrace,
}: {
data: {
_sentryTraceData?: string;
_sentryBaggage?: string;
};
baggage?: string;
sentryTrace?: string;
} = (await tracedGetInitialProps.apply(thisArg, args)) ?? {}; // Next.js allows undefined to be returned from a getInitialPropsFunction.

If (await tracedGetInitialProps.apply(thisArg, args)) results in undefined we get a {} default

But then all these:

const {
          data: initialProps, // undefined
          baggage, // undefined
          sentryTrace, // undefined
} = {}

become undefined, so we're executing this:

        // The Next.js serializer throws on undefined values so we need to guard for it (#12102)
        if (sentryTrace) {
          undefined._sentryTraceData = sentryTrace;
        }

Which will result in those undefined access problems...

You guys also have some other places where there's similar code that might be worth checking
https://github.com/search?q=repo%3Agetsentry%2Fsentry-javascript+tracedGetInitialProps.apply%28thisArg%2C+args%29%29&type=code

It also seems sentry had a similar issue in the past

Steps to Reproduce

Don't know how to simulate

Expected Result

No throw

Actual Result

Throw

Metadata

Metadata

Assignees

Labels

Package: nextjsIssues related to the Sentry Nextjs SDK

Type

Projects

Status

Waiting for: Product Owner

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions