Skip to content

Commit ef4367e

Browse files
committed
add RewriteFrames integration to client-side SDK
1 parent ff77d0c commit ef4367e

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

packages/nextjs/src/index.client.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { RewriteFrames } from '@sentry/integrations';
12
import { configureScope, init as reactInit, Integrations } from '@sentry/react';
23
import { BrowserTracing, defaultRequestInstrumentationOptions, hasTracingEnabled } from '@sentry/tracing';
34
import { EventProcessor } from '@sentry/types';
@@ -28,6 +29,8 @@ export { BrowserTracing };
2829
// Treeshakable guard to remove all code related to tracing
2930
declare const __SENTRY_TRACING__: boolean;
3031

32+
type GlobalWithAssetPrefixPath = typeof global & { __rewriteFramesAssetPrefixPath__: string };
33+
3134
/** Inits the Sentry NextJS SDK on the browser with the React SDK. */
3235
export function init(options: NextjsOptions): void {
3336
buildMetadata(options, ['nextjs', 'react']);
@@ -48,6 +51,25 @@ export function init(options: NextjsOptions): void {
4851
function addClientIntegrations(options: NextjsOptions): void {
4952
let integrations = options.integrations || [];
5053

54+
// This value is injected at build time, based on the output directory specified in the build config. Though a default
55+
// is set there, we set it here as well, just in case something has gone wrong with the injection.
56+
const assetPrefixPath = (global as GlobalWithAssetPrefixPath).__rewriteFramesAssetPrefixPath__ || '';
57+
58+
const defaultRewriteFramesIntegration = new RewriteFrames({
59+
// Turn `<origin>/<path>/_next/static/...` into `app:///_next/static/...`
60+
iteratee: frame => {
61+
try {
62+
const { origin } = new URL(frame.filename as string);
63+
frame.filename = frame.filename?.replace(origin, 'app://').replace(assetPrefixPath, '');
64+
} catch (err) {
65+
// Filename wasn't a properly formed URL, so there's nothing we can do
66+
}
67+
68+
return frame;
69+
},
70+
});
71+
integrations = addOrUpdateIntegration(defaultRewriteFramesIntegration, integrations);
72+
5173
// This evaluates to true unless __SENTRY_TRACING__ is text-replaced with "false", in which case everything inside
5274
// will get treeshaken away
5375
if (typeof __SENTRY_TRACING__ === 'undefined' || __SENTRY_TRACING__) {

0 commit comments

Comments
 (0)