From a814fc952b2b006d890781d1c2aa5fbd0936ded4 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Mon, 6 Nov 2023 15:26:29 +0000 Subject: [PATCH] fix(nextjs): Fix sourcemaps resolving for local dev when basePath is set --- .../common/devErrorSymbolicationEventProcessor.ts | 14 +++++++++++++- packages/nextjs/src/config/webpack.ts | 1 + 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/packages/nextjs/src/common/devErrorSymbolicationEventProcessor.ts b/packages/nextjs/src/common/devErrorSymbolicationEventProcessor.ts index d516780c0257..e918d4935dd7 100644 --- a/packages/nextjs/src/common/devErrorSymbolicationEventProcessor.ts +++ b/packages/nextjs/src/common/devErrorSymbolicationEventProcessor.ts @@ -1,4 +1,5 @@ import type { Event, EventHint } from '@sentry/types'; +import { GLOBAL_OBJ } from '@sentry/utils'; import type { StackFrame } from 'stacktrace-parser'; import * as stackTraceParser from 'stacktrace-parser'; @@ -8,6 +9,10 @@ type OriginalStackFrameResponse = { sourcePackage?: string; }; +const globalWithInjectedValues = GLOBAL_OBJ as typeof GLOBAL_OBJ & { + __sentryBasePath?: string; +}; + async function resolveStackFrame( frame: StackFrame, error: Error, @@ -26,13 +31,20 @@ async function resolveStackFrame( params.append(key, (frame[key as keyof typeof frame] ?? '').toString()); }); + let basePath = globalWithInjectedValues.__sentryBasePath ?? ''; + + // Prefix the basepath with a slash if it doesn't have one + if (basePath !== '' && !basePath.match(/^\//)) { + basePath = `/${basePath}`; + } + const controller = new AbortController(); const timer = setTimeout(() => controller.abort(), 3000); const res = await fetch( `${ // eslint-disable-next-line no-restricted-globals typeof window === 'undefined' ? 'http://localhost:3000' : '' // TODO: handle the case where users define a different port - }/__nextjs_original-stack-frame?${params.toString()}`, + }${basePath}/__nextjs_original-stack-frame?${params.toString()}`, { signal: controller.signal, }, diff --git a/packages/nextjs/src/config/webpack.ts b/packages/nextjs/src/config/webpack.ts index f4dd1d7ecfae..7b4ad3b6a857 100644 --- a/packages/nextjs/src/config/webpack.ts +++ b/packages/nextjs/src/config/webpack.ts @@ -955,6 +955,7 @@ function addValueInjectionLoader( SENTRY_RELEASE: buildContext.dev ? undefined : { id: sentryWebpackPluginOptions.release ?? getSentryRelease(buildContext.buildId) }, + __sentryBasePath: userNextConfig.basePath, }; const serverValues = {