2
0
mirror of https://github.com/tenrok/vue-meta.git synced 2026-06-23 16:50:34 +03:00

decouple tag names from updated info

This commit is contained in:
Declan de Wet
2016-11-06 03:04:33 +02:00
parent 6b03513576
commit 1f64d1ea7f
+26 -28
View File
@@ -3,16 +3,6 @@ import updateTagAttributes from './updaters/updateTagAttributes'
import updateTags from './updaters/updateTags' import updateTags from './updaters/updateTags'
import { SERVER_RENDERED_ATTRIBUTE } from '../shared/constants' import { SERVER_RENDERED_ATTRIBUTE } from '../shared/constants'
// tags to watch
const tags = [
'meta',
'link',
'base',
'style',
'script',
'noscript'
]
/** /**
* Performs client-side updates when new meta info is received * Performs client-side updates when new meta info is received
* *
@@ -26,30 +16,38 @@ export default function updateClientMetaInfo (newInfo) {
const addedTags = {} const addedTags = {}
const removedTags = {} const removedTags = {}
// update the title Object.keys(newInfo).forEach((key) => {
updateTitle(newInfo.title) switch (key) {
// update the title
// update <html> attrs case 'title':
updateTagAttributes(newInfo.htmlAttrs, htmlTag) updateTitle(newInfo.title)
break
// update <body> attrs // update attributes
updateTagAttributes(newInfo.bodyAttrs, document.getElementsByTagName('body')[0]) case 'htmlAttrs':
case 'bodyAttrs':
// update tags updateTagAttributes(newInfo[key], key === 'htmlAttrs' ? htmlTag : document.getElementsByTagName('body')[0])
for (let i = 0, len = tags.length; i < len; i++) { break
const tag = tags[i] // ignore these
const { oldTags, newTags } = updateTags(tag, newInfo[tag], document.getElementsByTagName('head')[0]) case 'titleChunk':
if (newTags.length) { case 'titleTemplate':
addedTags[tag] = newTags case 'changed':
removedTags[tag] = oldTags break
// catch-all update tags
default:
const { oldTags, newTags } = updateTags(key, newInfo[key], document.getElementsByTagName('head')[0])
if (newTags.length) {
addedTags[key] = newTags
removedTags[key] = oldTags
}
} }
} })
// emit event // emit "event" with new info
if (typeof newInfo.changed === 'function') { if (typeof newInfo.changed === 'function') {
newInfo.changed(newInfo, addedTags, removedTags) newInfo.changed(newInfo, addedTags, removedTags)
} }
} else { } else {
// remove the server render attribute so we can update on changes
htmlTag.removeAttribute(SERVER_RENDERED_ATTRIBUTE) htmlTag.removeAttribute(SERVER_RENDERED_ATTRIBUTE)
} }
} }