mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 05:35:13 +00:00
fix(nuxt): skip middleware for islands components (#20924)
This commit is contained in:
parent
b88aab049f
commit
2d680455ae
@ -235,21 +235,23 @@ export default defineNuxtPlugin<{ route: Route, router: Router }>({
|
|||||||
}
|
}
|
||||||
nuxtApp._processingMiddleware = true
|
nuxtApp._processingMiddleware = true
|
||||||
|
|
||||||
const middlewareEntries = new Set<RouteGuard>([...globalMiddleware, ...nuxtApp._middleware.global])
|
if (process.client || !nuxtApp.ssrContext?.islandContext) {
|
||||||
|
const middlewareEntries = new Set<RouteGuard>([...globalMiddleware, ...nuxtApp._middleware.global])
|
||||||
|
|
||||||
for (const middleware of middlewareEntries) {
|
for (const middleware of middlewareEntries) {
|
||||||
const result = await nuxtApp.runWithContext(() => middleware(to, from))
|
const result = await nuxtApp.runWithContext(() => middleware(to, from))
|
||||||
if (process.server) {
|
if (process.server) {
|
||||||
if (result === false || result instanceof Error) {
|
if (result === false || result instanceof Error) {
|
||||||
const error = result || createError({
|
const error = result || createError({
|
||||||
statusCode: 404,
|
statusCode: 404,
|
||||||
statusMessage: `Page Not Found: ${initialURL}`
|
statusMessage: `Page Not Found: ${initialURL}`
|
||||||
})
|
})
|
||||||
delete nuxtApp._processingMiddleware
|
delete nuxtApp._processingMiddleware
|
||||||
return nuxtApp.runWithContext(() => showError(error))
|
return nuxtApp.runWithContext(() => showError(error))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
if (result || result === false) { return result }
|
||||||
}
|
}
|
||||||
if (result || result === false) { return result }
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -135,42 +135,44 @@ const plugin: Plugin<{ router: Router }> = defineNuxtPlugin({
|
|||||||
}
|
}
|
||||||
nuxtApp._processingMiddleware = true
|
nuxtApp._processingMiddleware = true
|
||||||
|
|
||||||
type MiddlewareDef = string | RouteMiddleware
|
if (process.client || !nuxtApp.ssrContext?.islandContext) {
|
||||||
const middlewareEntries = new Set<MiddlewareDef>([...globalMiddleware, ...nuxtApp._middleware.global])
|
type MiddlewareDef = string | RouteMiddleware
|
||||||
for (const component of to.matched) {
|
const middlewareEntries = new Set<MiddlewareDef>([...globalMiddleware, ...nuxtApp._middleware.global])
|
||||||
const componentMiddleware = component.meta.middleware as MiddlewareDef | MiddlewareDef[]
|
for (const component of to.matched) {
|
||||||
if (!componentMiddleware) { continue }
|
const componentMiddleware = component.meta.middleware as MiddlewareDef | MiddlewareDef[]
|
||||||
if (Array.isArray(componentMiddleware)) {
|
if (!componentMiddleware) { continue }
|
||||||
for (const entry of componentMiddleware) {
|
if (Array.isArray(componentMiddleware)) {
|
||||||
middlewareEntries.add(entry)
|
for (const entry of componentMiddleware) {
|
||||||
}
|
middlewareEntries.add(entry)
|
||||||
} else {
|
}
|
||||||
middlewareEntries.add(componentMiddleware)
|
} 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 (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 }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -45,7 +45,7 @@ describe.skipIf(isWindows || process.env.TEST_BUILDER === 'webpack' || process.e
|
|||||||
|
|
||||||
it('default server bundle size', async () => {
|
it('default server bundle size', async () => {
|
||||||
stats.server = await analyzeSizes(['**/*.mjs', '!node_modules'], serverDir)
|
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)
|
const modules = await analyzeSizes('node_modules/**/*', serverDir)
|
||||||
expect(roundToKilobytes(modules.totalBytes)).toMatchInlineSnapshot('"2283k"')
|
expect(roundToKilobytes(modules.totalBytes)).toMatchInlineSnapshot('"2283k"')
|
||||||
|
Loading…
Reference in New Issue
Block a user