Skip to content

Commit

Permalink
ref(nextjs): Remove metadata builder class
Browse files Browse the repository at this point in the history
Refactor the class into functions to save on bundle size. Extracted from
changes in #4196.
  • Loading branch information
AbhiPrasad committed Dec 10, 2021
1 parent 7ff913a commit 7479626
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 55 deletions.
9 changes: 4 additions & 5 deletions packages/nextjs/src/index.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { configureScope, init as reactInit, Integrations as BrowserIntegrations
import { BrowserTracing, defaultRequestInstrumentationOptions } from '@sentry/tracing';

import { nextRouterInstrumentation } from './performance/client';
import { MetadataBuilder } from './utils/metadataBuilder';
import { buildMetadata } from './utils/metadata';
import { NextjsOptions } from './utils/nextjsOptions';
import { addIntegration, UserIntegrations } from './utils/userIntegrations';

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

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

// Only add BrowserTracing if a tracesSampleRate or tracesSampler is set
Expand All @@ -27,9 +26,9 @@ export function init(options: NextjsOptions): void {
...options,
integrations,
});
configureScope(scope => {
configureScope((scope) => {
scope.setTag('runtime', 'browser');
scope.addEventProcessor(event => (event.type === 'transaction' && event.transaction === '/404' ? null : event));
scope.addEventProcessor((event) => (event.type === 'transaction' && event.transaction === '/404' ? null : event));
});
}

Expand Down
9 changes: 4 additions & 5 deletions packages/nextjs/src/index.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { escapeStringForRegex, logger } from '@sentry/utils';
import * as domainModule from 'domain';
import * as path from 'path';

import { MetadataBuilder } from './utils/metadataBuilder';
import { buildMetadata } from './utils/metadata';
import { NextjsOptions } from './utils/nextjsOptions';
import { addIntegration } from './utils/userIntegrations';

Expand Down Expand Up @@ -43,8 +43,7 @@ export function init(options: NextjsOptions): void {
return;
}

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

nodeInit(options);

configureScope(scope => {
configureScope((scope) => {
scope.setTag('runtime', 'node');
if (isVercel) {
scope.setTag('vercel', true);
Expand Down Expand Up @@ -102,7 +101,7 @@ function addServerIntegrations(options: NextjsOptions): void {
const SOURCEMAP_FILENAME_REGEX = new RegExp(escapeStringForRegex(distDirAbsPath));

const defaultRewriteFramesIntegration = new RewriteFrames({
iteratee: frame => {
iteratee: (frame) => {
frame.filename = frame.filename?.replace(SOURCEMAP_FILENAME_REGEX, 'app:///_next');
return frame;
},
Expand Down
23 changes: 23 additions & 0 deletions packages/nextjs/src/utils/metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { SDK_VERSION } from '@sentry/core';
import { Options, SdkInfo } from '@sentry/types';

const PACKAGE_NAME_PREFIX = 'npm:@sentry/';

/**
* A builder for the SDK metadata in the options for the SDK initialization.
* @param options sdk options object that gets mutated
* @param names list of package names
*/
export function buildMetadata(options: Options, names: string[]): void {
options._metadata = options._metadata || {};
options._metadata.sdk =
options._metadata.sdk ||
({
name: 'sentry.javascript.nextjs',
packages: names.map((name) => ({
name: `${PACKAGE_NAME_PREFIX}/${name}`,
version: SDK_VERSION,
})),
version: SDK_VERSION,
} as SdkInfo);
}
45 changes: 0 additions & 45 deletions packages/nextjs/src/utils/metadataBuilder.ts

This file was deleted.

0 comments on commit 7479626

Please sign in to comment.