Skip to content

Commit 5cb675d

Browse files
author
Luca Forstner
committed
feat(nextjs): Do not strip origin information from different origin stack frames
1 parent a31e0d3 commit 5cb675d

File tree

4 files changed

+26
-27
lines changed

4 files changed

+26
-27
lines changed

packages/nextjs/src/client/clientNormalizationIntegration.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
1-
import { rewriteFramesIntegration } from '@sentry/browser';
1+
import { WINDOW, rewriteFramesIntegration } from '@sentry/browser';
22
import { defineIntegration } from '@sentry/core';
33

4+
const windowOrigin = WINDOW.location.origin;
5+
46
export const nextjsClientStackFrameNormalizationIntegration = defineIntegration(
5-
({ assetPrefixPath }: { assetPrefixPath: string }) => {
7+
({ assetPrefix, basePath }: { assetPrefix?: string; basePath?: string }) => {
68
const rewriteFramesInstance = rewriteFramesIntegration({
79
// Turn `<origin>/<path>/_next/static/...` into `app:///_next/static/...`
810
iteratee: frame => {
9-
try {
10-
const { origin } = new URL(frame.filename as string);
11-
frame.filename = frame.filename?.replace(origin, 'app://').replace(assetPrefixPath, '');
12-
} catch (err) {
13-
// Filename wasn't a properly formed URL, so there's nothing we can do
11+
if (assetPrefix) {
12+
frame.filename = frame.filename?.replace(assetPrefix, 'app://');
13+
} else if (basePath) {
14+
try {
15+
const { origin: frameOrigin } = new URL(frame.filename as string);
16+
if (frameOrigin === windowOrigin) {
17+
frame.filename = frame.filename?.replace(frameOrigin, 'app://').replace(basePath, '');
18+
}
19+
} catch (err) {
20+
// Filename wasn't a properly formed URL, so there's nothing we can do
21+
}
1422
}
1523

1624
// We need to URI-decode the filename because Next.js has wildcard routes like "/users/[id].js" which show up as "/users/%5id%5.js" in Error stacktraces.

packages/nextjs/src/client/index.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ export { captureUnderscoreErrorException } from '../common/pages-router-instrume
1616
export { browserTracingIntegration } from './browserTracingIntegration';
1717

1818
const globalWithInjectedValues = GLOBAL_OBJ as typeof GLOBAL_OBJ & {
19-
_sentryRewriteFramesAssetPrefixPath: string;
19+
_sentryAssetPrefix?: string;
20+
_sentryBasePath?: string;
2021
};
2122

2223
// Treeshakable guard to remove all code related to tracing
@@ -67,13 +68,11 @@ function getDefaultIntegrations(options: BrowserOptions): Integration[] {
6768
customDefaultIntegrations.push(browserTracingIntegration());
6869
}
6970

70-
// This value is injected at build time, based on the output directory specified in the build config. Though a default
71+
// These values are injected at build time, based on the output directory specified in the build config. Though a default
7172
// is set there, we set it here as well, just in case something has gone wrong with the injection.
72-
const assetPrefixPath =
73-
process.env._sentryRewriteFramesAssetPrefixPath ||
74-
globalWithInjectedValues._sentryRewriteFramesAssetPrefixPath ||
75-
'';
76-
customDefaultIntegrations.push(nextjsClientStackFrameNormalizationIntegration({ assetPrefixPath }));
73+
const assetPrefix = process.env._sentryAssetPrefix || globalWithInjectedValues._sentryAssetPrefix;
74+
const basePath = process.env._sentryBasePath || globalWithInjectedValues._sentryBasePath;
75+
customDefaultIntegrations.push(nextjsClientStackFrameNormalizationIntegration({ assetPrefix, basePath }));
7776

7877
return customDefaultIntegrations;
7978
}

packages/nextjs/src/config/webpack.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -595,8 +595,6 @@ function addValueInjectionLoader(
595595
buildContext: BuildContext,
596596
releaseName: string | undefined,
597597
): void {
598-
const assetPrefix = userNextConfig.assetPrefix || userNextConfig.basePath || '';
599-
600598
const isomorphicValues = {
601599
// `rewritesTunnel` set by the user in Next.js config
602600
_sentryRewritesTunnelPath:
@@ -619,11 +617,7 @@ function addValueInjectionLoader(
619617

620618
const clientValues = {
621619
...isomorphicValues,
622-
// Get the path part of `assetPrefix`, minus any trailing slash. (We use a placeholder for the origin if
623-
// `assetPrefix` doesn't include one. Since we only care about the path, it doesn't matter what it is.)
624-
_sentryRewriteFramesAssetPrefixPath: assetPrefix
625-
? new URL(assetPrefix, 'http://dogs.are.great').pathname.replace(/\/$/, '')
626-
: '',
620+
_sentryAssetPrefix: userNextConfig.assetPrefix,
627621
};
628622

629623
if (buildContext.isServer) {

packages/nextjs/src/config/withSentryConfig.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,6 @@ function setUpTunnelRewriteRules(userNextConfig: NextConfigObject, tunnelPath: s
250250

251251
// TODO: For Turbopack we need to pass the release name here and pick it up in the SDK
252252
function setUpBuildTimeVariables(userNextConfig: NextConfigObject, userSentryOptions: SentryBuildOptions): void {
253-
const assetPrefix = userNextConfig.assetPrefix || userNextConfig.basePath || '';
254253
const basePath = userNextConfig.basePath ?? '';
255254
const rewritesTunnelPath =
256255
userSentryOptions.tunnelRoute !== undefined && userNextConfig.output !== 'export'
@@ -261,11 +260,6 @@ function setUpBuildTimeVariables(userNextConfig: NextConfigObject, userSentryOpt
261260
// Make sure that if we have a windows path, the backslashes are interpreted as such (rather than as escape
262261
// characters)
263262
_sentryRewriteFramesDistDir: userNextConfig.distDir?.replace(/\\/g, '\\\\') || '.next',
264-
// Get the path part of `assetPrefix`, minus any trailing slash. (We use a placeholder for the origin if
265-
// `assetPrefix` doesn't include one. Since we only care about the path, it doesn't matter what it is.)
266-
_sentryRewriteFramesAssetPrefixPath: assetPrefix
267-
? new URL(assetPrefix, 'http://dogs.are.great').pathname.replace(/\/$/, '')
268-
: '',
269263
};
270264

271265
if (rewritesTunnelPath) {
@@ -276,6 +270,10 @@ function setUpBuildTimeVariables(userNextConfig: NextConfigObject, userSentryOpt
276270
buildTimeVariables._sentryBasePath = basePath;
277271
}
278272

273+
if (userNextConfig.assetPrefix) {
274+
buildTimeVariables._sentryAssetPrefix = userNextConfig.assetPrefix;
275+
}
276+
279277
if (typeof userNextConfig.env === 'object') {
280278
userNextConfig.env = { ...buildTimeVariables, ...userNextConfig.env };
281279
} else if (userNextConfig.env === undefined) {

0 commit comments

Comments
 (0)