2
0
mirror of https://github.com/tenrok/vue-meta.git synced 2026-06-08 14:22:25 +03:00

add sensible defaults to allow improved server code & add body attribute handling

This commit is contained in:
Declan de Wet
2016-11-02 19:36:25 +02:00
parent 4f4f65a11b
commit 8c7934ba34
5 changed files with 30 additions and 19 deletions
+4 -4
View File
@@ -11,11 +11,12 @@ export default function generateServerInjector (type, data) {
switch (type) {
case 'title':
return {
toString: () => `<${type} ${VUE_META_ATTRIBUTE}="true">${data}</${type}>`
text: () => `<${type} ${VUE_META_ATTRIBUTE}="true">${data}</${type}>`
}
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()
}
}
}
}
}
+13 -4
View File
@@ -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
}
+10 -8
View File
@@ -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 <html> attrs
updateTagAttributes(newInfo.htmlAttrs, htmlTag)
// update <body> attrs
updateTagAttributes(newInfo.bodyAttrs, bodyTag)
} else {
htmlTag.removeAttribute(SERVER_RENDERED_ATTRIBUTE)
}
@@ -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=<html/>] - the <html> 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)
+1 -1
View File
@@ -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
}