In Nuxt, global route middleware registered with addRouteMiddleware (with { global: true }) is designed to run on every route change, not just on the initial page load. Your code is correct for registering a global middleware:
export default defineNuxtPlugin(() => {
addRouteMiddleware('auth', () => {
console.log('This global middleware is added in the plugin and will run on every routing change')
}, { global: true })
})
If you observe that the middleware only runs on the first page load and not on subsequent route changes, this is not the expected behavior according to the official documentation. The middleware should execute on every route navigation, both on the initial load and on client-side navigations via <NuxtLink> or programmatic navigation. This is confirmed in the docs, which state:
addRouteMiddleware() is a helper function to dynamically add middleware in your application. ... Global route middleware can be defined ... and will be run on every route change.
addRouteMiddleware documentation
If your middleware is not running on every route change, possible reasons could include:
- The navigation is a full page reload (not a client-side navigation).
- There is an error in the plugin registration or middleware logic.
- There is a caching or hydration issue.
You may want to check that:
- You are navigating using
<NuxtLink> or programmatic navigation, not full page reloads.
- The plugin is properly loaded and not conditionally excluded.
- There are no errors in the browser console that might prevent the middleware from running.
If the issue persists and you are following the documented approach, it may be a bug or an environment-specific issue. In that case, consider reporting it or checking for related issues in the Nuxt repository.