Skip to content

Commit

Permalink
Use __non_webpack_require__
Browse files Browse the repository at this point in the history
Use `__non_webpack_require__` to get the actually exported storage module and not a secondarily generated one (we need the same instance)
  • Loading branch information
lforst committed Sep 12, 2023
1 parent e27c44c commit fb327d0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
// @ts-expect-error Because we cannot be sure if the RequestAsyncStorage module exists (it is not part of the Next.js public
// API) we use a shim if it doesn't exist. The logic for this is in the wrapping loader.
// eslint-disable-next-line import/no-unresolved
import { requestAsyncStorage } from '__SENTRY_NEXTJS_REQUEST_ASYNC_STORAGE_SHIM__';
// @ts-expect-error See above
// eslint-disable-next-line import/no-unresolved
import * as routeModule from '__SENTRY_WRAPPING_TARGET_FILE__';
Expand All @@ -10,8 +6,6 @@ import * as Sentry from '@sentry/nextjs';

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

declare const requestAsyncStorage: RequestAsyncStorage;

declare const routeModule: {
GET?: (...args: unknown[]) => unknown;
POST?: (...args: unknown[]) => unknown;
Expand Down Expand Up @@ -40,6 +34,11 @@ function wrapHandler<T>(handler: T, method: 'GET' | 'POST' | 'PUT' | 'PATCH' | '

// We try-catch here just in case the API around `requestAsyncStorage` changes unexpectedly since it is not public API
try {
// @ts-expect-error AAAA
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const requestAsyncStorage: RequestAsyncStorage = __non_webpack_require__(
'__SENTRY_NEXTJS_REQUEST_ASYNC_STORAGE_SHIM__',
).requestAsyncStorage;
const requestAsyncStore = requestAsyncStorage.getStore();
sentryTraceHeader = requestAsyncStore?.headers.get('sentry-trace') ?? undefined;
baggageHeader = requestAsyncStore?.headers.get('baggage') ?? undefined;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
// @ts-expect-error Because we cannot be sure if the RequestAsyncStorage module exists (it is not part of the Next.js public
// API) we use a shim if it doesn't exist. The logic for this is in the wrapping loader.
// eslint-disable-next-line import/no-unresolved
import { requestAsyncStorage } from '__SENTRY_NEXTJS_REQUEST_ASYNC_STORAGE_SHIM__';
// @ts-expect-error We use `__SENTRY_WRAPPING_TARGET_FILE__` as a placeholder for the path to the file being wrapped.
// eslint-disable-next-line import/no-unresolved
import * as serverComponentModule from '__SENTRY_WRAPPING_TARGET_FILE__';
Expand All @@ -10,8 +6,6 @@ import * as Sentry from '@sentry/nextjs';

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

declare const requestAsyncStorage: RequestAsyncStorage;

declare const serverComponentModule: {
default: unknown;
};
Expand All @@ -30,6 +24,11 @@ if (typeof serverComponent === 'function') {

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

0 comments on commit fb327d0

Please sign in to comment.