mirror of
https://github.com/tenrok/vue-meta.git
synced 2026-06-12 06:22:26 +03:00
024e7c5a62
* feat: add an appId to tags to support multiple apps * feat: show warning on calling () on non-vuemeta components * feat: always use appId ssr for server-generated apps * test: update tests for appId * chore: update circleci to only run audit for dependencies * fix: dont set data-vue-meta attribute on title it has no use on the client as we use document.title there. Which also means the appId listed would be wrong once the title is updated by another app then the ssr app * chore: remove unused import * chore: improve not supported message
78 lines
1.8 KiB
JavaScript
78 lines
1.8 KiB
JavaScript
import { metaInfoOptionKeys, metaInfoAttributeKeys } from '../shared/constants'
|
|
import { isArray } from '../utils/is-type'
|
|
import { includes } from '../utils/array'
|
|
import { updateAttribute, updateTag, updateTitle } from './updaters'
|
|
|
|
function getTag(tags, tag) {
|
|
if (!tags[tag]) {
|
|
tags[tag] = document.getElementsByTagName(tag)[0]
|
|
}
|
|
|
|
return tags[tag]
|
|
}
|
|
|
|
/**
|
|
* Performs client-side updates when new meta info is received
|
|
*
|
|
* @param {Object} newInfo - the meta info to update to
|
|
*/
|
|
export default function updateClientMetaInfo(appId, options = {}, newInfo) {
|
|
const { ssrAttribute } = options
|
|
|
|
// only cache tags for current update
|
|
const tags = {}
|
|
|
|
const htmlTag = getTag(tags, 'html')
|
|
|
|
// if this is a server render, then dont update
|
|
if (appId === 'ssr' && htmlTag.hasAttribute(ssrAttribute)) {
|
|
// remove the server render attribute so we can update on (next) changes
|
|
htmlTag.removeAttribute(ssrAttribute)
|
|
return false
|
|
}
|
|
|
|
// initialize tracked changes
|
|
const addedTags = {}
|
|
const removedTags = {}
|
|
|
|
for (const type in newInfo) {
|
|
// ignore these
|
|
if (includes(metaInfoOptionKeys, type)) {
|
|
continue
|
|
}
|
|
|
|
if (type === 'title') {
|
|
// update the title
|
|
updateTitle(newInfo.title)
|
|
continue
|
|
}
|
|
|
|
if (includes(metaInfoAttributeKeys, type)) {
|
|
const tagName = type.substr(0, 4)
|
|
updateAttribute(options, newInfo[type], getTag(tags, tagName))
|
|
continue
|
|
}
|
|
|
|
// tags should always be an array, ignore if it isnt
|
|
if (!isArray(newInfo[type])) {
|
|
continue
|
|
}
|
|
|
|
const { oldTags, newTags } = updateTag(
|
|
appId,
|
|
options,
|
|
type,
|
|
newInfo[type],
|
|
getTag(tags, 'head'),
|
|
getTag(tags, 'body')
|
|
)
|
|
|
|
if (newTags.length) {
|
|
addedTags[type] = newTags
|
|
removedTags[type] = oldTags
|
|
}
|
|
}
|
|
|
|
return { addedTags, removedTags }
|
|
}
|