Skip to content

Commit de86706

Browse files
AbhiPrasadonurtemizkan
authored andcommitted
ref(nextjs): Remove metadata builder class (#4263)
* ref(nextjs): Remove metadata builder class Refactor the class into functions to save on bundle size. Extracted from changes in #4196. * add tests for nextjs metadata
1 parent c245c7e commit de86706

File tree

7 files changed

+47
-105
lines changed

7 files changed

+47
-105
lines changed

Diff for: packages/nextjs/src/index.client.ts

+2-3
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

Diff for: packages/nextjs/src/index.server.ts

+2-3
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

Diff for: packages/nextjs/src/utils/metadata.ts

+23
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+
}

Diff for: packages/nextjs/src/utils/metadataBuilder.ts

-45
This file was deleted.

Diff for: packages/nextjs/test/index.client.test.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,16 @@ describe('Client init()', () => {
3232
sdk: {
3333
name: 'sentry.javascript.nextjs',
3434
version: expect.any(String),
35-
packages: expect.any(Array),
35+
packages: [
36+
{
37+
name: 'npm:@sentry/nextjs',
38+
version: expect.any(String),
39+
},
40+
{
41+
name: 'npm:@sentry/react',
42+
version: expect.any(String),
43+
},
44+
],
3645
},
3746
},
3847
environment: 'test',

Diff for: packages/nextjs/test/index.server.test.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,16 @@ describe('Server init()', () => {
3737
sdk: {
3838
name: 'sentry.javascript.nextjs',
3939
version: expect.any(String),
40-
packages: expect.any(Array),
40+
packages: [
41+
{
42+
name: 'npm:@sentry/nextjs',
43+
version: expect.any(String),
44+
},
45+
{
46+
name: 'npm:@sentry/node',
47+
version: expect.any(String),
48+
},
49+
],
4150
},
4251
},
4352
autoSessionTracking: false,

Diff for: packages/nextjs/test/utils/metadataBuilder.test.ts

-52
This file was deleted.

0 commit comments

Comments
 (0)