Skip to content

Commit

Permalink
refactor(nuxt): rename nuxtMiddleware to appMiddleware
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Mar 16, 2024
1 parent c6aa617 commit cac7454
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 16 deletions.
6 changes: 3 additions & 3 deletions packages/nuxt/src/app/plugins/router.ts
Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions packages/nuxt/src/core/nitro.ts
Expand Up @@ -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
}
}
})
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion packages/nuxt/src/core/templates.ts
Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion packages/nuxt/src/pages/module.ts
Expand Up @@ -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')
Expand Down
6 changes: 3 additions & 3 deletions packages/nuxt/src/pages/runtime/plugins/router.ts
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion packages/nuxt/types.d.mts
Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion packages/nuxt/types.d.ts
Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/basic/nuxt.config.ts
Expand Up @@ -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 }
},
Expand Down

0 comments on commit cac7454

Please sign in to comment.