Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
allowBuilds:
'@sentry/cli': true
minimumReleaseAge: 0
2 changes: 1 addition & 1 deletion packages/nuxt/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export default defineNuxtModule<ModuleOptions>({
}

if (serverConfigFile) {
addMiddlewareInstrumentation(nitro);
addMiddlewareInstrumentation(nitro, isNitroV3);

if (!usesDeprecatedInjectMode) {
addServerConfigShimWithWarning(nitro);
Expand Down
16 changes: 10 additions & 6 deletions packages/nuxt/src/vite/middlewareConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ export function addMiddlewareImports(): void {
* Adds middleware instrumentation to the Nitro build.
*
* @param nitro Nitro instance
* @param isNitroV3 Whether the app builds with Nitro v3 (Nuxt 5)
*/
export function addMiddlewareInstrumentation(nitro: Nitro): void {
export function addMiddlewareInstrumentation(nitro: Nitro, isNitroV3: boolean): void {
nitro.hooks.hook('rollup:before', (nitro, rollupConfig) => {
if (!rollupConfig.plugins) {
rollupConfig.plugins = [];
Expand All @@ -30,18 +31,20 @@ export function addMiddlewareInstrumentation(nitro: Nitro): void {
rollupConfig.plugins = [rollupConfig.plugins];
}

rollupConfig.plugins.push(middlewareInstrumentationPlugin(nitro));
rollupConfig.plugins.push(middlewareInstrumentationPlugin(nitro, isNitroV3));
});
}

/**
* Creates a rollup plugin for the middleware instrumentation by transforming the middleware code.
*
* @param nitro Nitro instance
* @param isNitroV3 Whether the app builds with Nitro v3 (Nuxt 5)
* @returns The rollup plugin for the middleware instrumentation.
*/
function middlewareInstrumentationPlugin(nitro: Nitro): InputPluginOption {
function middlewareInstrumentationPlugin(nitro: Nitro, isNitroV3: boolean): InputPluginOption {
const middlewareFiles = new Set<string>();
const wrapperModule = isNitroV3 ? '#imports/server' : '#imports';

return {
name: 'sentry-nuxt-middleware-instrumentation',
Expand All @@ -58,7 +61,7 @@ function middlewareInstrumentationPlugin(nitro: Nitro): InputPluginOption {
if (middlewareFiles.has(id)) {
const fileName = path.basename(id);
return {
code: wrapMiddlewareCode(code, fileName),
code: wrapMiddlewareCode(code, fileName, wrapperModule),
map: null,
};
}
Expand All @@ -72,15 +75,16 @@ function middlewareInstrumentationPlugin(nitro: Nitro): InputPluginOption {
*
* @param originalCode The original user code of the middleware.
* @param fileName The name of the middleware file, used for the span name and logging.
* @param wrapperModule Import specifier resolving to `wrapMiddlewareHandlerWithSentry`.
*
* @returns The wrapped user code of the middleware.
*/
function wrapMiddlewareCode(originalCode: string, fileName: string): string {
function wrapMiddlewareCode(originalCode: string, fileName: string, wrapperModule: string): string {
// Remove common file extensions
const cleanFileName = fileName.replace(/\.(ts|js|mjs|mts|cts)$/, '');

return `
import { wrapMiddlewareHandlerWithSentry } from '#imports';
import { wrapMiddlewareHandlerWithSentry } from '${wrapperModule}';

function defineInstrumentedEventHandler(handlerOrObject) {
return defineEventHandler(wrapMiddlewareHandlerWithSentry(handlerOrObject, '${cleanFileName}'));
Expand Down
Loading