|
1 | | -import { SENTRY_DSN, SENTRY_ENABLE } from '@/next.constants.mjs'; |
| 1 | +import { |
| 2 | + Dedupe, |
| 3 | + Breadcrumbs, |
| 4 | + HttpContext, |
| 5 | + LinkedErrors, |
| 6 | + BrowserClient, |
| 7 | + getCurrentHub, |
| 8 | + defaultStackParser, |
| 9 | + makeFetchTransport, |
| 10 | +} from '@sentry/nextjs'; |
2 | 11 |
|
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 | | -); |
| 12 | +import { |
| 13 | + SENTRY_DSN, |
| 14 | + SENTRY_ENABLE, |
| 15 | + SENTRY_CAPTURE_RATE, |
| 16 | + SENTRY_TUNNEL, |
| 17 | +} from '@/next.constants.mjs'; |
| 18 | + |
| 19 | +// This creates a custom Sentry Client with minimal integrations |
| 20 | +export const sentryClient = new BrowserClient({ |
| 21 | + // Only run Sentry on Vercel Environment |
| 22 | + enabled: SENTRY_ENABLE, |
| 23 | + // Provide Sentry's Secret Key |
| 24 | + dsn: SENTRY_DSN, |
| 25 | + // Sentry's Error Transport Mechanism |
| 26 | + transport: makeFetchTransport, |
| 27 | + // Sentry's Stack Trace Parser |
| 28 | + stackParser: defaultStackParser, |
| 29 | + // All supported Integrations by us |
| 30 | + integrations: [ |
| 31 | + new Dedupe(), |
| 32 | + new HttpContext(), |
| 33 | + new Breadcrumbs(), |
| 34 | + new LinkedErrors(), |
| 35 | + ], |
| 36 | + // We only want to allow ingestion from these pre-selected allowed URLs |
| 37 | + // Note that the vercel.app prefix is for our Pull Request Branch Previews |
| 38 | + allowUrls: ['https://nodejs.org/', /^https:\/\/.+\.vercel\.app/], |
| 39 | + // Percentage of events to send to Sentry (1% of them) (for performance metrics) |
| 40 | + tracesSampleRate: SENTRY_CAPTURE_RATE, |
| 41 | + // Percentage of events to send to Sentry (1% of them) (for session replays) |
| 42 | + replaysSessionSampleRate: SENTRY_CAPTURE_RATE, |
| 43 | + // Percentage of events to send to Sentry (1% of them) (for session replays when error happens) |
| 44 | + replaysOnErrorSampleRate: 1.0, |
| 45 | + // Provides a custom Sentry Tunnel Router |
| 46 | + // @note these are components of the Sentry DSN string |
| 47 | + // @see @sentry/nextjs/esm/client/tunnelRoute.js |
| 48 | + tunnel: SENTRY_TUNNEL(`?o=4506191161786368&p=4506191307735040`), |
| 49 | + // Adds custom filtering before sending an Event to Sentry |
| 50 | + beforeSend: (event, hint) => { |
| 51 | + // Attempts to grab the original Exception before any "magic" happens |
| 52 | + const exception = hint.originalException as Error; |
| 53 | + |
| 54 | + // We only want to capture Errors that have a Stack Trace and that are not Anonymous Errors |
| 55 | + return exception?.stack && !exception.stack.includes('<anonymous>') |
| 56 | + ? event |
| 57 | + : null; |
| 58 | + }, |
| 59 | +}); |
| 60 | + |
| 61 | +// Attaches this Browser Client to Sentry |
| 62 | +getCurrentHub().bindClient(sentryClient); |
| 63 | + |
| 64 | +// Loads this Dynamically to avoid adding this to the main bundle (initial load) |
| 65 | +import('@sentry/nextjs').then(({ Replay, BrowserTracing }) => { |
| 66 | + sentryClient.addIntegration(new Replay({ maskAllText: false })); |
| 67 | + sentryClient.addIntegration(new BrowserTracing()); |
| 68 | +}); |
0 commit comments