mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 05:35:13 +00:00
fix(nuxt): load layer middlewares before project middlewares (#22925)
This commit is contained in:
parent
4c027e4b40
commit
bc8a1fa614
@ -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 = []
|
||||
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 middlewareFiles = await resolveFiles(config.srcDir, `${middlewareDir}/*{${nuxt.options.extensions.join(',')}}`)
|
||||
app.middleware.push(...middlewareFiles.map((file) => {
|
||||
|
@ -1007,10 +1007,15 @@ describe('extends support', () => {
|
||||
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')
|
||||
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', () => {
|
||||
|
3
test/fixtures/basic/extends/bar/middleware/a.global.ts
vendored
Normal file
3
test/fixtures/basic/extends/bar/middleware/a.global.ts
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
export default defineNuxtRouteMiddleware(() => {
|
||||
useNuxtApp().extendsMiddleware = true
|
||||
})
|
@ -1,3 +1,3 @@
|
||||
export default defineNuxtRouteMiddleware((to) => {
|
||||
to.meta.override = 'Injected by extended middleware from bar'
|
||||
to.meta.override = 'This middleware should be overriden by bar'
|
||||
})
|
||||
|
2
test/fixtures/basic/extends/node_modules/foo/middleware/override.ts
generated
vendored
2
test/fixtures/basic/extends/node_modules/foo/middleware/override.ts
generated
vendored
@ -1,3 +1,3 @@
|
||||
export default defineNuxtRouteMiddleware((to) => {
|
||||
to.meta.override = 'This middleware should be overriden by bar'
|
||||
to.meta.override = 'Injected by extended middleware from bar'
|
||||
})
|
||||
|
5
test/fixtures/basic/middleware/b.global.ts
vendored
Normal file
5
test/fixtures/basic/middleware/b.global.ts
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
export default defineNuxtRouteMiddleware((to) => {
|
||||
if (to.path === '/middleware/ordering' && !useNuxtApp().extendsMiddleware) {
|
||||
return createError('extendsMiddleware not set in layer')
|
||||
}
|
||||
})
|
Loading…
Reference in New Issue
Block a user