mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-18 17:35:57 +00:00
docs: setting middleware at build time (#23480)
This commit is contained in:
parent
db3a9f0554
commit
677a144d96
@ -171,3 +171,31 @@ definePageMeta({
|
||||
Now, before navigation to that page can complete, the `auth` route middleware will be run.
|
||||
|
||||
:link-example{to="/docs/examples/routing/middleware"}
|
||||
|
||||
## Setting Middleware At Build Time
|
||||
|
||||
Instead of using `definePageMeta` on each page, you can add named route middleware within the `pages:extend` hook.
|
||||
|
||||
```ts [nuxt.config.ts]
|
||||
import type { NuxtPage } from 'nuxt/schema'
|
||||
|
||||
export default defineNuxtConfig({
|
||||
hooks: {
|
||||
'pages:extend' (pages) {
|
||||
function setMiddleware (pages: NuxtPage[]) {
|
||||
for (const page of pages) {
|
||||
if (/* some condition */ true) {
|
||||
page.meta ||= {}
|
||||
// Note that this will override any middleware set in `definePageMeta` in the page
|
||||
page.meta.middleware = ['named']
|
||||
}
|
||||
if (page.children) {
|
||||
setMiddleware(page.children)
|
||||
}
|
||||
}
|
||||
}
|
||||
setMiddleware(pages)
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
Loading…
Reference in New Issue
Block a user