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) { if (isAppManifestEnabled) {
const routeRules = await nuxtApp.runWithContext(() => getRouteRules(to.path)) const routeRules = await nuxtApp.runWithContext(() => getRouteRules(to.path))
if (routeRules.nuxtMiddleware) { if (routeRules.appMiddleware) {
for (const key in routeRules.nuxtMiddleware) { for (const key in routeRules.appMiddleware) {
const guard = nuxtApp._middleware.named[key] as RouteGuard | undefined const guard = nuxtApp._middleware.named[key] as RouteGuard | undefined
if (!guard) { return } if (!guard) { return }
if (routeRules.nuxtMiddleware[key]) { if (routeRules.appMiddleware[key]) {
middlewareEntries.add(guard) middlewareEntries.add(guard)
} else { } else {
middlewareEntries.delete(guard) middlewareEntries.delete(guard)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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