Server Middleware
All Nuxt.js topics∙ Topic
Server Middleware explains Nuxt route guard that runs before navigation resolves with focus terms: server, middleware, reference UD942ED. You will learn the rule, failure mode, verification plan, and production evidence for this Nuxt.js topic.
Syntax
export default defineNuxtRouteMiddleware((to) => { return navigateTo('/login') })
Example
// Topic: Server Middleware
const route = { path: '/admin', signedIn: false };
console.log(route.signedIn ? 'allow' : 'redirect:/login');
// Expected Output: redirect:/login
Best Practices
- 1Use route middleware for redirects and page access decisions, backed by server checks. Use the focus terms (server, middleware, reference UD942ED) to keep this lesson tied to its exact Nuxt.js topic.
- 2Document Nuxt route guard that runs before navigation resolves with focus terms: server, middleware, reference UD942ED in the smallest useful page, layout, composable, store, server route, or deployment step.
- 3Represent every loading, success, denied, stale, and failure state that Server Middleware can expose.
- 4Test direct URL, refresh, anonymous, signed-in, forbidden, and expired states. Include a check for these focus terms: server, middleware, reference UD942ED.
- 5Use correct redirects and blocked access tracked for server, middleware, reference UD942ED to guide improvements.