Skip to content

Commit fb327d0

Browse files
committed
Use __non_webpack_require__
Use `__non_webpack_require__` to get the actually exported storage module and not a secondarily generated one (we need the same instance)
1 parent e27c44c commit fb327d0

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

packages/nextjs/src/config/templates/routeHandlerWrapperTemplate.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
// @ts-expect-error Because we cannot be sure if the RequestAsyncStorage module exists (it is not part of the Next.js public
2-
// API) we use a shim if it doesn't exist. The logic for this is in the wrapping loader.
3-
// eslint-disable-next-line import/no-unresolved
4-
import { requestAsyncStorage } from '__SENTRY_NEXTJS_REQUEST_ASYNC_STORAGE_SHIM__';
51
// @ts-expect-error See above
62
// eslint-disable-next-line import/no-unresolved
73
import * as routeModule from '__SENTRY_WRAPPING_TARGET_FILE__';
@@ -10,8 +6,6 @@ import * as Sentry from '@sentry/nextjs';
106

117
import type { RequestAsyncStorage } from './requestAsyncStorageShim';
128

13-
declare const requestAsyncStorage: RequestAsyncStorage;
14-
159
declare const routeModule: {
1610
GET?: (...args: unknown[]) => unknown;
1711
POST?: (...args: unknown[]) => unknown;
@@ -40,6 +34,11 @@ function wrapHandler<T>(handler: T, method: 'GET' | 'POST' | 'PUT' | 'PATCH' | '
4034

4135
// We try-catch here just in case the API around `requestAsyncStorage` changes unexpectedly since it is not public API
4236
try {
37+
// @ts-expect-error AAAA
38+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
39+
const requestAsyncStorage: RequestAsyncStorage = __non_webpack_require__(
40+
'__SENTRY_NEXTJS_REQUEST_ASYNC_STORAGE_SHIM__',
41+
).requestAsyncStorage;
4342
const requestAsyncStore = requestAsyncStorage.getStore();
4443
sentryTraceHeader = requestAsyncStore?.headers.get('sentry-trace') ?? undefined;
4544
baggageHeader = requestAsyncStore?.headers.get('baggage') ?? undefined;

packages/nextjs/src/config/templates/serverComponentWrapperTemplate.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
// @ts-expect-error Because we cannot be sure if the RequestAsyncStorage module exists (it is not part of the Next.js public
2-
// API) we use a shim if it doesn't exist. The logic for this is in the wrapping loader.
3-
// eslint-disable-next-line import/no-unresolved
4-
import { requestAsyncStorage } from '__SENTRY_NEXTJS_REQUEST_ASYNC_STORAGE_SHIM__';
51
// @ts-expect-error We use `__SENTRY_WRAPPING_TARGET_FILE__` as a placeholder for the path to the file being wrapped.
62
// eslint-disable-next-line import/no-unresolved
73
import * as serverComponentModule from '__SENTRY_WRAPPING_TARGET_FILE__';
@@ -10,8 +6,6 @@ import * as Sentry from '@sentry/nextjs';
106

117
import type { RequestAsyncStorage } from './requestAsyncStorageShim';
128

13-
declare const requestAsyncStorage: RequestAsyncStorage;
14-
159
declare const serverComponentModule: {
1610
default: unknown;
1711
};
@@ -30,6 +24,11 @@ if (typeof serverComponent === 'function') {
3024

3125
// We try-catch here just in `requestAsyncStorage` is undefined since it may not be defined
3226
try {
27+
// @ts-expect-error AAAA
28+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
29+
const requestAsyncStorage: RequestAsyncStorage = __non_webpack_require__(
30+
'__SENTRY_NEXTJS_REQUEST_ASYNC_STORAGE_SHIM__',
31+
).requestAsyncStorage;
3332
const requestAsyncStore = requestAsyncStorage.getStore();
3433
sentryTraceHeader = requestAsyncStore?.headers.get('sentry-trace');
3534
baggageHeader = requestAsyncStore?.headers.get('baggage');

0 commit comments

Comments
 (0)