--- sidebar: auto --- # API Reference ## Plugin properties :::tip VueMeta exports a Vue plugin as default export. This section describes the properties of that default export ::: ### version - type `string` The version of the plugin, it is the same as the package version ### install The method used by Vue to install the plugin ### hasMetaInfo - argument: - vm (a Vue component) - returns `boolean` A helper function which returns true when the Vue component passed as argument has metaInfo defined ### generate ::: warning This method is not available in the browser builds ::: - arguments: - metaInfo - options (optional) - returns `metaInfo` This method is similar to [`$meta.inject()`](/api/#meta-inject) but works without the need for a Vue instance ```js import VueMeta from 'vue-meta' const { generate } = VueMeta const rawMetaInfo = { meta: [{ charset: 'utf-8' }] } const metaInfo = generate(rawMetaInfo /*, yourOptions*/) const HEAD = metaInfo.script.text() + metaInfo.meta.text() ``` will result In ```html ``` ## Plugin options ### keyName - type `string` - default `metaInfo` The name of the component option that contains all the information that gets converted to the various meta tags & attributes for the page ### attribute - type `string` - default `data-vue-meta` The name of the attribute vue-meta arguments on elements to know which it should manage and which it should ignore. ### ssrAttribute - type `string` - default `data-vue-meta-server-rendered` The name of the attribute that is added to the `html` tag to inform `vue-meta` that the server has already generated the meta tags for the initial render See [How to prevent update on page load](/faq/prevent-initial) ### ssrAppId - type `string` - default `ssr` The app id for a server side rendered app. You shouldnt have to change this normally ### tagIDKeyName - type `string` - default `vmid` The property that tells `vue-meta` to overwrite (instead of append) an item in a tag list. For example, if you have two `meta` tag list items that both have `vmid` of 'description', then vue-meta will overwrite the shallowest one with the deepest one. ### contentKeyName - type `string` - default `content` The key name for the content-holding property ### metaTemplateKeyName - type `string` - default `template` The key name for possible meta templates ### refreshOnceOnNavigation - type `boolean` - default `false` When `true` then `vue-meta` will pause updates once page navigation starts and resumes updates when navigation finishes (resuming also triggers an update). This could both be a performance improvement as a possible fix for 'flickering' when you are e.g. replacing stylesheets :::tip Its not supported to disable `refreshOnceOnNavigation` once enabled ::: ### debounceWait - type `number` - default `10` A timeout is used to debounce updates so vue-meta won't be updating the meta info immediately, this option determines how long updates are debounced ### waitOnDestroyed - type `boolean` - default `true` Once a component is destroyed, vue-meta will update the meta information to make sure any info added by the destroyed component is removed. To support transitions, vue-meta sets an interval to wait until the DOM element has been removed before it requests a meta info update. If you set this option to `false`, the meta info update will be immediately called after `nextTick` instead ## Plugin methods The `vue-meta` plugin injects a `$meta()` function in the Vue prototype which provides the following methods :::tip Note `$meta()` is a function so we only need to insert it once in the `Vue.prototype`, but still use `this` to reference the component it was called from ::: ### $meta().getOptions - returns [`pluginOptions`](/api/#plugin-options) Could be used by third-party libraries who wish to interact with `vue-meta` ### $meta().setOptions - arguments: - options (type `object`) - returns [`pluginOptions`](/api/#plugin-options) You can toggle some plugin options during runtime by calling this method. Only [plugin options](/api/#plugin-options) marked can be changed ```js vm.$meta().setOptions({ debounceWait: 50 }) ``` ### $meta().addApp - arguments: - appName (type: `string`) - returns app object `{ set, remove }` Originally `vue-meta` only supported adding meta info from Vue components. This caused some issues for third party integrations as they have to add their meta info through eg the root component while that already could contain meta info. To improve this third party integrations can use `addApp` to add their meta information. Example of adding additional meta info for eg a third party plugin: ```js const { set, remove } = vm.$meta().addApp('custom') set({ meta: [{ charset: 'utf=8' }] }) setTimeout(() => remove(), 3000) ``` There is no reactivity for custom apps. The integrator need to take care of that and call `set` and `remove` when appropriate. If you call `addApp.set` on the client before the app is mounted, the tags will be processed on the first refresh. If you call set when the app is mounted they tags are immediately processed. The function is called addApp because the added metaInfo is treated exactly the same as when there are multiple apps on one page. Eg the tags that will be added will also list the `appId` you specifiy: ```html ``` ### $meta().refresh - returns [`metaInfo`](/api/#metaInfo-properties) Updates the current metadata with new metadata. Useful when updating metadata as the result of an asynchronous action that resolves after the initial render takes place. ### $meta().inject - arguments - injectOptions (type: `object`) - returns [`metaInfo`](/api/#metaInfo-properties) :::tip SSR only `inject` is available in the server plugin only and is not available on the client ::: You can pass an object to inject with global inject options. See [SSR injection method arguments](/api/#noscript-text) for a list of available options. It returns a special `metaInfo` object where all keys have an object as value which contains a `text()` method for returning html code See [Rendering with renderToString](/guide/ssr.html#simple-rendering-with-rendertostring) for an example #### Passing arguments to `text()` In some cases you can pass an argument to the text method. E.g. to [automatically add the ssrAttribute on ssr](/faq/prevent-initial.html) or [render properties in the body](/api/#ssr-support) ### $meta().pause - arguments: - refresh (type `boolean`, default `false`) - returns `resume()` Pauses global metadata updates until either the returned resume method is called or [resume](/api/#meta-resume) ### $meta().resume - arguments: - refresh (type `boolean`, default `false`) - returns [`metaInfo`](/api/#metaInfo-properties) (optional) Resumes metadata updates after they have been paused. If `refresh` is `true` it immediately initiates a metadata update by calling [refresh](/api/#meta-refresh) ## metaInfo properties ::: tip Note The documentation below uses `metaInfo` as `keyName` in the examples, please note that this is [configurable](/api/#keyname) and could be different in your case ::: ### title - type `string` Maps to the inner-text value of the `` element. ```js { metaInfo: { title: 'Foo Bar' } } ``` ```html <title>Foo Bar ``` ### titleTemplate - type `string | Function` The value of `title` will be injected into the `%s` placeholder in `titleTemplate` before being rendered. The original title will be available on `metaInfo.titleChunk`. ```js { metaInfo: { title: 'Foo Bar', titleTemplate: '%s - Baz' } } ``` ```html Foo Bar - Baz ``` The property can also be a function: ```js 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 `` element. Since `v2.0` value can also be an `Array` ```js { metaInfo: { htmlAttrs: { lang: 'en', amp: true }, bodyAttrs: { class: ['dark-mode', 'mobile'] } } } ``` ```html Foo Bar ``` ### base - type `object` Maps to a newly-created `` element, where object properties map to attributes. ```js { metaInfo: { base: { target: '_blank', href: '/' } } } ``` ```html ``` ### meta - type `collection` Each item in the array maps to a newly-created `` element, where object properties map to attributes. ```js { metaInfo: { meta: [ { charset: 'utf-8' }, { name: 'viewport', content: 'width=device-width, initial-scale=1' } ] } } ``` ```html ``` #### Content templates Since `v1.5.0`, you can now set up meta templates that work similar to the titleTemplate: ```js { 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' } ] } } ``` ```html ``` ### link - type `collection` Each item in the array maps to a newly-created `` element, where object properties map to attributes. ```js { metaInfo: { link: [ { rel: 'stylesheet', href: '/css/index.css' }, { rel: 'favicon', href: 'favicon.ico' } ] } } ``` ```html ``` ### style - type `object` Each item in the array maps to a newly-created ` ``` ### script - type `collection` Each item in the array maps to a newly-created ` ``` #### Add JSON data 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. ::: tip When passing an array or object to the `json` property the keys and values of the variable will still be sanitized to prevent XSS ::: ```js { metaInfo: { script: [{ type: 'application/ld+json', json: { '@context': 'http://schema.org', unsafe: '

hello

' } }] } } ``` ```html ``` #### Add other raw data ::: warning You have to disable sanitizers so the content of `innerHTML` won't be escaped. Please see [__dangerouslyDisableSanitizersByTagID](/api/#dangerouslydisablesanitizersbytagid) for more info on related risks ::: ```js { metaInfo: { script: [{ vmid: 'ldjson-schema', innerHTML: '{ "@context": "http://schema.org" }', type: 'application/ld+json' }], __dangerouslyDisableSanitizersByTagID: { 'ldjson-schema': ['innerHTML'] }, } } ``` ```html ``` ### noscript - type `collection` Each item in the array maps to a newly-created `