fix(nuxt): load layer middlewares before project middlewares (#22925)

This commit is contained in:
Joaquín Sánchez 2023-09-16 10:39:51 +02:00 committed by GitHub
parent 4c027e4b40
commit bc8a1fa614
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 18 additions and 5 deletions

View File

@ -116,9 +116,9 @@ async function resolveApp (nuxt: Nuxt, app: NuxtApp) {
} }
} }
// Resolve middleware/ from all config layers // Resolve middleware/ from all config layers, layers first
app.middleware = [] app.middleware = []
for (const config of nuxt.options._layers.map(layer => layer.config)) { for (const config of nuxt.options._layers.map(layer => layer.config).reverse()) {
const middlewareDir = (config.rootDir === nuxt.options.rootDir ? nuxt.options : config).dir?.middleware || 'middleware' const middlewareDir = (config.rootDir === nuxt.options.rootDir ? nuxt.options : config).dir?.middleware || 'middleware'
const middlewareFiles = await resolveFiles(config.srcDir, `${middlewareDir}/*{${nuxt.options.extensions.join(',')}}`) const middlewareFiles = await resolveFiles(config.srcDir, `${middlewareDir}/*{${nuxt.options.extensions.join(',')}}`)
app.middleware.push(...middlewareFiles.map((file) => { app.middleware.push(...middlewareFiles.map((file) => {

View File

@ -1007,10 +1007,15 @@ describe('extends support', () => {
expect(html).toContain('Middleware | foo: Injected by extended middleware from foo') expect(html).toContain('Middleware | foo: Injected by extended middleware from foo')
}) })
it('extends bar/middleware/override over foo/middleware/override', async () => { // theme is added after layers
it('extends foo/middleware/override over bar/middleware/override', async () => {
const html = await $fetch('/override') const html = await $fetch('/override')
expect(html).toContain('Middleware | override: Injected by extended middleware from bar') expect(html).toContain('Middleware | override: Injected by extended middleware from bar')
}) })
it('global middlewares sorting', async () => {
const html = await $fetch('/middleware/ordering')
expect(html).toContain('catchall at middleware')
})
}) })
describe('composables', () => { describe('composables', () => {

View File

@ -0,0 +1,3 @@
export default defineNuxtRouteMiddleware(() => {
useNuxtApp().extendsMiddleware = true
})

View File

@ -1,3 +1,3 @@
export default defineNuxtRouteMiddleware((to) => { export default defineNuxtRouteMiddleware((to) => {
to.meta.override = 'Injected by extended middleware from bar' to.meta.override = 'This middleware should be overriden by bar'
}) })

View File

@ -1,3 +1,3 @@
export default defineNuxtRouteMiddleware((to) => { export default defineNuxtRouteMiddleware((to) => {
to.meta.override = 'This middleware should be overriden by bar' to.meta.override = 'Injected by extended middleware from bar'
}) })

View File

@ -0,0 +1,5 @@
export default defineNuxtRouteMiddleware((to) => {
if (to.path === '/middleware/ordering' && !useNuxtApp().extendsMiddleware) {
return createError('extendsMiddleware not set in layer')
}
})