1
+ import { RewriteFrames } from '@sentry/integrations' ;
1
2
import { configureScope , init as reactInit , Integrations } from '@sentry/react' ;
2
3
import { BrowserTracing , defaultRequestInstrumentationOptions , hasTracingEnabled } from '@sentry/tracing' ;
3
4
import { EventProcessor } from '@sentry/types' ;
@@ -28,6 +29,8 @@ export { BrowserTracing };
28
29
// Treeshakable guard to remove all code related to tracing
29
30
declare const __SENTRY_TRACING__ : boolean ;
30
31
32
+ type GlobalWithAssetPrefixPath = typeof global & { __rewriteFramesAssetPrefixPath__ : string } ;
33
+
31
34
/** Inits the Sentry NextJS SDK on the browser with the React SDK. */
32
35
export function init ( options : NextjsOptions ) : void {
33
36
buildMetadata ( options , [ 'nextjs' , 'react' ] ) ;
@@ -48,6 +51,25 @@ export function init(options: NextjsOptions): void {
48
51
function addClientIntegrations ( options : NextjsOptions ) : void {
49
52
let integrations = options . integrations || [ ] ;
50
53
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
+
51
73
// This evaluates to true unless __SENTRY_TRACING__ is text-replaced with "false", in which case everything inside
52
74
// will get treeshaken away
53
75
if ( typeof __SENTRY_TRACING__ === 'undefined' || __SENTRY_TRACING__ ) {
0 commit comments