diff --git a/packages/nitro/src/compat.ts b/packages/nitro/src/compat.ts index 5fa8bd39f6..725f446b3f 100644 --- a/packages/nitro/src/compat.ts +++ b/packages/nitro/src/compat.ts @@ -57,7 +57,7 @@ export default function nuxt2CompatModule () { // Resolve middleware nuxt.hook('modules:done', () => { - const { middleware, legacyMiddleware } = resolveMiddleware(nuxt.options.serverMiddleware) + const { middleware, legacyMiddleware } = resolveMiddleware(nuxt) if (nuxt.server) { nuxt.server.setLegacyMiddleware(legacyMiddleware) } diff --git a/packages/nitro/src/server/middleware.ts b/packages/nitro/src/server/middleware.ts index cb34779e41..6c81e90cb3 100644 --- a/packages/nitro/src/server/middleware.ts +++ b/packages/nitro/src/server/middleware.ts @@ -2,7 +2,7 @@ import { resolve, join, extname } from 'upath' import { joinURL } from 'ufo' import globby from 'globby' import { watch } from 'chokidar' -import { tryResolvePath } from '@nuxt/kit' +import { tryResolvePath, Nuxt } from '@nuxt/kit' export interface ServerMiddleware { route: string @@ -51,11 +51,11 @@ export function scanMiddleware (serverDir: string, onChange?: (results: ServerMi return scan() } -export function resolveMiddleware (serverMiddleware: any[]) { +export function resolveMiddleware (nuxt: Nuxt) { const middleware: ServerMiddleware[] = [] const legacyMiddleware: ServerMiddleware[] = [] - for (let m of serverMiddleware) { + for (let m of nuxt.options.serverMiddleware) { if (typeof m === 'string') { m = { handler: m } } const route = m.path || m.route || '/' const handle = m.handler || m.handle @@ -67,7 +67,9 @@ export function resolveMiddleware (serverMiddleware: any[]) { middleware.push({ ...m, handle: tryResolvePath(handle, { - extensions: ['.ts', '.js'] + extensions: ['.ts', '.js'], + alias: nuxt.options.alias, + base: nuxt.options.srcDir }), route }) diff --git a/packages/nuxt3/src/nitro.ts b/packages/nuxt3/src/nitro.ts index f9692987a5..7696dd578e 100644 --- a/packages/nuxt3/src/nitro.ts +++ b/packages/nuxt3/src/nitro.ts @@ -22,7 +22,7 @@ export function initNitro (nuxt: Nuxt) { // Resolve middleware nuxt.hook('modules:done', () => { - const { middleware, legacyMiddleware } = resolveMiddleware(nuxt.options.serverMiddleware) + const { middleware, legacyMiddleware } = resolveMiddleware(nuxt) nuxt.server.setLegacyMiddleware(legacyMiddleware) nitroContext.middleware.push(...middleware) nitroDevContext.middleware.push(...middleware)