Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

docs(api): add defineNuxtRouteMiddleware util #6933

Merged
merged 5 commits into from
Aug 26, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 53 additions & 3 deletions docs/content/3.api/3.utils/define-nuxt-route-middleware.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,57 @@
# `defineNuxtRouteMiddleware`

::ReadMore{link="/guide/features/routing"}
::
You can create named route middleware using `defineNuxtRouteMiddleware` helper function.

Route middleware are generally stored in the `middleware/` directory unless otherwise customized using [nuxt.config](/api/configuration/nuxt.config#middleware).

## Usage

```ts
export default defineNuxtRouteMiddleware((middleware: RouteMiddleware) => {})
```

### Parameters

`defineNuxtRouteMiddleware` provides parameter of type `RouteMiddleware` which includes two Vue route objects:

- **to:** `type: RouteMiddleware` - `to` is the first argument that return the next route

- **from:** `type: RouteMiddleware` - `from` is the second argument that returns the current route

Learn more about available properties ofΒ **[route objects](https://v3.nuxtjs.org/api/composables/use-route)**Β .

::NeedContribution
## Examples

### Show error page

You can use route middleware to throw errors and show helpful error messages.

```ts [middleware/error.ts]
export default defineNuxtRouteMiddleware((to) => {
if (to.params.id === '1') {
showError({ statusCode: 404, statusMessage: "Page Not Found" })
DamianGlowala marked this conversation as resolved.
Show resolved Hide resolved
}
})
```

Above route middleware will take user on a custom error page defined at `~/error.vue` and show error message and code as passed from the middleware.

### Redirection

The route middleware below shows how you can use `useState` in combination with `navigateTo` helper method to redirect users on different routes based on their authentication status.

```ts [middleware/auth.ts]
export default defineNuxtRouteMiddleware((to, from) => {
const auth = useState('auth')

if (!auth.value.authenticated) {
return navigateTo('/login')
}
return navigateTo('/dashboard')
})
```

Both [navigateTo](/api/utils/navigate-to) and [abortNavigation](/api/utils/abort-navigation) are globally available helper methods that you can use inside `defineNuxtRouteMiddleware` helper function.

::ReadMore{link="/guide/features/routing"}
::