2
0
mirror of https://github.com/tenrok/vue-meta.git synced 2026-06-24 16:10:35 +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
+22 -24
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 = {}
Object.keys(newInfo).forEach((key) => {
switch (key) {
// update the title // update the title
case 'title':
updateTitle(newInfo.title) updateTitle(newInfo.title)
break
// update <html> attrs // update attributes
updateTagAttributes(newInfo.htmlAttrs, htmlTag) case 'htmlAttrs':
case 'bodyAttrs':
// update <body> attrs updateTagAttributes(newInfo[key], key === 'htmlAttrs' ? htmlTag : document.getElementsByTagName('body')[0])
updateTagAttributes(newInfo.bodyAttrs, document.getElementsByTagName('body')[0]) break
// ignore these
// update tags case 'titleChunk':
for (let i = 0, len = tags.length; i < len; i++) { case 'titleTemplate':
const tag = tags[i] case 'changed':
const { oldTags, newTags } = updateTags(tag, newInfo[tag], document.getElementsByTagName('head')[0]) break
// catch-all update tags
default:
const { oldTags, newTags } = updateTags(key, newInfo[key], document.getElementsByTagName('head')[0])
if (newTags.length) { if (newTags.length) {
addedTags[tag] = newTags addedTags[key] = newTags
removedTags[tag] = oldTags 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)
} }
} }