Skip to content

Commit a28951e

Browse files
authored
refactor: lazy-load sentry (#6128)
* refactor: lazy-load sentry * chore: other sentry changes
1 parent 6eda91b commit a28951e

5 files changed

Lines changed: 47 additions & 47 deletions

File tree

next.config.mjs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
import { withSentryConfig } from '@sentry/nextjs';
44
import withNextIntl from 'next-intl/plugin';
55

6-
import { BASE_PATH, ENABLE_STATIC_EXPORT } from './next.constants.mjs';
6+
import {
7+
BASE_PATH,
8+
ENABLE_STATIC_EXPORT,
9+
SENTRY_DSN,
10+
SENTRY_ENABLE,
11+
} from './next.constants.mjs';
712
import { redirects, rewrites } from './next.rewrites.mjs';
813

914
/** @type {import('next').NextConfig} */
@@ -78,28 +83,37 @@ const sentrySettings = {
7883
org: 'nodejs-org',
7984
// Define the Sentry Project on our Sentry Organisation
8085
project: 'nodejs-org',
86+
// Sentry DSN for the Node.js Website
87+
dsn: SENTRY_DSN,
8188
};
8289

8390
/** @type {import('@sentry/nextjs/types/config/types').UserSentryOptions} */
8491
const sentryConfig = {
8592
// Upload Next.js or third-party code in addition to our code
8693
widenClientFileUpload: true,
87-
// Transpile the Sentry code too since we target older browsers in our .browserslistrc
88-
transpileClientSDK: true,
8994
// Attempt to circumvent ad blockers
90-
tunnelRoute: ENABLE_STATIC_EXPORT ? undefined : '/monitoring',
95+
tunnelRoute: !ENABLE_STATIC_EXPORT && '/monitoring',
9196
// Prevent source map comments in built files
9297
hideSourceMaps: false,
9398
// Tree shake Sentry stuff from the bundle
9499
disableLogger: true,
100+
// Applies same WebPack Transpilation as Next.js
101+
transpileClientSDK: true,
95102
};
96103

97-
// Next.js Config with Sentry Configuration
98-
export default withSentryConfig(
104+
// Next.js Configuration with `next.intl` enabled
105+
const nextWithIntl = withNextIntl('./i18n.tsx')(nextConfig);
106+
107+
// Next.js Configuration with `sentry` enabled
108+
const nextWithSentry = withSentryConfig(
99109
// Next.js Config with i18n Configuration
100-
withNextIntl()(nextConfig),
110+
nextWithIntl,
101111
// Default Sentry Settings
102112
sentrySettings,
103113
// Default Sentry Extension Configuration
104114
sentryConfig
105115
);
116+
117+
// Decides wheter enabling Sentry or not
118+
// By default we only want to enable Sentry within a Vercel Environment
119+
export default SENTRY_ENABLE ? nextWithSentry : nextWithIntl;

next.constants.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,9 @@ export const DEFAULT_VIEWPORT = {
190190
*/
191191
export const SENTRY_DSN =
192192
'https://02884d0745aecaadf5f780278fe5fe70@o4506191161786368.ingest.sentry.io/4506191307735040';
193+
194+
/**
195+
* This states if Sentry should be enabled and bundled within our App
196+
*/
197+
export const SENTRY_ENABLE =
198+
process.env.NODE_ENV === 'development' || !!VERCEL_ENV;

sentry.client.config.ts

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,17 @@
1-
import { init, Replay } from '@sentry/nextjs';
1+
import { SENTRY_DSN, SENTRY_ENABLE } from '@/next.constants.mjs';
22

3-
import { SENTRY_DSN, VERCEL_ENV } from '@/next.constants.mjs';
4-
5-
init({
6-
// Only run Sentry on Vercel Environment
7-
enabled: !!VERCEL_ENV,
8-
// Tell Sentry where to send events
9-
dsn: SENTRY_DSN,
10-
// Percentage of events to send to Sentry (1% of them) (for performance metrics)
11-
tracesSampleRate: 0.01,
12-
// Percentage of sessions to sample (1%)
13-
replaysSessionSampleRate: 0.01,
14-
// Percentage of errors to sample (all of them)
15-
replaysOnErrorSampleRate: 1.0,
16-
// Support replaying client sessions to capture unhappy paths
17-
integrations: [new Replay()],
18-
// We only want to capture errors from _next folder on production
19-
// We don't want to capture errors from preview branches here
20-
allowUrls: ['https://nodejs.org/_next'],
21-
// Filter-out Events/Errors that are invalid for us
22-
beforeSend: (event, hint) => {
23-
const originalException = hint.originalException as Error;
24-
25-
// All the Events we send must have a message and stack trace
26-
if (originalException?.message && originalException?.stack) {
27-
// All our Events come eventually from code that originates on node_modules
28-
// so everything else should be discarded
29-
// Even React Errors or other errors will eventually have node_modules in the code
30-
if (String(originalException.stack).includes('node_modules')) {
31-
return event;
32-
}
33-
}
34-
35-
return null;
36-
},
37-
});
3+
// This lazy-loads Sentry on the Browser
4+
import('@sentry/nextjs').then(({ init }) =>
5+
init({
6+
// Only run Sentry on Vercel Environment
7+
enabled: SENTRY_ENABLE,
8+
// Tell Sentry where to send events
9+
dsn: SENTRY_DSN,
10+
// Disable Sentry Tracing as we don't need to have it
11+
// as Vercel already does Web Vitals and Performance Measurement on Client-Side
12+
enableTracing: false,
13+
// We only want to capture errors from _next folder on production
14+
// We don't want to capture errors from preview branches here
15+
allowUrls: ['https://nodejs.org/_next'],
16+
})
17+
);

sentry.edge.config.ts

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

3-
import { SENTRY_DSN, VERCEL_ENV } from '@/next.constants.mjs';
3+
import { SENTRY_DSN, SENTRY_ENABLE } from '@/next.constants.mjs';
44

55
init({
66
// Only run Sentry on Vercel Environment
7-
enabled: !!VERCEL_ENV,
7+
enabled: SENTRY_ENABLE,
88
// Tell Sentry where to send events
99
dsn: SENTRY_DSN,
1010
// Percentage of events to send to Sentry (1% of them) (for performance metrics)

sentry.server.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { init } from '@sentry/nextjs';
22
import { ProfilingIntegration } from '@sentry/profiling-node';
33

4-
import { SENTRY_DSN, VERCEL_ENV } from '@/next.constants.mjs';
4+
import { SENTRY_DSN, SENTRY_ENABLE } from '@/next.constants.mjs';
55

66
init({
77
// Only run Sentry on Vercel Environment
8-
enabled: !!VERCEL_ENV,
8+
enabled: SENTRY_ENABLE,
99
// Tell Sentry where to send events
1010
dsn: SENTRY_DSN,
1111
// Percentage of events to send to Sentry (1% of them) (for performance metrics)

0 commit comments

Comments
 (0)