From 97035a054a2b735f7a91c936f569944cdb87ff4e Mon Sep 17 00:00:00 2001 From: Johan Preynat Date: Tue, 25 Jun 2024 15:55:48 +0200 Subject: [PATCH] Tree shake unused code for Sentry (#2359) --- next.config.js | 23 +++++++++++++++++++---- sentry.client.config.ts | 16 ++++++++++++++-- sentry.edge.config.ts | 4 ++-- sentry.server.config.ts | 4 ++-- src/app/(space)/error.tsx | 4 ++-- src/app/global-error.tsx | 4 ++-- src/lib/tracing.ts | 4 ++-- src/middleware.ts | 10 +++++----- 8 files changed, 48 insertions(+), 21 deletions(-) diff --git a/next.config.js b/next.config.js index d66066bea..cfcc05037 100644 --- a/next.config.js +++ b/next.config.js @@ -9,7 +9,7 @@ module.exports = withSentryConfig( GITBOOK_ASSETS_PREFIX: process.env.GITBOOK_ASSETS_PREFIX, }, - webpack(config) { + webpack(config, { dev, webpack }) { config.resolve.fallback = { ...config.resolve.fallback, @@ -19,6 +19,21 @@ module.exports = withSentryConfig( http: false, }; + // Tree shake debug code for Sentry + // https://docs.sentry.io/platforms/javascript/guides/nextjs/configuration/tree-shaking/#tree-shaking-with-nextjs + if (!dev) { + config.plugins.push( + new webpack.DefinePlugin({ + __SENTRY_DEBUG__: false, + // We always init Sentry with enableTracing: false for now, so this is useless + __SENTRY_TRACING__: false, + __RRWEB_EXCLUDE_IFRAME__: true, + __RRWEB_EXCLUDE_SHADOW_DOM__: true, + __SENTRY_EXCLUDE_REPLAY_WORKER__: true, + }), + ); + } + return config; }, @@ -45,9 +60,9 @@ module.exports = withSentryConfig( { protocol: 'https', hostname: '*.gitbook.io', - } - ] - } + }, + ], + }, }, { silent: true, diff --git a/sentry.client.config.ts b/sentry.client.config.ts index ca1397fef..a83f7540e 100644 --- a/sentry.client.config.ts +++ b/sentry.client.config.ts @@ -1,8 +1,15 @@ -import * as Sentry from '@sentry/nextjs'; +import { + BrowserClient, + makeFetchTransport, + defaultStackParser, + getCurrentScope, +} from '@sentry/nextjs'; const dsn = process.env.SENTRY_DSN; if (dsn) { - Sentry.init({ + // To tree shake default integrations that we don't use + // https://docs.sentry.io/platforms/javascript/guides/nextjs/configuration/tree-shaking/#tree-shaking-default-integrations + const client = new BrowserClient({ debug: false, dsn, integrations: [], @@ -11,5 +18,10 @@ if (dsn) { beforeSendTransaction: () => { return null; }, + transport: makeFetchTransport, + stackParser: defaultStackParser, }); + + getCurrentScope().setClient(client); + client.init(); } diff --git a/sentry.edge.config.ts b/sentry.edge.config.ts index 77aba0797..16a57e884 100644 --- a/sentry.edge.config.ts +++ b/sentry.edge.config.ts @@ -1,8 +1,8 @@ -import * as Sentry from '@sentry/nextjs'; +import { init } from '@sentry/nextjs'; const dsn = process.env.SENTRY_DSN; if (dsn) { - Sentry.init({ + init({ debug: false, dsn, diff --git a/sentry.server.config.ts b/sentry.server.config.ts index 77aba0797..16a57e884 100644 --- a/sentry.server.config.ts +++ b/sentry.server.config.ts @@ -1,8 +1,8 @@ -import * as Sentry from '@sentry/nextjs'; +import { init } from '@sentry/nextjs'; const dsn = process.env.SENTRY_DSN; if (dsn) { - Sentry.init({ + init({ debug: false, dsn, diff --git a/src/app/(space)/error.tsx b/src/app/(space)/error.tsx index 7aed4408b..79af35a98 100644 --- a/src/app/(space)/error.tsx +++ b/src/app/(space)/error.tsx @@ -1,6 +1,6 @@ 'use client'; -import * as Sentry from '@sentry/nextjs'; +import { captureException } from '@sentry/nextjs'; import React from 'react'; import { Button } from '@/components/primitives/Button'; @@ -15,7 +15,7 @@ export default function ErrorPage(props: { const language = useLanguage(); React.useEffect(() => { - Sentry.captureException(error); + captureException(error); }, [error]); return ( diff --git a/src/app/global-error.tsx b/src/app/global-error.tsx index 3ad15da3c..a4a68aca2 100644 --- a/src/app/global-error.tsx +++ b/src/app/global-error.tsx @@ -1,12 +1,12 @@ 'use client'; -import * as Sentry from '@sentry/nextjs'; +import { captureException } from '@sentry/nextjs'; import Error from 'next/error'; import { useEffect } from 'react'; export default function GlobalError({ error }: { error: Error }) { useEffect(() => { - Sentry.captureException(error); + captureException(error); }, [error]); return ( diff --git a/src/lib/tracing.ts b/src/lib/tracing.ts index 880e2095b..d2a2afd23 100644 --- a/src/lib/tracing.ts +++ b/src/lib/tracing.ts @@ -1,4 +1,4 @@ -import * as Sentry from '@sentry/nextjs'; +import { startSpan } from '@sentry/nextjs'; export interface TraceSpan { setAttribute: (label: string, value: boolean | string | number) => void; @@ -22,7 +22,7 @@ export async function trace( typeof name === 'string' ? { operation: name, name: undefined } : name; const completeName = executionName ? `${operation}(${executionName})` : operation; - return await Sentry.startSpan( + return await startSpan( { name: completeName, op: operation, diff --git a/src/middleware.ts b/src/middleware.ts index 6e924f8d2..85360de78 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -1,5 +1,5 @@ import { GitBookAPI } from '@gitbook/api'; -import * as Sentry from '@sentry/nextjs'; +import { setTag, setContext } from '@sentry/nextjs'; import assertNever from 'assert-never'; import jwt from 'jsonwebtoken'; import type { ResponseCookie } from 'next/dist/compiled/@edge-runtime/cookies'; @@ -94,8 +94,8 @@ interface ContentAPITokenPayload { export async function middleware(request: NextRequest) { const { url, mode } = getInputURL(request); - Sentry.setTag('url', url.toString()); - Sentry.setContext('request', { + setTag('url', url.toString()); + setContext('request', { method: request.method, url: url.toString(), rawRequestURL: request.url, @@ -145,8 +145,8 @@ export async function middleware(request: NextRequest) { return writeCookies(NextResponse.redirect(normalizedVA.toString()), resolved.cookies); } - Sentry.setTag('space', resolved.space); - Sentry.setContext('content', { + setTag('space', resolved.space); + setContext('content', { space: resolved.space, changeRequest: resolved.changeRequest, revision: resolved.revision,