Nuxt provides a customizable route middleware framework you can use throughout your application, ideal for extracting code that you want to run before navigating to a particular route.
There are three kinds of route middleware:
middleware/
directory and will be automatically loaded via asynchronous import when used on a page. (Note: The route middleware name is normalized to kebab-case, so someMiddleware
becomes some-middleware
.)middleware/
directory (with a .global
suffix) and will be automatically run on every route change.The first two kinds of route middleware can be defined in definePageMeta
.
Route middleware are navigation guards that receive the current route and the next route as arguments.
export default defineNuxtRouteMiddleware((to, from) => { if (to.params.id === '1') { return abortNavigation() } return navigateTo('/')})
Nuxt provides two globally available helpers that can be returned directly from the middleware:
navigateTo (to: RouteLocationRaw | undefined | null, options: { replace: boolean, redirectCode: number, external: boolean )
- Redirects to the given route, within plugins or middleware. It can also be called directly to perform page navigation.abortNavigation (err?: string | Error)
- Aborts the navigation, with an optional error message.Unlike navigation guards in the vue-router docs, a third next()
argument is not passed, and redirects or route cancellation is handled by returning a value from the middleware. Possible return values are:
return navigateTo('/')
or return navigateTo({ path: '/' })
- redirects to the given path and will set the redirect code to 302
Found if the redirect happens on the server sidereturn navigateTo('/', { redirectCode: 301 })
- redirects to the given path and will set the redirect code to 301
Moved Permanently if the redirect happens on the server sidereturn abortNavigation()
- stops the current navigationreturn abortNavigation(error)
- rejects the current navigation with an errorIt is possible to add global or named route middleware manually using the addRouteMiddleware()
helper function, such as from within a plugin.
export default defineNuxtPlugin(() => { addRouteMiddleware('global-test', () => { console.log('this global middleware was added in a plugin and will be run on every route change') }, { global: true }) addRouteMiddleware('named-test', () => { console.log('this named middleware was added in a plugin and would override any existing middleware of the same name') })})
-| middleware/---| auth.ts
In your page file, you can reference this route middleware
<script setup>definePageMeta({ middleware: ["auth"] // or middleware: 'auth'})</script>
Now, before navigation to that page can complete, the auth
route middleware will be run.