Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(nextjs): Fix sourcemaps resolving for local dev when basePath is set #9457

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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,
Expand All @@ -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(/^\//)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l: can we do a startsWith('/') check instead of the regex?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would but it's not IE11 :(

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,
},
Expand Down
1 change: 1 addition & 0 deletions packages/nextjs/src/config/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,7 @@ function addValueInjectionLoader(
SENTRY_RELEASE: buildContext.dev
? undefined
: { id: sentryWebpackPluginOptions.release ?? getSentryRelease(buildContext.buildId) },
__sentryBasePath: userNextConfig.basePath,
};

const serverValues = {
Expand Down