metaInfo properties
Note
The documentation below uses metaInfo
as keyName
in the examples, please note that this is configurable and could be different in your case
title
- type
string
Maps to the inner-text value of the<title>
element.
{
metaInfo: {
title: 'Foo Bar'
}
}
<title>Foo Bar</title>
titleTemplate
- type
string | Function
The value oftitle
will be injected into the%s
placeholder intitleTemplate
before being rendered. The original title will be available onmetaInfo.titleChunk
.
{
metaInfo: {
title: 'Foo Bar',
titleTemplate: '%s - Baz'
}
}
<title>Foo Bar - Baz</title>
The property can also be a function:
titleTemplate: (titleChunk) => {
// If undefined or blank then we don't need the hyphen
return titleChunk ? `${titleChunk} - Site Title` : 'Site Title';
}
htmlAttrs
headAttrs
bodyAttrs
- type
object
Each key:value maps to the equivalent attribute:value of the<body>
element.
Since v2.0
value can also be an Array<string>
{
metaInfo: {
htmlAttrs: {
lang: 'en',
amp: true
},
bodyAttrs: {
class: ['dark-mode', 'mobile']
}
}
}
<html lang="en" amp>
<body class="dark-mode mobile">Foo Bar</body>
base
- type
object
Maps to a newly-created<base>
element, where object properties map to attributes.
{
metaInfo: {
base: { target: '_blank', href: '/' }
}
}
<base target="_blank" href="/">
meta
- type
collection
Each item in the array maps to a newly-created<meta>
element, where object properties map to attributes.
{
metaInfo: {
meta: [
{ charset: 'utf-8' },
{ name: 'viewport', content: 'width=device-width, initial-scale=1' }
]
}
}
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
Content templates
Since v1.5.0
, you can now set up meta templates that work similar to the titleTemplate:
{
metaInfo: {
meta: [
{ charset: 'utf-8' },
{
property: 'og:title',
content: 'Test title',
// following template options are identical
// template: '%s - My page',
template: chunk => `${chunk} - My page`,
vmid: 'og:title'
}
]
}
}
<meta charset="utf-8">
<meta name="og:title" property="og:title" content="Test title - My page">
link
- type
collection
Each item in the array maps to a newly-created<link>
element, where object properties map to attributes.
{
metaInfo: {
link: [
{ rel: 'stylesheet', href: '/css/index.css' },
{ rel: 'favicon', href: 'favicon.ico' }
]
}
}
<link rel="stylesheet" href="/css/index.css">
<link rel="favicon" href="favicon.ico">
style
- type
object
Each item in the array maps to a newly-created<style>
element, where object properties map to attributes.
{
metaInfo: {
style: [
{ cssText: '.foo { color: red }', type: 'text/css' }
]
}
}
<style type="text/css">.foo { color: red }</style>
script
- type
collection
Each item in the array maps to a newly-created<script>
element, where object properties map to attributes.
{
metaInfo: {
script: [
{ src: 'https://cdn.jsdelivr.net/npm/vue/dist/vue.js', async: true, defer: true }
],
}
}
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js" async defer></script>
Add JSON data (since v2.1)
If you wish to use a JSON variable within a script tag (e.g. for JSON-LD), you can directly pass your variable by using the json
property.
When passing an array or object to the json
property the keys and values of the variable will still be sanitized to prevent XSS
{
metaInfo: {
script: [{
type: 'application/ld+json'
json: {
'@context': 'http://schema.org',
unsafe: '<p>hello</p>'
}
}]
}
}
<script type="application/ld+json">
{ "@context": "http://schema.org", "unsafe": "<p>hello</p>" }
</script>
Add other raw data
You have to disable sanitizers so the content of innerHTML
won't be escaped. Please see __dangerouslyDisableSanitizersByTagID for more info on related risks
{
metaInfo: {
script: [{
vmid: 'ldjson-schema',
innerHTML: '{ "@context": "http://schema.org" }',
type: 'application/ld+json'
}],
__dangerouslyDisableSanitizersByTagID: {
'ldjson-schema': ['innerHTML']
},
}
}
<script type="application/ld+json">{ "@context": "http://schema.org" }</script>
noscript
- type
collection
Each item in the array maps to a newly-created<noscript>
element, where object properties map to attributes.
{
metaInfo: {
noscript: [
{ innerHTML: 'This website requires JavaScript.' }
]
}
}
<noscript>This website requires JavaScript.</noscript>
__dangerouslyDisableSanitizers
- type
Array<string>
If you need to disable sanitation, please always use __dangerouslyDisableSanitizersByTagID when possible
By disabling sanitization, you are opening potential vectors for attacks such as SQL injection & Cross-Site Scripting (XSS). Be very careful to not compromise your application.
By default, vue-meta
sanitizes HTML entities in every property. You can disable this behaviour on a per-property basis using __dangerouslyDisableSantizers
. Just pass it a list of properties you want sanitization to be disabled on:
{
metaInfo: {
title: '<I will be sanitized>',
meta: [{
vmid: 'description',
name: 'description',
content: '& I will not be <sanitized>'
}],
__dangerouslyDisableSanitizers: ['meta']
}
}
<title><I will be sanitized></title>
<meta vmid="description" name="description" content="& I will not be <sanitized>">
__dangerouslyDisableSanitizersByTagID
- type
object
By disabling sanitation, you are opening potential vectors for attacks such as SQL injection & Cross-Site Scripting (XSS). Be very careful to not compromise your application.
Provides same functionality as __dangerouslyDisableSanitizers
but you can specify which property for which tagIDKeyName
sanitation should be disabled. It expects an object with the vmid as key and an array with property keys as value:
{
metaInfo: {
title: '<I will be sanitized>',
meta: [{
vmid: 'description',
name: 'still-&-sanitized',
content: '& I will not be <sanitized>'
}],
__dangerouslyDisableSanitizersByTagID: {
description: ['content']
}
}
}
<title><I will be sanitized></title>
<meta vmid="description" name="still-&-sanitized" content="& I will not be <sanitized>">
changed
- type
Function
A callback function which is called whenever themetaInfo
updates / changes.
The callback receives the following arguments:
- newInfo
- type
object
The updatedmetaInfo
object
- type
- addedTags
- type
Array<HTMLElement>
List of elements that were added
- type
- removedTags
- type
Array<HTMLElement>
List of elements that were removed
- type
{
metaInfo: {
changed (newInfo, addedTags, removedTags) {
console.log('Metadata was updated!')
}
}
}
afterNavigation
- type
Function
A callback function which is called whenvue-meta
has updated the metadata after navigation occurred.This can be used to track page views with the updated document title etc.
Adding a afterNavigation
callback behaves the same as when refreshOnceOnNavigation is true
The callback receives the following arguments:
- newInfo
- type
object
The updatedmetaInfo
object
- type
{
metaInfo: {
afterNavigation(metaInfo) {
trackPageView(document.title)
// is the same as
trackPageView(metaInfo.title)
}
}
}