Nuxt & Universal SSR (50 Qs)•Part 1: Nuxt 3 Fundamentals (Junior / Entry-Level)
How do Nuxt 3 Plugins work and how do you restrict execution?
mid LevelCode ExampleVue & NuxtHTTP & REST APIs
In Simple Words:
Plugins in the plugins/ directory run before initializing the Vue application. They are used to register global directives, third-party libraries, or global helpers using defineNuxtPlugin.
File Naming Conventions for Environment Restriction:
plugins/analytics.client.ts-> Runs only on the client/browser.plugins/audit-logger.server.ts-> Runs only on the server.plugins/i18n.ts-> Universal (runs on both client and server).
typescript
// plugins/toast.client.ts
export default defineNuxtPlugin((nuxtApp) => {
return {
provide: {
toast: (msg: string) => {
alert(msg);
}
}
};
});
// Usage anywhere in app: const { $toast } = useNuxtApp();