🌐 AI搜索 & 代理 主页
Skip to content

Commit 27dc1f2

Browse files
authored
fix(nuxt): print route middleware path in warning (#33136)
1 parent 911ce76 commit 27dc1f2

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

packages/nuxt/src/app/plugins/router.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ export default defineNuxtPlugin<{ route: Route, router: Router }>({
268268

269269
for (const middleware of middlewareEntries) {
270270
if (import.meta.dev) {
271-
nuxtApp._processingMiddleware = middleware.name || true
271+
nuxtApp._processingMiddleware = (middleware as any)._path || true
272272
}
273273
const result = await nuxtApp.runWithContext(() => middleware(to, from))
274274
if (import.meta.server) {

packages/nuxt/src/core/templates.ts

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { generateTypes, resolveSchema } from 'untyped'
66
import escapeRE from 'escape-string-regexp'
77
import { hash } from 'ohash'
88
import { camelCase } from 'scule'
9-
import { filename } from 'pathe/utils'
9+
import { filename, reverseResolveAlias } from 'pathe/utils'
1010
import type { Nitro } from 'nitropack/types'
1111

1212
import { annotatePlugins, checkForCircularDependencies } from './app'
@@ -331,14 +331,38 @@ export const layoutTemplate: NuxtTemplate = {
331331
// Add middleware template
332332
export const middlewareTemplate: NuxtTemplate = {
333333
filename: 'middleware.mjs',
334-
getContents ({ app }) {
334+
getContents ({ app, nuxt }) {
335335
const globalMiddleware = app.middleware.filter(mw => mw.global)
336336
const namedMiddleware = app.middleware.filter(mw => !mw.global)
337-
const namedMiddlewareObject = genObjectFromRawEntries(namedMiddleware.map(mw => [mw.name, genDynamicImport(mw.path)]))
337+
const alias = nuxt.options.dev ? { ...nuxt?.options.alias || {}, ...strippedAtAliases } : {}
338338
return [
339339
...globalMiddleware.map(mw => genImport(mw.path, genSafeVariableName(mw.name))),
340-
`export const globalMiddleware = ${genArrayFromRaw(globalMiddleware.map(mw => genSafeVariableName(mw.name)))}`,
341-
`export const namedMiddleware = ${namedMiddlewareObject}`,
340+
...!nuxt.options.dev
341+
? [
342+
`export const globalMiddleware = ${genArrayFromRaw(globalMiddleware.map(mw => genSafeVariableName(mw.name)))}`,
343+
`export const namedMiddleware = ${genObjectFromRawEntries(namedMiddleware.map(mw => [mw.name, genDynamicImport(mw.path)]))}`,
344+
]
345+
: [
346+
`const _globalMiddleware = ${genObjectFromRawEntries(globalMiddleware.map(mw => [reverseResolveAlias(mw.path, alias).pop() || mw.path, genSafeVariableName(mw.name)]))}`,
347+
`for (const path in _globalMiddleware) {`,
348+
` Object.defineProperty(_globalMiddleware[path], '_path', { value: path })`,
349+
`}`,
350+
`export const globalMiddleware = Object.values(_globalMiddleware)`,
351+
`const _namedMiddleware = ${genArrayFromRaw(namedMiddleware.map(mw => ({
352+
name: genString(mw.name),
353+
path: genString(reverseResolveAlias(mw.path, alias).pop() || mw.path),
354+
import: genDynamicImport(mw.path),
355+
})))}`,
356+
`for (const mw of _namedMiddleware) {`,
357+
` const i = mw.import`,
358+
` mw.import = () => i().then(r => {`,
359+
` Object.defineProperty(r.default || r, '_path', { value: mw.path })`,
360+
` return r`,
361+
` })`,
362+
`}`,
363+
`export const namedMiddleware = Object.fromEntries(_namedMiddleware.map(mw => [mw.name, mw.import]))`,
364+
],
365+
342366
].join('\n')
343367
},
344368
}
@@ -649,3 +673,8 @@ export const buildTypeTemplate: NuxtTemplate = {
649673
return declarations
650674
},
651675
}
676+
677+
const strippedAtAliases = {
678+
'@': '',
679+
'@@': '',
680+
}

packages/nuxt/src/pages/runtime/plugins/router.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ const plugin: Plugin<{ router: Router }> = defineNuxtPlugin({
224224

225225
try {
226226
if (import.meta.dev) {
227-
nuxtApp._processingMiddleware = (typeof entry === 'string' ? entry : middleware.name) || true
227+
nuxtApp._processingMiddleware = (middleware as any)._path || (typeof entry === 'string' ? entry : true)
228228
}
229229
const result = await nuxtApp.runWithContext(() => middleware(to, from))
230230
if (import.meta.server || (!nuxtApp.payload.serverRendered && nuxtApp.isHydrating)) {

0 commit comments

Comments
 (0)