refactor(nuxt): rename nuxtMiddleware to appMiddleware

This commit is contained in:
Daniel Roe 2024-03-16 15:03:09 -07:00
parent c6aa617839
commit cac7454702
No known key found for this signature in database
GPG Key ID: CBC814C393D93268
8 changed files with 16 additions and 16 deletions

View File

@ -249,12 +249,12 @@ export default defineNuxtPlugin<{ route: Route, router: Router }>({
if (isAppManifestEnabled) {
const routeRules = await nuxtApp.runWithContext(() => getRouteRules(to.path))
if (routeRules.nuxtMiddleware) {
for (const key in routeRules.nuxtMiddleware) {
if (routeRules.appMiddleware) {
for (const key in routeRules.appMiddleware) {
const guard = nuxtApp._middleware.named[key] as RouteGuard | undefined
if (!guard) { return }
if (routeRules.nuxtMiddleware[key]) {
if (routeRules.appMiddleware[key]) {
middlewareEntries.add(guard)
} else {
middlewareEntries.delete(guard)

View File

@ -267,16 +267,16 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
nuxt.hook('nitro:config', (config) => {
const rules = config.routeRules
for (const rule in rules) {
if (!(rules[rule] as any).nuxtMiddleware) { continue }
const value = (rules[rule] as any).nuxtMiddleware
if (!(rules[rule] as any).appMiddleware) { continue }
const value = (rules[rule] as any).appMiddleware
if (typeof value === 'string') {
(rules[rule] as NitroOptions['routeRules']).nuxtMiddleware = { [value]: true }
(rules[rule] as NitroOptions['routeRules']).appMiddleware = { [value]: true }
} else if (Array.isArray(value)) {
const normalizedRules: Record<string, boolean> = {}
for (const middleware of value) {
normalizedRules[middleware] = true
}
(rules[rule] as NitroOptions['routeRules']).nuxtMiddleware = normalizedRules
(rules[rule] as NitroOptions['routeRules']).appMiddleware = normalizedRules
}
}
})
@ -291,7 +291,7 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
const filteredRules = {} as Record<string, any>
for (const routeKey in _routeRules[key]) {
const value = (_routeRules as any)[key][routeKey]
if (['prerender', 'redirect', 'nuxtMiddleware'].includes(routeKey) && value) {
if (['prerender', 'redirect', 'appMiddleware'].includes(routeKey) && value) {
if (routeKey === 'redirect') {
filteredRules[routeKey] = typeof value === 'string' ? value : value.to
} else {

View File

@ -244,7 +244,7 @@ declare module 'nitropack' {
interface NitroRouteRules {
ssr?: boolean
experimentalNoScripts?: boolean
nuxtMiddleware?: Record<string, boolean>
appMiddleware?: Record<string, boolean>
}
interface NitroRuntimeHooks {
'dev:ssr-logs': (ctx: { logs: LogObject[], path: string }) => void | Promise<void>

View File

@ -477,7 +477,7 @@ export default defineNuxtModule({
'}',
'declare module \'nitropack\' {',
' interface NitroRouteConfig {',
' nuxtMiddleware?: MiddlewareKey | MiddlewareKey[] | Record<MiddlewareKey, boolean>',
' appMiddleware?: MiddlewareKey | MiddlewareKey[] | Record<MiddlewareKey, boolean>',
' }',
'}'
].join('\n')

View File

@ -179,9 +179,9 @@ const plugin: Plugin<{ router: Router }> = defineNuxtPlugin({
if (isAppManifestEnabled) {
const routeRules = await nuxtApp.runWithContext(() => getRouteRules(to.path))
if (routeRules.nuxtMiddleware) {
for (const key in routeRules.nuxtMiddleware) {
if (routeRules.nuxtMiddleware[key]) {
if (routeRules.appMiddleware) {
for (const key in routeRules.appMiddleware) {
if (routeRules.appMiddleware[key]) {
middlewareEntries.add(key)
} else {
middlewareEntries.delete(key)

View File

@ -26,7 +26,7 @@ declare module 'nitropack' {
interface NitroRouteRules {
ssr?: boolean
experimentalNoScripts?: boolean
nuxtMiddleware?: Record<string, boolean>
appMiddleware?: Record<string, boolean>
}
interface NitroRuntimeHooks {
'dev:ssr-logs': (ctx: { logs: LogObject[], path: string }) => void | Promise<void>

View File

@ -26,7 +26,7 @@ declare module 'nitropack' {
interface NitroRouteRules {
ssr?: boolean
experimentalNoScripts?: boolean
nuxtMiddleware?: Record<string, boolean>
appMiddleware?: Record<string, boolean>
}
interface NitroRuntimeHooks {
'dev:ssr-logs': (ctx: { logs: LogObject[], path: string }) => void | Promise<void>

View File

@ -60,7 +60,7 @@ export default defineNuxtConfig({
},
routeRules: {
'/route-rules/spa': { ssr: false },
'/route-rules/middleware': { nuxtMiddleware: 'route-rules-middleware' },
'/route-rules/middleware': { appMiddleware: 'route-rules-middleware' },
'/hydration/spa-redirection/**': { ssr: false },
'/no-scripts': { experimentalNoScripts: true }
},