From 2d680455ae3eb68138e6a6771e3af1df8f0ff81e Mon Sep 17 00:00:00 2001 From: Julien Huang Date: Sun, 21 May 2023 00:26:29 +0200 Subject: [PATCH] fix(nuxt): skip middleware for islands components (#20924) --- packages/nuxt/src/app/plugins/router.ts | 26 +++---- .../nuxt/src/pages/runtime/plugins/router.ts | 70 ++++++++++--------- test/bundle.test.ts | 2 +- 3 files changed, 51 insertions(+), 47 deletions(-) diff --git a/packages/nuxt/src/app/plugins/router.ts b/packages/nuxt/src/app/plugins/router.ts index e824f79467..ae4ba74952 100644 --- a/packages/nuxt/src/app/plugins/router.ts +++ b/packages/nuxt/src/app/plugins/router.ts @@ -235,21 +235,23 @@ export default defineNuxtPlugin<{ route: Route, router: Router }>({ } nuxtApp._processingMiddleware = true - const middlewareEntries = new Set([...globalMiddleware, ...nuxtApp._middleware.global]) + if (process.client || !nuxtApp.ssrContext?.islandContext) { + const middlewareEntries = new Set([...globalMiddleware, ...nuxtApp._middleware.global]) - for (const middleware of middlewareEntries) { - const result = await nuxtApp.runWithContext(() => middleware(to, from)) - if (process.server) { - if (result === false || result instanceof Error) { - const error = result || createError({ - statusCode: 404, - statusMessage: `Page Not Found: ${initialURL}` - }) - delete nuxtApp._processingMiddleware - return nuxtApp.runWithContext(() => showError(error)) + for (const middleware of middlewareEntries) { + const result = await nuxtApp.runWithContext(() => middleware(to, from)) + if (process.server) { + if (result === false || result instanceof Error) { + const error = result || createError({ + statusCode: 404, + statusMessage: `Page Not Found: ${initialURL}` + }) + delete nuxtApp._processingMiddleware + return nuxtApp.runWithContext(() => showError(error)) + } } + if (result || result === false) { return result } } - if (result || result === false) { return result } } }) diff --git a/packages/nuxt/src/pages/runtime/plugins/router.ts b/packages/nuxt/src/pages/runtime/plugins/router.ts index 65f6a96d1a..28fcca3c73 100644 --- a/packages/nuxt/src/pages/runtime/plugins/router.ts +++ b/packages/nuxt/src/pages/runtime/plugins/router.ts @@ -135,42 +135,44 @@ const plugin: Plugin<{ router: Router }> = defineNuxtPlugin({ } nuxtApp._processingMiddleware = true - type MiddlewareDef = string | RouteMiddleware - const middlewareEntries = new Set([...globalMiddleware, ...nuxtApp._middleware.global]) - for (const component of to.matched) { - const componentMiddleware = component.meta.middleware as MiddlewareDef | MiddlewareDef[] - if (!componentMiddleware) { continue } - if (Array.isArray(componentMiddleware)) { - for (const entry of componentMiddleware) { - middlewareEntries.add(entry) - } - } else { - middlewareEntries.add(componentMiddleware) - } - } - - for (const entry of middlewareEntries) { - const middleware = typeof entry === 'string' ? nuxtApp._middleware.named[entry] || await namedMiddleware[entry]?.().then((r: any) => r.default || r) : entry - - if (!middleware) { - if (process.dev) { - throw new Error(`Unknown route middleware: '${entry}'. Valid middleware: ${Object.keys(namedMiddleware).map(mw => `'${mw}'`).join(', ')}.`) - } - throw new Error(`Unknown route middleware: '${entry}'.`) - } - - const result = await nuxtApp.runWithContext(() => middleware(to, from)) - if (process.server || (!nuxtApp.payload.serverRendered && nuxtApp.isHydrating)) { - if (result === false || result instanceof Error) { - const error = result || createError({ - statusCode: 404, - statusMessage: `Page Not Found: ${initialURL}` - }) - await nuxtApp.runWithContext(() => showError(error)) - return false + if (process.client || !nuxtApp.ssrContext?.islandContext) { + type MiddlewareDef = string | RouteMiddleware + const middlewareEntries = new Set([...globalMiddleware, ...nuxtApp._middleware.global]) + for (const component of to.matched) { + const componentMiddleware = component.meta.middleware as MiddlewareDef | MiddlewareDef[] + if (!componentMiddleware) { continue } + if (Array.isArray(componentMiddleware)) { + for (const entry of componentMiddleware) { + middlewareEntries.add(entry) + } + } else { + middlewareEntries.add(componentMiddleware) } } - if (result || result === false) { return result } + + for (const entry of middlewareEntries) { + const middleware = typeof entry === 'string' ? nuxtApp._middleware.named[entry] || await namedMiddleware[entry]?.().then((r: any) => r.default || r) : entry + + if (!middleware) { + if (process.dev) { + throw new Error(`Unknown route middleware: '${entry}'. Valid middleware: ${Object.keys(namedMiddleware).map(mw => `'${mw}'`).join(', ')}.`) + } + throw new Error(`Unknown route middleware: '${entry}'.`) + } + + const result = await nuxtApp.runWithContext(() => middleware(to, from)) + if (process.server || (!nuxtApp.payload.serverRendered && nuxtApp.isHydrating)) { + if (result === false || result instanceof Error) { + const error = result || createError({ + statusCode: 404, + statusMessage: `Page Not Found: ${initialURL}` + }) + await nuxtApp.runWithContext(() => showError(error)) + return false + } + } + if (result || result === false) { return result } + } } }) diff --git a/test/bundle.test.ts b/test/bundle.test.ts index f64ef20071..67b51ed7b1 100644 --- a/test/bundle.test.ts +++ b/test/bundle.test.ts @@ -45,7 +45,7 @@ describe.skipIf(isWindows || process.env.TEST_BUILDER === 'webpack' || process.e it('default server bundle size', async () => { stats.server = await analyzeSizes(['**/*.mjs', '!node_modules'], serverDir) - expect(roundToKilobytes(stats.server.totalBytes)).toMatchInlineSnapshot('"62.2k"') + expect(roundToKilobytes(stats.server.totalBytes)).toMatchInlineSnapshot('"62.4k"') const modules = await analyzeSizes('node_modules/**/*', serverDir) expect(roundToKilobytes(modules.totalBytes)).toMatchInlineSnapshot('"2283k"')