A document manager for Vue.js. Think react-helmet but for Vue.js.
Features:
- lightweight (1kb before gzipping);
- user-defined injection logic;
- server-side rendering support;
- reactive.
Installation
Get it directly from NPM:
npm i --save-dev vue-document
or through unpkg:
<script src="https://unpkg.com/vue-document></script>
(in this case vue-document will be available as window.VueDocument
).
Usage
NOTE: Any values specified in children components get automatically merged in
(overriding existing entries (in a "deep merge" fashion)).
Client-Side
<template> <div id="app">{{ message }}</div></template> <script> import Vue from 'vue' import VueDocument from 'vue-document' Vue.use(VueDocument, { injector (document) { // "this" is referencing Vue component which caused "document" metadata to change const metadata = this.$root.$document document.title = metadata.head.title document.querySelector('meta[name="description"]').content = metadata.meta.find((meta) => meta.name === 'description').content } }) export default { document: { head: { title: 'Custom Title', meta: [ {name: 'description', content: 'Custom Description'} ] } }, el: '#app', data: { message: 'Hello Vue!' } }</script>
In the example above, instead of writing injector
ourselves we could use vue-document-injector-title.js and
vue-document-injector-meta.js (later is used for custom <meta/>
injection, not just description).
injector
handler could then be simplified to
Vue
Server-Side
The example below is not using bundleRenderer just to keep things simple.
There is nothing stopping you from doing that.
const Vue = const VueDocument = Vue const vm = document: head: title: 'Custom Title' meta: name: 'description' content: 'Custom Description' { return } el: '#app' data: message: 'Hello Vue!' const renderer = renderer
Another option is to inject vm.$document
into the existing html (which can be helpful
if page is generated by html-webpack-plugin or something similar):
const Vue = const VueDocument = const titleInjector metaInjector = VueDocument Vue const window = const document = windowdocument const vm = ...vm console