mirror of
https://github.com/nuxt/nuxt.git
synced 2025-02-23 00:50:05 +00:00
fix(nuxt): sort global middlewares by name
This commit is contained in:
parent
559a72370b
commit
df8af3c414
@ -7,6 +7,7 @@ import type { Nuxt, NuxtApp, NuxtPlugin, NuxtTemplate, ResolvedNuxtTemplate } fr
|
|||||||
import * as defaultTemplates from './templates'
|
import * as defaultTemplates from './templates'
|
||||||
import { getNameFromPath, hasSuffix, uniqueBy } from './utils'
|
import { getNameFromPath, hasSuffix, uniqueBy } from './utils'
|
||||||
import { extractMetadata, orderMap } from './plugins/plugin-metadata'
|
import { extractMetadata, orderMap } from './plugins/plugin-metadata'
|
||||||
|
import {NuxtMiddleware} from "@nuxt/schema/src";
|
||||||
|
|
||||||
export function createApp (nuxt: Nuxt, options: Partial<NuxtApp> = {}): NuxtApp {
|
export function createApp (nuxt: Nuxt, options: Partial<NuxtApp> = {}): NuxtApp {
|
||||||
return defu(options, {
|
return defu(options, {
|
||||||
@ -117,13 +118,21 @@ async function resolveApp (nuxt: Nuxt, app: NuxtApp) {
|
|||||||
|
|
||||||
// Resolve middleware/ from all config layers
|
// Resolve middleware/ from all config layers
|
||||||
app.middleware = []
|
app.middleware = []
|
||||||
|
// Track global middlewares so we can apply a sorting mechanism to them
|
||||||
|
const globalMiddlewares: NuxtMiddleware[] = []
|
||||||
for (const config of nuxt.options._layers.map(layer => layer.config)) {
|
for (const config of nuxt.options._layers.map(layer => layer.config)) {
|
||||||
const middlewareFiles = await resolveFiles(config.srcDir, `${config.dir?.middleware || 'middleware'}/*{${nuxt.options.extensions.join(',')}}`)
|
const middlewareFiles = await resolveFiles(config.srcDir, `${config.dir?.middleware || 'middleware'}/*{${nuxt.options.extensions.join(',')}}`)
|
||||||
app.middleware.push(...middlewareFiles.map((file) => {
|
for (const path of middlewareFiles) {
|
||||||
const name = getNameFromPath(file)
|
const global = hasSuffix(path, '.global')
|
||||||
return { name, path: file, global: hasSuffix(file, '.global') }
|
const name = getNameFromPath(path)
|
||||||
}))
|
if (global) {
|
||||||
|
globalMiddlewares.push({ name, path, global })
|
||||||
|
} else {
|
||||||
|
app.middleware.push({ name, path, global })
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
app.middleware.unshift(...globalMiddlewares.sort((a, b) => a.name.localeCompare(b.name)))
|
||||||
|
|
||||||
// Resolve plugins
|
// Resolve plugins
|
||||||
app.plugins = [
|
app.plugins = [
|
||||||
|
Loading…
Reference in New Issue
Block a user