import { addPlugin, addPluginTemplate, addTemplate, addVitePlugin, createResolver, defineNuxtModule } from '@nuxt/kit'; // Needed to make TS evaluate the augmentation of Nitro types (https://github.com/nuxt/nuxt/pull/34039) import type {} from '@nuxt/nitro-server'; import * as path from 'path'; import { NUXT_BUILD_ASSETS_DIR_GLOBAL, NUXT_CDN_URL_GLOBAL } from './common/buildAssets'; import { NUXT_BASE_PATH_GLOBAL } from './common/tunnel'; import type { SentryNuxtModuleOptions } from './common/types'; import { setupCloudflareEntry } from './vite/cloudflare'; import { addTunnelPlugin, setupNitroServer } from './vite/nitroServer'; import { setupOrchestrion } from './vite/orchestrion'; import { NUXT_RELEASE_RUNTIME_CONFIG_KEY, setupSourceMaps } from './vite/sourceMaps'; import { findCloudflareOptionsFile, findDefaultSdkInitFile, resolveServerApi } from './vite/utils'; import { setupViteServer } from './vite/viteServer'; import { stringifyForCodeInjection, warnOnConflictingSourceMapOptions } from '@sentry/core'; export type ModuleOptions = SentryNuxtModuleOptions; type NuxtPageSubset = { file?: string; path: string }; export default defineNuxtModule({ meta: { name: '@sentry/nuxt/module', configKey: 'sentry', compatibility: { nuxt: '>=3.7.0', }, }, defaults: {}, async setup(moduleOptionsParam, nuxt) { if (moduleOptionsParam?.enabled === false) { return; } const serverApi = resolveServerApi(); const moduleOptions = { ...moduleOptionsParam, // oxlint-disable-next-line typescript/no-deprecated -- supported until removal autoInjectServerSentry: moduleOptionsParam.autoInjectServerSentry, // oxlint-disable-next-line typescript/no-deprecated -- supported until removal experimental_entrypointWrappedFunctions: moduleOptionsParam.experimental_entrypointWrappedFunctions || [ 'default', 'handler', 'server', ], }; const moduleDirResolver = createResolver(import.meta.url); const buildDirResolver = createResolver(nuxt.options.buildDir); const clientConfigFile = await findDefaultSdkInitFile('client', nuxt, moduleOptions); if (clientConfigFile) { // Inject the client-side Sentry config file with a side effect import addPluginTemplate({ mode: 'client', filename: 'sentry-client-config.mjs', order: 0, // Dynamic import of config file to wrap it within a Nuxt context (here: defineNuxtPlugin) // Makes it possible to call useRuntimeConfig() in the user-defined sentry config file getContents: () => ` import { defineNuxtPlugin, useRuntimeConfig } from "#imports"; export default defineNuxtPlugin({ name: 'sentry-client-config', async setup() { const runtimeConfig = useRuntimeConfig(); globalThis.${NUXT_BASE_PATH_GLOBAL} = runtimeConfig.app.baseURL; globalThis.${NUXT_BUILD_ASSETS_DIR_GLOBAL} = runtimeConfig.app.buildAssetsDir; globalThis.${NUXT_CDN_URL_GLOBAL} = runtimeConfig.app.cdnURL; const sentryRelease = runtimeConfig.public.${NUXT_RELEASE_RUNTIME_CONFIG_KEY}; if (sentryRelease) { globalThis.SENTRY_RELEASE = { id: sentryRelease }; } await import("${buildDirResolver.resolve(`/${clientConfigFile}`)}") } });`, }); // Add the plugin which loads client integrations etc. - // this must run after the sentry-client-config plugin has run, and the client is initialized! addPlugin({ src: moduleDirResolver.resolve('./runtime/plugins/sentry.client'), mode: 'client', order: 1, }); } const serverConfigFile = await findDefaultSdkInitFile('server', nuxt, moduleOptions); const cloudflareOptionsFile = await findCloudflareOptionsFile(nuxt, moduleOptions); const nuxtMajor = parseInt((nuxt as unknown as { _version: string })._version?.split('.')[0] ?? '3', 10); const isMinNuxtV4 = nuxtMajor >= 4; // Orchestrion runs on both the Node path (gated on a server config file) and the Cloudflare path // (which has no server config file, the SDK is set up via `withSentry` or `sentryCloudflareNitroPlugin`). The // Cloudflare detection happens inside, keyed off the resolved Nitro preset. setupOrchestrion(nuxt, !!serverConfigFile, moduleOptions.buildTimeInstrumentation); if (cloudflareOptionsFile) { setupCloudflareEntry(nuxt, cloudflareOptionsFile); } if (serverApi === 'nuxt') { if (serverConfigFile) { setupViteServer(nuxt, serverConfigFile, moduleOptions); } } else if (serverConfigFile || cloudflareOptionsFile) { setupNitroServer(nuxt, serverApi, serverConfigFile, moduleOptions, !!cloudflareOptionsFile); } else { // `sentryCloudflareNitroPlugin` initializes the SDK without a config file, so it needs the tunnel too. addTunnelPlugin(serverApi); } // Names the server spans of pages after their route, with the SDK set up from the server config, from // `sentry.cloudflare.config.ts` or from `sentryCloudflareNitroPlugin`. if (isMinNuxtV4) { addPlugin({ src: moduleDirResolver.resolve('./runtime/plugins/route-detector.server'), mode: 'server' }); } else { addPlugin({ src: moduleDirResolver.resolve('./runtime/plugins/route-detector-legacy.server'), mode: 'server' }); } if ((nuxt.options.experimental as { ssrErrorBoundary?: boolean }).ssrErrorBoundary) { addPlugin({ src: moduleDirResolver.resolve('./runtime/plugins/error-boundary.server'), mode: 'server' }); } if (clientConfigFile || serverConfigFile || cloudflareOptionsFile) { // The bundler plugins do not get `sourcemaps.delete`. warnOnConflictingSourceMapOptions(moduleOptions.sourcemaps); setupSourceMaps(moduleOptions, nuxt, addVitePlugin, !!clientConfigFile, serverApi === 'nuxt'); } let pagesData: NuxtPageSubset[] = []; nuxt.hooks.hook('pages:extend', pages => { pagesData = pages .map(page => ({ file: page.file, path: page.path })) .filter(page => { // Check for dynamic parameter (e.g., :userId or [userId]) return page.path.includes(':') || page?.file?.includes('['); }); }); if (isMinNuxtV4) { const pagesDataVirtualModuleId = '#sentry/nuxt-pages-data.mjs'; // Vite virtual plugin (for the Vite SSR build, where addPlugin mode:'server' plugins are bundled) addVitePlugin({ name: 'sentry-nuxt-pages-data-virtual', resolveId: (id: string) => (id === pagesDataVirtualModuleId ? `\0${pagesDataVirtualModuleId}` : null), load: (id: string) => id === `\0${pagesDataVirtualModuleId}` ? `export default ${stringifyForCodeInjection(pagesData, null, 2)};` : undefined, }); } else { // Nuxt v3: register as a build template (accessible via #build/) addTemplate({ filename: 'sentry--nuxt-pages-data.mjs', getContents: () => `export default ${stringifyForCodeInjection(pagesData, null, 2)};`, }); } // Add the sentry config file to the include array nuxt.hook('prepare:types', options => { const tsConfig = options.tsConfig as { include?: string[] }; if (!tsConfig.include) { tsConfig.include = []; } // Add type references for useRuntimeConfig in root files for nuxt v4 // Should be relative to `root/.nuxt` if (clientConfigFile) { const relativePath = path.relative(nuxt.options.buildDir, clientConfigFile); tsConfig.include.push(relativePath); } if (serverConfigFile) { const relativePath = path.relative(nuxt.options.buildDir, serverConfigFile); tsConfig.include.push(relativePath); } if (cloudflareOptionsFile) { const relativePath = path.relative(nuxt.options.buildDir, cloudflareOptionsFile); tsConfig.include.push(relativePath); } }); }, });