diff --git a/src/generateServerInjector.js b/src/generateServerInjector.js index 49aaa41..e3545b5 100644 --- a/src/generateServerInjector.js +++ b/src/generateServerInjector.js @@ -11,11 +11,12 @@ export default function generateServerInjector (type, data) { switch (type) { case 'title': return { - toString: () => `<${type} ${VUE_META_ATTRIBUTE}="true">${data}` + text: () => `<${type} ${VUE_META_ATTRIBUTE}="true">${data}` } - case 'htmlAttrs': { + case 'htmlAttrs': + case 'bodyAttrs': return { - toString () { + text () { let attributeStr = '' let watchedAttrs = [] for (let attr in data) { @@ -28,6 +29,5 @@ export default function generateServerInjector (type, data) { return attributeStr.trim() } } - } } } diff --git a/src/inject.js b/src/inject.js index a95e465..9e7f0e0 100644 --- a/src/inject.js +++ b/src/inject.js @@ -9,12 +9,21 @@ import generateServerInjector from './generateServerInjector' * @return {Object} - server meta info with `toString` methods */ export default function inject () { - const info = getMetaInfo(this.$root) - const serverMetaInfo = {} + const Vue = this.constructor + + // get meta info with sensible defaults + const info = Vue.util.extend({ + title: '', + htmlAttrs: {}, + bodyAttrs: {} + }, getMetaInfo(this.$root)) + + // generate server injectors for (let key in info) { if (info.hasOwnProperty(key) && key !== 'titleTemplate') { - serverMetaInfo[key] = generateServerInjector(key, info[key]) + info[key] = generateServerInjector(key, info[key]) } } - return serverMetaInfo + + return info } diff --git a/src/updateClientMetaInfo.js b/src/updateClientMetaInfo.js index 3c23e44..efdee2a 100644 --- a/src/updateClientMetaInfo.js +++ b/src/updateClientMetaInfo.js @@ -1,9 +1,10 @@ -import updateTitleTag from './updateTitleTag' -import updateHtmlTagAttributes from './updateHtmlTagAttributes' +import updateTitle from './updateTitle' +import updateTagAttributes from './updateTagAttributes' import { SERVER_RENDERED_ATTRIBUTE } from './constants' if (typeof window !== 'undefined' && window !== null) { var htmlTag = document.getElementsByTagName('html')[0] + var bodyTag = document.getElementsByTagName('body')[0] } /** @@ -14,13 +15,14 @@ if (typeof window !== 'undefined' && window !== null) { export default function updateClientMetaInfo (newInfo, $root) { // if this is not a server render, then update if (htmlTag.getAttribute(SERVER_RENDERED_ATTRIBUTE) === null) { - if (newInfo.title) { - updateTitleTag(newInfo.title) - } + // update the title + updateTitle(newInfo.title) - if (newInfo.htmlAttrs) { - updateHtmlTagAttributes(newInfo.htmlAttrs, htmlTag) - } + // update attrs + updateTagAttributes(newInfo.htmlAttrs, htmlTag) + + // update attrs + updateTagAttributes(newInfo.bodyAttrs, bodyTag) } else { htmlTag.removeAttribute(SERVER_RENDERED_ATTRIBUTE) } diff --git a/src/updateHtmlTagAttributes.js b/src/updateTagAttributes.js similarity index 85% rename from src/updateHtmlTagAttributes.js rename to src/updateTagAttributes.js index d5993be..728da8b 100644 --- a/src/updateHtmlTagAttributes.js +++ b/src/updateTagAttributes.js @@ -4,9 +4,9 @@ import { VUE_META_ATTRIBUTE } from './constants' * updates the document's html tag attributes * * @param {Object} attrs - the new document html attributes - * @param {HTMLElement} [tag=] - the tag + * @param {HTMLElement} tag - the HTMLElment tag to update with new attrs */ -export default function updateHtmlTagAttributes (attrs, tag = document.getElementsByTagName('html')[0]) { +export default function updateTagAttributes (attrs, tag) { const vueMetaAttrString = tag.getAttribute(VUE_META_ATTRIBUTE) const vueMetaAttrs = vueMetaAttrString ? vueMetaAttrString.split(',') : [] const toRemove = [].concat(vueMetaAttrs) diff --git a/src/updateTitleTag.js b/src/updateTitle.js similarity index 65% rename from src/updateTitleTag.js rename to src/updateTitle.js index f4fc9ed..2797754 100644 --- a/src/updateTitleTag.js +++ b/src/updateTitle.js @@ -3,6 +3,6 @@ * * @param {String} title - the new title of the document */ -export default function updateTitleTag (title = document.title) { +export default function updateTitle (title = document.title) { document.title = title }