Plugins
Plugins now have a different format, and take only one argument (nuxtApp).
Plugin Migration Steps
- Migrate your plugins to use the
defineNuxtPluginhelper function - Remove any entries in your
nuxt.configplugins array that are located in yourapp/plugins/folder. All files in this directory at the top level (and any index files in any subdirectories) will be automatically registered. - Instead of setting
modetoclientorserver, you can indicate this in the file name. For example,~/plugins/my-plugin.client.tswill only be loaded on client-side.
Plugin Context
ThenuxtApp object provides:
vueApp- The Vue app instanceprovide()- Provide values to your apphook()- Hook into Nuxt lifecycle$router- Vue Router instance$route- Current routessrContext- Server-side rendering context (server-only)payload- Data payload from server to client
Example: Vue Plugin
Example: Providing a Helper
Plugin Naming Convention
*.client.ts- Client-side only*.server.ts- Server-side only*.ts- Both client and server
Route Middleware
Route middleware has a different format.~/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
- Migrate your route middleware to use the
defineNuxtRouteMiddlewarehelper function - Any global middleware (such as in your
nuxt.config) can be placed in your~/middlewarefolder with a.globalextension, for example~/middleware/auth.global.ts
Middleware Types
Named Middleware
Place middleware in~/middleware/ and reference it by name:
middleware/auth.ts
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 routeabortNavigation(error?)- Cancel navigation with optional erroruseState()- Access shared stateuseCookie()- Access cookiesuseRequestHeaders()- Access request headers (server-only)
Example: Authentication Middleware
middleware/auth.ts
Example: Redirect Middleware
Migration Checklist
Plugins
- Wrap plugins with
defineNuxtPlugin - Replace
inject()withnuxtApp.provide() - Remove plugin entries from
nuxt.config - Rename files with
.clientor.serversuffixes as needed - Update plugin imports to use
useNuxtApp()
Middleware
- Wrap middleware with
defineNuxtRouteMiddleware - Replace
redirect()withnavigateTo() - Replace
contextaccess with appropriate composables - Move global middleware to
.global.tsfiles - Update page middleware usage to use
definePageMeta