Skip to content

Commit 97035a0

Browse files
authored
Tree shake unused code for Sentry (#2359)
1 parent db45c6d commit 97035a0

File tree

8 files changed

+48
-21
lines changed

8 files changed

+48
-21
lines changed

next.config.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = withSentryConfig(
99
GITBOOK_ASSETS_PREFIX: process.env.GITBOOK_ASSETS_PREFIX,
1010
},
1111

12-
webpack(config) {
12+
webpack(config, { dev, webpack }) {
1313
config.resolve.fallback = {
1414
...config.resolve.fallback,
1515

@@ -19,6 +19,21 @@ module.exports = withSentryConfig(
1919
http: false,
2020
};
2121

22+
// Tree shake debug code for Sentry
23+
// https://docs.sentry.io/platforms/javascript/guides/nextjs/configuration/tree-shaking/#tree-shaking-with-nextjs
24+
if (!dev) {
25+
config.plugins.push(
26+
new webpack.DefinePlugin({
27+
__SENTRY_DEBUG__: false,
28+
// We always init Sentry with enableTracing: false for now, so this is useless
29+
__SENTRY_TRACING__: false,
30+
__RRWEB_EXCLUDE_IFRAME__: true,
31+
__RRWEB_EXCLUDE_SHADOW_DOM__: true,
32+
__SENTRY_EXCLUDE_REPLAY_WORKER__: true,
33+
}),
34+
);
35+
}
36+
2237
return config;
2338
},
2439

@@ -45,9 +60,9 @@ module.exports = withSentryConfig(
4560
{
4661
protocol: 'https',
4762
hostname: '*.gitbook.io',
48-
}
49-
]
50-
}
63+
},
64+
],
65+
},
5166
},
5267
{
5368
silent: true,

sentry.client.config.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
import * as Sentry from '@sentry/nextjs';
1+
import {
2+
BrowserClient,
3+
makeFetchTransport,
4+
defaultStackParser,
5+
getCurrentScope,
6+
} from '@sentry/nextjs';
27

38
const dsn = process.env.SENTRY_DSN;
49
if (dsn) {
5-
Sentry.init({
10+
// To tree shake default integrations that we don't use
11+
// https://docs.sentry.io/platforms/javascript/guides/nextjs/configuration/tree-shaking/#tree-shaking-default-integrations
12+
const client = new BrowserClient({
613
debug: false,
714
dsn,
815
integrations: [],
@@ -11,5 +18,10 @@ if (dsn) {
1118
beforeSendTransaction: () => {
1219
return null;
1320
},
21+
transport: makeFetchTransport,
22+
stackParser: defaultStackParser,
1423
});
24+
25+
getCurrentScope().setClient(client);
26+
client.init();
1527
}

sentry.edge.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import * as Sentry from '@sentry/nextjs';
1+
import { init } from '@sentry/nextjs';
22

33
const dsn = process.env.SENTRY_DSN;
44
if (dsn) {
5-
Sentry.init({
5+
init({
66
debug: false,
77
dsn,
88

sentry.server.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import * as Sentry from '@sentry/nextjs';
1+
import { init } from '@sentry/nextjs';
22

33
const dsn = process.env.SENTRY_DSN;
44
if (dsn) {
5-
Sentry.init({
5+
init({
66
debug: false,
77
dsn,
88

src/app/(space)/error.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client';
22

3-
import * as Sentry from '@sentry/nextjs';
3+
import { captureException } from '@sentry/nextjs';
44
import React from 'react';
55

66
import { Button } from '@/components/primitives/Button';
@@ -15,7 +15,7 @@ export default function ErrorPage(props: {
1515
const language = useLanguage();
1616

1717
React.useEffect(() => {
18-
Sentry.captureException(error);
18+
captureException(error);
1919
}, [error]);
2020

2121
return (

src/app/global-error.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use client';
22

3-
import * as Sentry from '@sentry/nextjs';
3+
import { captureException } from '@sentry/nextjs';
44
import Error from 'next/error';
55
import { useEffect } from 'react';
66

77
export default function GlobalError({ error }: { error: Error }) {
88
useEffect(() => {
9-
Sentry.captureException(error);
9+
captureException(error);
1010
}, [error]);
1111

1212
return (

src/lib/tracing.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as Sentry from '@sentry/nextjs';
1+
import { startSpan } from '@sentry/nextjs';
22

33
export interface TraceSpan {
44
setAttribute: (label: string, value: boolean | string | number) => void;
@@ -22,7 +22,7 @@ export async function trace<T>(
2222
typeof name === 'string' ? { operation: name, name: undefined } : name;
2323
const completeName = executionName ? `${operation}(${executionName})` : operation;
2424

25-
return await Sentry.startSpan(
25+
return await startSpan(
2626
{
2727
name: completeName,
2828
op: operation,

src/middleware.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { GitBookAPI } from '@gitbook/api';
2-
import * as Sentry from '@sentry/nextjs';
2+
import { setTag, setContext } from '@sentry/nextjs';
33
import assertNever from 'assert-never';
44
import jwt from 'jsonwebtoken';
55
import type { ResponseCookie } from 'next/dist/compiled/@edge-runtime/cookies';
@@ -94,8 +94,8 @@ interface ContentAPITokenPayload {
9494
export async function middleware(request: NextRequest) {
9595
const { url, mode } = getInputURL(request);
9696

97-
Sentry.setTag('url', url.toString());
98-
Sentry.setContext('request', {
97+
setTag('url', url.toString());
98+
setContext('request', {
9999
method: request.method,
100100
url: url.toString(),
101101
rawRequestURL: request.url,
@@ -145,8 +145,8 @@ export async function middleware(request: NextRequest) {
145145
return writeCookies(NextResponse.redirect(normalizedVA.toString()), resolved.cookies);
146146
}
147147

148-
Sentry.setTag('space', resolved.space);
149-
Sentry.setContext('content', {
148+
setTag('space', resolved.space);
149+
setContext('content', {
150150
space: resolved.space,
151151
changeRequest: resolved.changeRequest,
152152
revision: resolved.revision,

0 commit comments

Comments
 (0)