Repository navigation
Description
Is there an existing issue for this?
- I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues
- I have reviewed the documentation https://docs.sentry.io/
- I am using the latest SDK release https://github.com/getsentry/sentry-javascript/releases
How do you use Sentry?
Sentry Saas (sentry.io)
Which SDK are you using?
@sentry/nextjs
SDK Version
10.41.0
Framework Version
NextJS v15
Link to Sentry event
No response
Reproduction Example/SDK Setup
// sentry-test.tsx
import React from 'react';
const SentryTest: React.FC = () => {
throw new Error('This is a test 2 error for Sentry integration.');
};
export default SentryTest;// _error.tsx
CustomError.getInitialProps = async (pageContext) => {
const { err, res } = pageContext;
const statusCode = res?.statusCode || 500;
// This currently returns undefined
const lastEventId = await Sentry.captureUnderscoreErrorException(pageContext);
return { statusCode, lastEventId };
};Steps to Reproduce
- Set up a Next.js app using the Pages Router without
getServerSideProps - Create a page that throws during render or in
getInitialProps - In
_error.tsx, callcaptureUnderscoreErrorException(pageContext)ingetInitialProps - Observe that the returned value is always
undefined - Confirm
Sentry.getIsolationScope().lastEventId()returnsundefined
Possible Root Cause
Sentry's webpack loader wraps _error.tsx via pageWrapperTemplate.ts, which applies wrapErrorGetInitialPropsWithSentry to the page's getInitialProps. Inside that wrapper, the user's getInitialProps is passed through withErrorInstrumentation before being called.
When an error is thrown during component render or in a page's getInitialProps, withErrorInstrumentation catches it and calls captureException on it — setting __sentry_captured__ = true on the error object. By the time the user's getInitialProps runs and calls captureUnderscoreErrorException, the error is already marked as captured and its isolation scope is already torn down.
captureUnderscoreErrorException then hits this branch:
if (err && core.isAlreadyCaptured(err)) {
responseEnd.waitUntil(responseEnd.flushSafelyWithTimeout());
return core.getIsolationScope().lastEventId(); // always undefined — scope is gone
}This design assumes errors originate from getServerSideProps, where Sentry wraps each function individually and the isolation scope is still active when _error.getInitialProps runs. When errors come from component render or page-level getInitialProps instead, withErrorInstrumentation captures the error at the _error wrapper level first, and the scope is already gone by the time captureUnderscoreErrorException runs.
Current Workaround
Store the event ID in beforeSend and read it from the error object in _error.tsx:
// sentry.server.config.ts
beforeSend(event, hint) {
const err = hint?.originalException;
if (err && typeof err === 'object' && event.event_id) {
(err as any).__sentry_event_id__ = event.event_id;
}
return event;
}
// _error.tsx
CustomError.getInitialProps = async (pageContext) => {
const { err, res } = pageContext;
const statusCode = res?.statusCode || 500;
const lastEventId =
(err as any)?.__sentry_event_id__ ??
await Sentry.captureUnderscoreErrorException(pageContext);
return { statusCode, lastEventId };
};Expected Result
Get the lastEventId in getInitialProps() in _error.tsx when calling Sentry.captureUnderscoreErrorException(pageContext) or Sentry.lastEventId() after the error is captured.
Actual Result
We get undefined.
While debugging in the server, we can see that the _lastEventId value is undefined by the time it reaches getInitialProps.
Additional Context
Even if we don't get the lastEventId, it's correctly being sent to Sentry, so that part is not an issue.
Priority
React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it.
Metadata
Metadata
Assignees
Projects
- StatusShow more project fieldsNo status
Activity
linear-code commentedon Mar 7, 2026
JS-1886
ajimenezlyft commentedon Mar 7, 2026
This is related to this issue reported by another colleague.
[-]Sentry.lastEventId() doesn't work on SSR when the error is thrown in the component render[/-][+]`Sentry.lastEventId()` doesn't work on SSR when the error is thrown in the component render[/+]s1gr1d commentedon Mar 10, 2026
Hi, thanks for the issue. We'll take a look!
In general,
lastEventIdmight not give back the correct event ID as it could have been dropped already. See the docs on it: https://docs.sentry.io/platforms/javascript/configuration/apis/#lastEventIdfix(nextjs): Log correct `lastEventId` when error is thrown in compon…
fix(nextjs): Log correct `lastEventId` when error is thrown in compon…
github-actions commentedon Mar 17, 2026
A PR closing this issue has just been released 🚀
This issue was referenced by PR #19764, which was included in the 10.44.0 release.