Skip to content

Commit

Permalink
docs: setting middleware at build time (#23480)
Browse files Browse the repository at this point in the history
  • Loading branch information
justinkekeocha committed Dec 14, 2023
1 parent db3a9f0 commit 677a144
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions docs/2.guide/2.directory-structure/1.middleware.md
Expand Up @@ -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)
}
}
})
```

0 comments on commit 677a144

Please sign in to comment.