Nuxt plugins pattern

From Intense-Visions/harness-engineering · View interactive version

Nuxt plugins pattern is an AI agent skill from the GitHub repository Intense-Visions/harness-engineering. > Extend the Nuxt application instance, register global services, and provide values to composables using defineNuxtPlugin

How to use this skill

Skill file

Nuxt Plugins Pattern

Extend the Nuxt application instance, register global services, and provide values to composables using defineNuxtPlugin

When to Use

Instructions

  1. Create files in plugins/ — they are automatically registered. Files are named without registration in nuxt.config.ts:
// plugins/my-plugin.ts
export default defineNuxtPlugin((nuxtApp) => {
  // runs on both server and client
});
  1. Register Vue plugins by calling nuxtApp.vueApp.use():
// plugins/vue-query.ts
import { VueQueryPlugin, QueryClient } from '@tanstack/vue-query';

export default defineNuxtPlugin((nuxtApp) => {
  const queryClient = new QueryClient();
  nuxtApp.vueApp.use(VueQueryPlugin, { queryClient });
});
  1. Provide values accessible throughout the app via useNuxtApp().$<name>:
// plugins/analytics.ts
export default defineNuxtPlugin(() => {
  const analytics = createAnalyticsClient();
  return {
    provide: {
      analytics,
    },
  };
});

// In any composable or component:
const { $analytics } = useNuxtApp();
$analytics.track('page_view');
  1. Augment TypeScript types for provided values:
// types/nuxt.d.ts
declare module '#app' {
  interface NuxtApp {
    $analytics: AnalyticsClient;
  }
}
declare module 'vue' {
  interface ComponentCustomProperties {
    $analytics: AnalyticsClient;
  }
}
export {};
  1. Restrict execution to client-only by naming the file .client.ts:
// plugins/crisp-chat.client.ts
export default defineNuxtPlugin(() => {
  // runs only in the browser
  window.$crisp = [];
  window.CRISP_WEBSITE_ID = 'your-id';
});
  1. Restrict execution to server-only by naming the file .server.ts:
// plugins/db-connection.server.ts
export default defineNuxtPlugin(async () => {
  await connectDatabase();
});
  1. Control plugin execution order using numeric prefixes or the order property:
// plugins/01.init.ts   — runs first
// plugins/02.auth.ts   — runs second

// Or use the order property (lower = earlier):
export default defineNuxtPlugin({
  name: 'auth',
  enforce: 'pre', // or 'post'
  setup(nuxtApp) {
    /* ... */
  },
});
  1. Access other plugins or Nuxt context within a plugin via the nuxtApp argument:
export default defineNuxtPlugin((nuxtApp) => {
  nuxtApp.hook('app:mounted', () => {
    console.log('App is mounted');
  });
});

Details

Plugin vs. composable vs. module:

Use a plugin when you need one-time initialization. Use a composable when you need reactive per-call state.

Async plugins:

Plugins can be async. Nuxt awaits them before rendering the app (blocking). Use sparingly for critical initialization; defer non-critical work to nuxtApp.hook('app:mounted', ...):

export default defineNuxtPlugin(async (nuxtApp) => {
  await criticalInit(); // blocks SSR render
  nuxtApp.hook('app:mounted', () => {
    nonCriticalInit(); // deferred to client mount
  });
});

Accessing the request event in server plugins:

export default defineNuxtPlugin((nuxtApp) => {
  const event = useRequestEvent(); // only valid server-side
  const userAgent = getHeader(event, 'user-agent');
});

Plugin execution order with enforce:

When NOT to use:

Source

https://nuxt.com/docs/guide/directory-structure/plugins

Process

  1. Read the instructions and examples in this document.
  2. Apply the patterns to your implementation, adapting to your specific context.
  3. Verify your implementation against the details and edge cases listed above.

Harness Integration

Success Criteria

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.