mirror of
https://github.com/tenrok/vue-meta.git
synced 2026-06-23 15:20:34 +03:00
add sensible defaults to allow improved server code & add body attribute handling
This commit is contained in:
@@ -11,11 +11,12 @@ export default function generateServerInjector (type, data) {
|
|||||||
switch (type) {
|
switch (type) {
|
||||||
case 'title':
|
case 'title':
|
||||||
return {
|
return {
|
||||||
toString: () => `<${type} ${VUE_META_ATTRIBUTE}="true">${data}</${type}>`
|
text: () => `<${type} ${VUE_META_ATTRIBUTE}="true">${data}</${type}>`
|
||||||
}
|
}
|
||||||
case 'htmlAttrs': {
|
case 'htmlAttrs':
|
||||||
|
case 'bodyAttrs':
|
||||||
return {
|
return {
|
||||||
toString () {
|
text () {
|
||||||
let attributeStr = ''
|
let attributeStr = ''
|
||||||
let watchedAttrs = []
|
let watchedAttrs = []
|
||||||
for (let attr in data) {
|
for (let attr in data) {
|
||||||
@@ -28,6 +29,5 @@ export default function generateServerInjector (type, data) {
|
|||||||
return attributeStr.trim()
|
return attributeStr.trim()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-4
@@ -9,12 +9,21 @@ import generateServerInjector from './generateServerInjector'
|
|||||||
* @return {Object} - server meta info with `toString` methods
|
* @return {Object} - server meta info with `toString` methods
|
||||||
*/
|
*/
|
||||||
export default function inject () {
|
export default function inject () {
|
||||||
const info = getMetaInfo(this.$root)
|
const Vue = this.constructor
|
||||||
const serverMetaInfo = {}
|
|
||||||
|
// 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) {
|
for (let key in info) {
|
||||||
if (info.hasOwnProperty(key) && key !== 'titleTemplate') {
|
if (info.hasOwnProperty(key) && key !== 'titleTemplate') {
|
||||||
serverMetaInfo[key] = generateServerInjector(key, info[key])
|
info[key] = generateServerInjector(key, info[key])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return serverMetaInfo
|
|
||||||
|
return info
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
import updateTitleTag from './updateTitleTag'
|
import updateTitle from './updateTitle'
|
||||||
import updateHtmlTagAttributes from './updateHtmlTagAttributes'
|
import updateTagAttributes from './updateTagAttributes'
|
||||||
import { SERVER_RENDERED_ATTRIBUTE } from './constants'
|
import { SERVER_RENDERED_ATTRIBUTE } from './constants'
|
||||||
|
|
||||||
if (typeof window !== 'undefined' && window !== null) {
|
if (typeof window !== 'undefined' && window !== null) {
|
||||||
var htmlTag = document.getElementsByTagName('html')[0]
|
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) {
|
export default function updateClientMetaInfo (newInfo, $root) {
|
||||||
// if this is not a server render, then update
|
// if this is not a server render, then update
|
||||||
if (htmlTag.getAttribute(SERVER_RENDERED_ATTRIBUTE) === null) {
|
if (htmlTag.getAttribute(SERVER_RENDERED_ATTRIBUTE) === null) {
|
||||||
if (newInfo.title) {
|
// update the title
|
||||||
updateTitleTag(newInfo.title)
|
updateTitle(newInfo.title)
|
||||||
}
|
|
||||||
|
|
||||||
if (newInfo.htmlAttrs) {
|
// update <html> attrs
|
||||||
updateHtmlTagAttributes(newInfo.htmlAttrs, htmlTag)
|
updateTagAttributes(newInfo.htmlAttrs, htmlTag)
|
||||||
}
|
|
||||||
|
// update <body> attrs
|
||||||
|
updateTagAttributes(newInfo.bodyAttrs, bodyTag)
|
||||||
} else {
|
} else {
|
||||||
htmlTag.removeAttribute(SERVER_RENDERED_ATTRIBUTE)
|
htmlTag.removeAttribute(SERVER_RENDERED_ATTRIBUTE)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,9 +4,9 @@ import { VUE_META_ATTRIBUTE } from './constants'
|
|||||||
* updates the document's html tag attributes
|
* updates the document's html tag attributes
|
||||||
*
|
*
|
||||||
* @param {Object} attrs - the new document html 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 vueMetaAttrString = tag.getAttribute(VUE_META_ATTRIBUTE)
|
||||||
const vueMetaAttrs = vueMetaAttrString ? vueMetaAttrString.split(',') : []
|
const vueMetaAttrs = vueMetaAttrString ? vueMetaAttrString.split(',') : []
|
||||||
const toRemove = [].concat(vueMetaAttrs)
|
const toRemove = [].concat(vueMetaAttrs)
|
||||||
@@ -3,6 +3,6 @@
|
|||||||
*
|
*
|
||||||
* @param {String} title - the new title of the document
|
* @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
|
document.title = title
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user