Skip to content

Commit 7479626

Browse files
committed
ref(nextjs): Remove metadata builder class
Refactor the class into functions to save on bundle size. Extracted from changes in #4196.
1 parent 7ff913a commit 7479626

File tree

4 files changed

+31
-55
lines changed

4 files changed

+31
-55
lines changed

packages/nextjs/src/index.client.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { configureScope, init as reactInit, Integrations as BrowserIntegrations
22
import { BrowserTracing, defaultRequestInstrumentationOptions } from '@sentry/tracing';
33

44
import { nextRouterInstrumentation } from './performance/client';
5-
import { MetadataBuilder } from './utils/metadataBuilder';
5+
import { buildMetadata } from './utils/metadata';
66
import { NextjsOptions } from './utils/nextjsOptions';
77
import { addIntegration, UserIntegrations } from './utils/userIntegrations';
88

@@ -13,8 +13,7 @@ export const Integrations = { ...BrowserIntegrations, BrowserTracing };
1313

1414
/** Inits the Sentry NextJS SDK on the browser with the React SDK. */
1515
export function init(options: NextjsOptions): void {
16-
const metadataBuilder = new MetadataBuilder(options, ['nextjs', 'react']);
17-
metadataBuilder.addSdkMetadata();
16+
buildMetadata(options, ['nextjs', 'react']);
1817
options.environment = options.environment || process.env.NODE_ENV;
1918

2019
// Only add BrowserTracing if a tracesSampleRate or tracesSampler is set
@@ -27,9 +26,9 @@ export function init(options: NextjsOptions): void {
2726
...options,
2827
integrations,
2928
});
30-
configureScope(scope => {
29+
configureScope((scope) => {
3130
scope.setTag('runtime', 'browser');
32-
scope.addEventProcessor(event => (event.type === 'transaction' && event.transaction === '/404' ? null : event));
31+
scope.addEventProcessor((event) => (event.type === 'transaction' && event.transaction === '/404' ? null : event));
3332
});
3433
}
3534

packages/nextjs/src/index.server.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { escapeStringForRegex, logger } from '@sentry/utils';
66
import * as domainModule from 'domain';
77
import * as path from 'path';
88

9-
import { MetadataBuilder } from './utils/metadataBuilder';
9+
import { buildMetadata } from './utils/metadata';
1010
import { NextjsOptions } from './utils/nextjsOptions';
1111
import { addIntegration } from './utils/userIntegrations';
1212

@@ -43,8 +43,7 @@ export function init(options: NextjsOptions): void {
4343
return;
4444
}
4545

46-
const metadataBuilder = new MetadataBuilder(options, ['nextjs', 'node']);
47-
metadataBuilder.addSdkMetadata();
46+
buildMetadata(options, ['nextjs', 'node']);
4847
options.environment = options.environment || process.env.NODE_ENV;
4948
addServerIntegrations(options);
5049
// Right now we only capture frontend sessions for Next.js
@@ -62,7 +61,7 @@ export function init(options: NextjsOptions): void {
6261

6362
nodeInit(options);
6463

65-
configureScope(scope => {
64+
configureScope((scope) => {
6665
scope.setTag('runtime', 'node');
6766
if (isVercel) {
6867
scope.setTag('vercel', true);
@@ -102,7 +101,7 @@ function addServerIntegrations(options: NextjsOptions): void {
102101
const SOURCEMAP_FILENAME_REGEX = new RegExp(escapeStringForRegex(distDirAbsPath));
103102

104103
const defaultRewriteFramesIntegration = new RewriteFrames({
105-
iteratee: frame => {
104+
iteratee: (frame) => {
106105
frame.filename = frame.filename?.replace(SOURCEMAP_FILENAME_REGEX, 'app:///_next');
107106
return frame;
108107
},

packages/nextjs/src/utils/metadata.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { SDK_VERSION } from '@sentry/core';
2+
import { Options, SdkInfo } from '@sentry/types';
3+
4+
const PACKAGE_NAME_PREFIX = 'npm:@sentry/';
5+
6+
/**
7+
* A builder for the SDK metadata in the options for the SDK initialization.
8+
* @param options sdk options object that gets mutated
9+
* @param names list of package names
10+
*/
11+
export function buildMetadata(options: Options, names: string[]): void {
12+
options._metadata = options._metadata || {};
13+
options._metadata.sdk =
14+
options._metadata.sdk ||
15+
({
16+
name: 'sentry.javascript.nextjs',
17+
packages: names.map((name) => ({
18+
name: `${PACKAGE_NAME_PREFIX}/${name}`,
19+
version: SDK_VERSION,
20+
})),
21+
version: SDK_VERSION,
22+
} as SdkInfo);
23+
}

packages/nextjs/src/utils/metadataBuilder.ts

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)