From bc8a1fa614eb084aae49cdfea8babdaaf7948335 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaqu=C3=ADn=20S=C3=A1nchez?= Date: Sat, 16 Sep 2023 10:39:51 +0200 Subject: [PATCH] fix(nuxt): load layer middlewares before project middlewares (#22925) --- packages/nuxt/src/core/app.ts | 4 ++-- test/basic.test.ts | 7 ++++++- test/fixtures/basic/extends/bar/middleware/a.global.ts | 3 +++ test/fixtures/basic/extends/bar/middleware/override.ts | 2 +- .../basic/extends/node_modules/foo/middleware/override.ts | 2 +- test/fixtures/basic/middleware/b.global.ts | 5 +++++ 6 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 test/fixtures/basic/extends/bar/middleware/a.global.ts create mode 100644 test/fixtures/basic/middleware/b.global.ts diff --git a/packages/nuxt/src/core/app.ts b/packages/nuxt/src/core/app.ts index d5913947c4..ad328e2b39 100644 --- a/packages/nuxt/src/core/app.ts +++ b/packages/nuxt/src/core/app.ts @@ -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) => { diff --git a/test/basic.test.ts b/test/basic.test.ts index 8118262645..a5461fc2e8 100644 --- a/test/basic.test.ts +++ b/test/basic.test.ts @@ -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', () => { diff --git a/test/fixtures/basic/extends/bar/middleware/a.global.ts b/test/fixtures/basic/extends/bar/middleware/a.global.ts new file mode 100644 index 0000000000..bf57318277 --- /dev/null +++ b/test/fixtures/basic/extends/bar/middleware/a.global.ts @@ -0,0 +1,3 @@ +export default defineNuxtRouteMiddleware(() => { + useNuxtApp().extendsMiddleware = true +}) diff --git a/test/fixtures/basic/extends/bar/middleware/override.ts b/test/fixtures/basic/extends/bar/middleware/override.ts index 00edf78124..fbbfd73091 100644 --- a/test/fixtures/basic/extends/bar/middleware/override.ts +++ b/test/fixtures/basic/extends/bar/middleware/override.ts @@ -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' }) diff --git a/test/fixtures/basic/extends/node_modules/foo/middleware/override.ts b/test/fixtures/basic/extends/node_modules/foo/middleware/override.ts index fbbfd73091..00edf78124 100644 --- a/test/fixtures/basic/extends/node_modules/foo/middleware/override.ts +++ b/test/fixtures/basic/extends/node_modules/foo/middleware/override.ts @@ -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' }) diff --git a/test/fixtures/basic/middleware/b.global.ts b/test/fixtures/basic/middleware/b.global.ts new file mode 100644 index 0000000000..4acb1cd605 --- /dev/null +++ b/test/fixtures/basic/middleware/b.global.ts @@ -0,0 +1,5 @@ +export default defineNuxtRouteMiddleware((to) => { + if (to.path === '/middleware/ordering' && !useNuxtApp().extendsMiddleware) { + return createError('extendsMiddleware not set in layer') + } +})