4.1 KiB
navigation.icon | description |
---|---|
uil:file-search-alt | Nuxt provides good default values for meta tags, but you can override these if you need to. |
SEO and Meta
Out-of-the-box, Nuxt provides good default values for charset
and viewport
meta tags, but you can override these if you need to, as well as customize other meta tags for your site in several different ways.
:ReadMore{link="/api/configuration/nuxt-config#head"}
useHead
Composable
Within your setup
function, you can call useHead
with an object of meta properties with keys corresponding to meta tags: title
, titleTemplate
, base
, script
, noscript
, style
, meta
and link
, as well as htmlAttrs
and bodyAttrs
. There are also two shorthand properties, charset
and viewport
, which set the corresponding meta tags. Alternatively, you can pass a function returning the object for reactive metadata.
For example:
<script setup>
useHead({
title: 'My App',
// or, instead:
// titleTemplate: (title) => `My App - ${title}`,
viewport: 'width=device-width, initial-scale=1, maximum-scale=1',
charset: 'utf-8',
meta: [
{ name: 'description', content: 'My amazing site.' }
],
bodyAttrs: {
class: 'test'
}
})
</script>
::ReadMore{link="/api/composables/use-head"} ::
Title Templates
You can use the titleTemplate
option to provide a dynamic template for customizing the title of your site, for example, by adding the name of your site to the title of every page.
The titleTemplate
can either be a string, where %s
is replaced with the title, or a function. If you want to use a function (for full control), then this cannot be set in your nuxt.config
, and it is recommended instead to set it within your app.vue
file, where it will apply to all pages on your site:
<script setup>
useHead({
titleTemplate: (titleChunk) => {
return titleChunk ? `${titleChunk} - Site Title` : 'Site Title';
}
})
</script>
Now, if you set the title to My Page
with useHead
on another page of your site, the title would appear as 'My Page - Site Title' in the browser tab. You could also pass null
to default to the site title.
Body Meta Tags
You can use the body: true
option on the link
and script
meta tags to append them to the end of the <body>
tag.
For example:
<script setup>
useHead({
script: [
{
src: 'https://third-party-script.com',
body: true
}
]
})
</script>
Meta Components
Nuxt provides <Title>
, <Base>
, <Script>
, <NoScript>
, <Style>
, <Meta>
, <Link>
, <Body>
, <Html>
and <Head>
components so that you can interact directly with your metadata within your component's template.
Because these component names match native HTML elements, it is very important that they are capitalized in the template.
<Head>
and <Body>
can accept nested meta tags (for aesthetic reasons) but this has no effect on where the nested meta tags are rendered in the final HTML.
For example:
<script setup>
const title = ref('Hello World')
</script>
<template>
<div>
<Head>
<Title>{{ title }}</Title>
<Meta name="description" :content="title" />
<Style type="text/css" children="body { background-color: green; }" />
</Head>
<h1>{{ title }}</h1>
</div>
</template>
Example: Usage With definePageMeta
Within your pages/
directory, you can use definePageMeta
along with useHead
to set metadata based on the current route.
For example, you can first set the current page title (this is extracted at build time via a macro, so it can't be set dynamically):
<script setup>
definePageMeta({
title: 'Some Page'
})
</script>
And then in your layout file, you might use the route's metadata you have previously set:
<script setup>
const route = useRoute()
useHead({
meta: [{ name: 'og:title', content: `App Name - ${route.meta.title}` }]
})
</script>
:LinkExample{link="/examples/composables/use-head"}
:ReadMore{link="/guide/directory-structure/pages/#page-metadata"}