import { PAGELOAD } from '@sentry/conventions/op'; import { getClient, GLOBAL_OBJ } from '@sentry/core'; import { _INTERNAL_withCacheOrigin } from '@sentry/core/browser'; import { browserTracingIntegration, _INTERNAL_createVueRouteProvider, getRouteProvider, setRouteProvider, vueIntegration, } from '@sentry/vue'; import { defineNuxtPlugin } from 'nuxt/app'; import type { GlobalObjWithIntegrationOptions } from '../../client/vueIntegration'; import { reportNuxtError } from '../utils'; import { isIgnoredNuxtError } from '../utils/isIgnoredNuxtError'; // --- Types are copied from @sentry/vue (so it does not need to be exported) --- // The following type is an intersection of the Route type from VueRouter v2, v3, and v4. // This is not great, but kinda necessary to make it work with all versions at the same time. type Route = { /** Unparameterized URL */ path: string; /** * Query params (keys map to null when there is no value associated, e.g. "?foo" and to an array when there are * multiple query params that have the same key, e.g. "?foo&foo=bar") */ query: Record; /** Route name (VueRouter provides a way to give routes individual names) */ name?: string | symbol | null | undefined; /** Evaluated parameters */ params: Record; /** All the matched route objects as defined in VueRouter constructor */ matched: { path: string }[]; }; interface VueRouter { onError: (fn: (err: Error) => void) => void; beforeEach: (fn: (to: Route, from: Route, next?: () => void) => void) => void; } type VueRouteProviderRouter = ReturnType[0]>; // Tree-shakable guard to remove all code related to tracing declare const __SENTRY_TRACING__: boolean; export default defineNuxtPlugin({ name: 'sentry-client-integrations', dependsOn: ['sentry-client-config'], async setup(nuxtApp) { // Registered outside the tracing guard, because route parameterization should not depend on // tracing: anything that needs a route name (bfcache metrics, web vitals) can resolve one even // when tracing is tree-shaken away. Nuxt installs the router before its plugins run, so unlike // `@sentry/vue` this can read it straight off `nuxtApp`. const client = getClient(); // A `routeProvider` passed to `Sentry.init` is the user's choice, so it is left in place. if (client && '$router' in nuxtApp && !getRouteProvider(client)) { setRouteProvider( _INTERNAL_createVueRouteProvider(() => nuxtApp.$router as VueRouteProviderRouter), client, ); } // This evaluates to true unless __SENTRY_TRACING__ is text-replaced with "false", in which case everything inside // will get tree-shaken away if (typeof __SENTRY_TRACING__ === 'undefined' || __SENTRY_TRACING__) { const sentryClient = getClient(); if (sentryClient && '$router' in nuxtApp) { sentryClient.addIntegration( browserTracingIntegration({ router: nuxtApp.$router as VueRouter, routeLabel: 'path', // A client-side shell (`ssr: false`) has no server-rendered content to mark. beforeStartSpan: options => options.op === PAGELOAD && nuxtApp.payload.serverRendered === true ? _INTERNAL_withCacheOrigin(options, 'static') : options, }), ); } } nuxtApp.hook('app:created', vueApp => { const sentryClient = getClient(); if (sentryClient) { // Adding the Vue integration without the Vue error handler // Nuxt is registering their own error handler, which is unset after hydration: https://github.com/nuxt/nuxt/blob/d3fdbcaac6cf66d21e25d259390d7824696f1a87/packages/nuxt/src/app/entry.ts#L64-L73 // We don't want to wrap the existing error handler, as it leads to a 500 error: https://github.com/getsentry/sentry-javascript/issues/12515 sentryClient.addIntegration( vueIntegration({ // We pick up the options from the "fake" vueIntegration exported by the SDK. ...(GLOBAL_OBJ as GlobalObjWithIntegrationOptions)._sentryNuxtVueIntegrationOptions, app: vueApp, attachErrorHandler: false, }), ); } }); // Capture exception in the next event loop, to make sure that all breadcrumbs are recorded in time. nuxtApp.hook('app:error', error => { if (isIgnoredNuxtError(error)) { return; } setTimeout(() => { reportNuxtError({ error }); }); }); // Nuxt calls `vue:error` for every error that reaches the root component, so an error thrown during setup // (e.g. `createError({ statusCode: 404 })`) arrives here too, not only in `app:error`. nuxtApp.hook('vue:error', (error, instance, info) => { if (isIgnoredNuxtError(error)) { return; } setTimeout(() => { reportNuxtError({ error, instance, info }); }); }); }, });