Your suggestion is correct: You should not call useRoute inside a Nuxt plugin's setup function. The official Nuxt documentation explicitly states that useRoute should only be used in the setup function of a Vue component or in a Nuxt plugin, but with an important caveat—using it in a plugin is only valid if the plugin is executed in a component context (e.g., when providing helpers that will be used in components). In most cases, calling useRoute directly in a plugin's setup (especially at the top level) is not recommended and can lead to unexpected behavior, because there is no concept of a "current route" in the plugin context during app initialization.
From the docs:
The useRoute() composable should only be used in the setup function of a Vue component or in a Nuxt plugin.
This applies to any composable that uses useRoute() internally too.
However, the context here is important: if your plugin is running outside of a component context (such as during app initialization), useRoute will not work as expected. Instead, you should use the router instance (from useRouter or nuxtApp.$router) to access route information in a way that is safe for both server and client contexts.
Example from the docs:
Using useRoute in middleware is not recommended because it can lead to unexpected behavior. There is no concept of a "current route" in middleware. The useRoute() composable should only be used in the setup function of a Vue component or in a Nuxt plugin.
But the same applies to plugins that are not running in a component context. So your code:
export default defineNuxtPlugin({
name: 'plugin',
parallel: true,
setup: async () => {
const route = useRoute();
if (route.name === 'my-route') {
doSomething()
}
},
});