mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 05:35:13 +00:00
feat(kit): add extendRouteRules
method (#9771)
This commit is contained in:
parent
fa129cb83d
commit
585e8be2e0
@ -61,6 +61,7 @@ description: Nuxt Kit provides composable utilities to help interacting with Nux
|
||||
[source code](https://github.com/nuxt/nuxt/blob/main/packages/kit/src/pages.ts)
|
||||
|
||||
- `extendPages (callback: pages => void)`
|
||||
- `extendRouteRules (route: string, rule: NitroRouteConfig, options: ExtendRouteRulesOptions)`
|
||||
|
||||
### Plugins
|
||||
|
||||
|
@ -1,4 +1,6 @@
|
||||
import type { NuxtHooks } from '@nuxt/schema'
|
||||
import type { NitroRouteConfig } from 'nitropack'
|
||||
import { defu } from 'defu'
|
||||
import { useNuxt } from './context'
|
||||
import { isNuxt2 } from './compatibility'
|
||||
|
||||
@ -11,3 +13,24 @@ export function extendPages (cb: NuxtHooks['pages:extend']) {
|
||||
nuxt.hook('pages:extend', cb)
|
||||
}
|
||||
}
|
||||
|
||||
export interface ExtendRouteRulesOptions {
|
||||
/**
|
||||
* Override route rule config
|
||||
*
|
||||
* @default false
|
||||
*/
|
||||
override?: boolean
|
||||
}
|
||||
|
||||
export function extendRouteRules (route: string, rule: NitroRouteConfig, options: ExtendRouteRulesOptions = {}) {
|
||||
const nuxt = useNuxt()
|
||||
for (const opts of [nuxt.options, nuxt.options.nitro]) {
|
||||
if (!opts.routeRules) {
|
||||
opts.routeRules = {}
|
||||
}
|
||||
opts.routeRules[route] = options.override
|
||||
? defu(rule, opts.routeRules[route])
|
||||
: defu(opts.routeRules[route], rule)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user