- Anonymous (or inline) route middleware are defined directly within the page.
- Named route middleware, placed in the
app/middleware/and automatically loaded via asynchronous import when used on a page. - Global route middleware, placed in the
app/middleware/with a.globalsuffix and is run on every route change.
definePageMeta.
Name of middleware are normalized to kebab-case:
myMiddleware becomes my-middleware.Route middleware run within the Vue part of your Nuxt app. Despite the similar name, they are completely different from server middleware, which are run in the Nitro server part of your app.
Usage
Route middleware are navigation guards that receive the current route and the next route as arguments.middleware/my-middleware.ts
navigateTo- Redirects to the given routeabortNavigation- Aborts the navigation, with an optional error message.
vue-router, a third next() argument is not passed, and redirect or route cancellation is handled by returning a value from the middleware.
Possible return values are:
- nothing (a simple
returnor no return at all) - does not block navigation and will move to the next middleware function, if any, or complete the route navigation return navigateTo('/')- redirects to the given path and will set the redirect code to302Found if the redirect happens on the server sidereturn navigateTo('/', { redirectCode: 301 })- redirects to the given path and will set the redirect code to301Moved Permanently if the redirect happens on the server sidereturn abortNavigation()- stops the current navigationreturn abortNavigation(error)- rejects the current navigation with an error
Middleware Order
Middleware runs in the following order:- Global Middleware
- Page defined middleware order (if there are multiple middleware declared with the array syntax)
pages/profile.vue
analytics.global.tssetup.global.ts- Custom inline middleware
auth.ts
Ordering Global Middleware
By default, global middleware is executed alphabetically based on the filename. However, there may be times you want to define a specific order. For example, in the last scenario,setup.global.ts may need to run before analytics.global.ts. In that case, we recommend prefixing global middleware with ‘alphabetical’ numbering.
In case you’re new to ‘alphabetical’ numbering, remember that filenames are sorted as strings, not as numeric values. For example,
10.new.global.ts would come before 2.new.global.ts. This is why the example prefixes single digit numbers with 0.When Middleware Runs
If your site is server-rendered or generated, middleware for the initial page will be executed both when the page is rendered and then again on the client. This might be needed if your middleware needs a browser environment, such as if you have a generated site, aggressively cache responses, or want to read a value from local storage. However, if you want to avoid this behaviour you can do so:middleware/example.ts
Adding Middleware Dynamically
It is possible to add global or named route middleware manually using theaddRouteMiddleware() helper function, such as from within a plugin.
Example
auth route middleware will be run.