definePageMeta
definePageMeta
is a compiler macro that you can use to set metadata for your page components located in the pages/
directory (unless set otherwise). This way you can set custom metadata for each static or dynamic route of your Nuxt application.
<script setup> definePageMeta({ title: 'Articles' })</script>
definePageMeta(meta: PageMeta) => voidinterface PageMeta { validate?: (route: RouteLocationNormalized) => boolean | Promise<boolean> | Partial<NuxtError> | Promise<Partial<NuxtError>> redirect?: RouteRecordRedirectOption alias?: string | string[] pageTransition?: boolean | TransitionProps layoutTransition?: boolean | TransitionProps key?: false | string | ((route: RouteLocationNormalizedLoaded) => string) keepalive?: boolean | KeepAliveProps layout?: false | LayoutKey | Ref<LayoutKey> | ComputedRef<LayoutKey> middleware?: MiddlewareKey | NavigationGuard | Array<MiddlewareKey | NavigationGuard> [key: string]: any}
meta
PageMeta
pageTransition
boolean
| TransitionProps
false
to disable the page transition.layoutTransition
boolean
| TransitionProps
false
to disable the layout transition.key
false
| string
| ((route: RouteLocationNormalizedLoaded) => string)
key
value when you need more control over when the <NuxtPage>
component is re-rendered.keepalive
boolean
| KeepAliveProps
true
when you want to preserve page state across route changes or use the KeepAliveProps
for a fine-grained control.layout
false
| LayoutKey
| Ref<LayoutKey>
| ComputedRef<LayoutKey>
false
in case the default layout needs to be disabled.middleware
MiddlewareKey
| NavigationGuard
| Array<MiddlewareKey | NavigationGuard>
definePageMeta
. Learn more about route middleware.validate
(route: RouteLocationNormalized) => boolean | Promise<boolean> | Partial<NuxtError> | Promise<Partial<NuxtError>>
statusCode
/statusMessage
to respond immediately with an error (other matches will not be checked).[key: string]
any
meta
object.The example below demonstrates:
key
can be a function that returns a value;keepalive
property makes sure that the <modal>
component is not cached when switching between multiple components;pageType
as a custom property:<script setup> definePageMeta({ key: (route) => route.fullPath, keepalive: { exclude: ['modal'] }, pageType: 'Checkout' })</script>
The example below shows how the middleware can be defined using a function
directly within the definePageMeta
or set as a string
that matches the middleware file name located in the middleware/
directory:
<script setup> definePageMeta({ // define middleware as a function middleware: [ function (to, from) { const auth = useState('auth') if (!auth.value.authenticated) { return navigateTo('/login') } return navigateTo('/checkout') } ], // ... or a string middleware: 'auth' // ... or multiple strings middleware: ['auth', 'another-named-middleware']})</script>
You can define the layout that matches the layout's file name located (by default) in the layouts/
directory. You can also disable the layout by setting the layout
to false
:
<script setup> definePageMeta({ // set custom layout layout: 'admin' // ... or disable a default layout layout: false })</script>