Route middleware lets you run code before a navigation completes — checking authentication, verifying permissions, or redirecting users to the right page. Nuxe automatically discovers everyDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/dvlkit/nuxe/llms.txt
Use this file to discover all available pages before exploring further.
.ts or .js file inside app/middleware/ and wires them into the router, so you never need to register them manually.
Types of middleware
Nuxe recognises three flavours of middleware based purely on the file name:Named
Applied only to specific pages that opt in via
definePage({ meta: { middleware: 'admin' } }). The middleware name is derived from the file name.Global
Files ending in
.global.ts run automatically on every navigation — client-side and SSR alike.Server-only global
Files ending in
.server.global.ts run during SSR only. They are skipped entirely after the app hydrates on the client.File naming conventions
| File name | Kind | Registered name |
|---|---|---|
admin.ts | Named | admin |
auth.global.ts | Global (client + SSR) | — |
auth.server.global.ts | Global, SSR only | — |
01-auth.global.ts | Global, runs first (ordered) | — |
_helpers.ts | Ignored — _-prefix skips scanning | — |
- or _ separator (e.g. 01-auth.global.ts) to control the order in which multiple global middlewares execute. Files without a numeric prefix are sorted after all numbered ones.
Files whose names start with
_ are skipped by the middleware scanner. Use this convention for shared helper modules that should not be registered as middleware themselves.defineNuxeRouteMiddleware
Every middleware file must export a default function wrapped in defineNuxeRouteMiddleware. This is a thin identity wrapper that provides TypeScript types — it accepts any Vue Router NavigationGuard.
(to, from) arguments as a standard Vue Router navigation guard and can return undefined (continue), a route location (redirect), or false (cancel).
Named middleware example
app/middleware/admin.ts guards the admin section. It logs the navigation and, in a real app, would redirect non-admin users away:
definePage:
Global middleware example
app/middleware/auth.global.ts runs before every navigation without needing to opt in per page:
Server-only global middleware example
app/middleware/auth.server.global.ts is identical in structure but only executes during the initial server-side render — ideal for reading cookies or session data that only exist on the server:
navigateTo(to, options?)
Call navigateTo inside a middleware guard to redirect the user. It handles both client-side navigation and SSR HTTP redirects automatically.
The destination route. Pass a string path, a route location object, or
null/undefined to continue navigation unchanged.Replace the current history entry instead of pushing a new one. Defaults to
false.HTTP status code used during SSR redirects. Defaults to
302. Has no effect on client-side navigation.Treat
to as an external URL and navigate via window.location.href (client) or an HTTP redirect (SSR). Required when redirecting outside the Vue Router.Examples
abortNavigation(err?)
Call abortNavigation to cancel the current navigation without redirecting. Optionally pass an error payload to surface a status code and message.
Optional error payload. Pass an
Error, a plain string (becomes statusMessage), or an object with statusCode and statusMessage.