Skip to main content

Plugins

Plugins now have a different format, and take only one argument (nuxtApp).

Plugin Migration Steps

  1. Migrate your plugins to use the defineNuxtPlugin helper function
  2. Remove any entries in your nuxt.config plugins array that are located in your app/plugins/ folder. All files in this directory at the top level (and any index files in any subdirectories) will be automatically registered.
  3. Instead of setting mode to client or server, you can indicate this in the file name. For example, ~/plugins/my-plugin.client.ts will only be loaded on client-side.

Plugin Context

The nuxtApp object provides:
  • vueApp - The Vue app instance
  • provide() - Provide values to your app
  • hook() - Hook into Nuxt lifecycle
  • $router - Vue Router instance
  • $route - Current route
  • ssrContext - Server-side rendering context (server-only)
  • payload - Data payload from server to client

Example: Vue Plugin

Example: Providing a Helper

Usage in components:

Plugin Naming Convention

  • *.client.ts - Client-side only
  • *.server.ts - Server-side only
  • *.ts - Both client and server
Example:

Route Middleware

Route middleware has a different format.
Much like Nuxt 2, route middleware placed in your ~/middleware folder is automatically registered. You can then specify it by name in a component. However, this is done with definePageMeta rather than as a component option.

Middleware Migration Steps

  1. Migrate your route middleware to use the defineNuxtRouteMiddleware helper function
  2. Any global middleware (such as in your nuxt.config) can be placed in your ~/middleware folder with a .global extension, for example ~/middleware/auth.global.ts

Middleware Types

Named Middleware

Place middleware in ~/middleware/ and reference it by name:
middleware/auth.ts
Use in pages:

Global Middleware

Add .global suffix to run on every route:
middleware/analytics.global.ts

Inline Middleware

Define middleware directly in your page:

Middleware Helpers

Nuxt 3 provides several helper functions for middleware:
  • navigateTo(route) - Redirect to a route
  • abortNavigation(error?) - Cancel navigation with optional error
  • useState() - Access shared state
  • useCookie() - Access cookies
  • useRequestHeaders() - Access request headers (server-only)

Example: Authentication Middleware

middleware/auth.ts

Example: Redirect Middleware

Migration Checklist

Plugins

  • Wrap plugins with defineNuxtPlugin
  • Replace inject() with nuxtApp.provide()
  • Remove plugin entries from nuxt.config
  • Rename files with .client or .server suffixes as needed
  • Update plugin imports to use useNuxtApp()

Middleware

  • Wrap middleware with defineNuxtRouteMiddleware
  • Replace redirect() with navigateTo()
  • Replace context access with appropriate composables
  • Move global middleware to .global.ts files
  • Update page middleware usage to use definePageMeta