Skip to content

Commit 9929a2a

Browse files
committed
fix(nuxt): Inject handler import from resolved path, delete addMiddlewareImports()
1 parent 02151f5 commit 9929a2a

2 files changed

Lines changed: 9 additions & 19 deletions

File tree

packages/nuxt/src/module.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
DEV_SERVER_CONFIG_PATH,
2222
} from './vite/addServerConfig';
2323
import { addDatabaseInstrumentation } from './vite/databaseConfig';
24-
import { addMiddlewareImports, addMiddlewareInstrumentation } from './vite/middlewareConfig';
24+
import { addMiddlewareInstrumentation } from './vite/middlewareConfig';
2525
import { setupOrchestrion } from './vite/orchestrion';
2626
import { setupSourceMaps } from './vite/sourceMaps';
2727
import { addStorageInstrumentation } from './vite/storageConfig';
@@ -120,8 +120,6 @@ export default defineNuxtModule<ModuleOptions>({
120120
addPlugin({ src: moduleDirResolver.resolve('./runtime/plugins/route-detector-legacy.server'), mode: 'server' });
121121
}
122122

123-
// Preps the middleware instrumentation module.
124-
addMiddlewareImports();
125123
addStorageInstrumentation(nuxt, !isNitroV3);
126124
addDatabaseInstrumentation(nuxt.options.nitro, !isNitroV3, moduleOptions);
127125

packages/nuxt/src/vite/middlewareConfig.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
1-
import { addServerImports, createResolver } from '@nuxt/kit';
1+
import { createResolver } from '@nuxt/kit';
22
import type { Nitro } from 'nitropack/types';
33
import * as path from 'path';
44
import type { InputPluginOption } from 'rollup';
55

6-
/**
7-
* Adds a server import for the middleware instrumentation.
8-
*/
9-
export function addMiddlewareImports(): void {
10-
addServerImports([
11-
{
12-
name: 'wrapMiddlewareHandlerWithSentry',
13-
from: createResolver(import.meta.url).resolve('./runtime/hooks/wrapMiddlewareHandler'),
14-
},
15-
]);
16-
}
17-
186
/**
197
* Adds middleware instrumentation to the Nitro build.
208
*
@@ -42,6 +30,9 @@ export function addMiddlewareInstrumentation(nitro: Nitro): void {
4230
*/
4331
function middlewareInstrumentationPlugin(nitro: Nitro): InputPluginOption {
4432
const middlewareFiles = new Set<string>();
33+
// Imported by absolute path rather than through `#imports`: with `imports.autoImport: false`,
34+
// Nuxt 5 generates an empty server `#imports` module, dropping `addServerImports` registrations.
35+
const wrapperModule = createResolver(import.meta.url).resolve('./runtime/hooks/wrapMiddlewareHandler');
4536

4637
return {
4738
name: 'sentry-nuxt-middleware-instrumentation',
@@ -58,7 +49,7 @@ function middlewareInstrumentationPlugin(nitro: Nitro): InputPluginOption {
5849
if (middlewareFiles.has(id)) {
5950
const fileName = path.basename(id);
6051
return {
61-
code: wrapMiddlewareCode(code, fileName),
52+
code: wrapMiddlewareCode(code, fileName, wrapperModule),
6253
map: null,
6354
};
6455
}
@@ -72,15 +63,16 @@ function middlewareInstrumentationPlugin(nitro: Nitro): InputPluginOption {
7263
*
7364
* @param originalCode The original user code of the middleware.
7465
* @param fileName The name of the middleware file, used for the span name and logging.
66+
* @param wrapperModule Absolute path of the module exporting `wrapMiddlewareHandlerWithSentry`.
7567
*
7668
* @returns The wrapped user code of the middleware.
7769
*/
78-
function wrapMiddlewareCode(originalCode: string, fileName: string): string {
70+
function wrapMiddlewareCode(originalCode: string, fileName: string, wrapperModule: string): string {
7971
// Remove common file extensions
8072
const cleanFileName = fileName.replace(/\.(ts|js|mjs|mts|cts)$/, '');
8173

8274
return `
83-
import { wrapMiddlewareHandlerWithSentry } from '#imports';
75+
import { wrapMiddlewareHandlerWithSentry } from ${JSON.stringify(wrapperModule)};
8476
8577
function defineInstrumentedEventHandler(handlerOrObject) {
8678
return defineEventHandler(wrapMiddlewareHandlerWithSentry(handlerOrObject, '${cleanFileName}'));

0 commit comments

Comments
 (0)