mirror of
https://github.com/tenrok/vue-meta.git
synced 2026-06-22 08:00:35 +03:00
prevent overriding pre-existing meta tags with shared attribute names
This commit is contained in:
@@ -14,7 +14,7 @@ import deepmerge from 'deepmerge'
|
|||||||
* @return {Object} - final aggregated result
|
* @return {Object} - final aggregated result
|
||||||
*/
|
*/
|
||||||
export default function getComponentOption (opts, result = {}) {
|
export default function getComponentOption (opts, result = {}) {
|
||||||
const { component, option, deep } = opts
|
const { component, option, deep, arrayMerge } = opts
|
||||||
const { $options } = component
|
const { $options } = component
|
||||||
|
|
||||||
// only collect option data if it exists
|
// only collect option data if it exists
|
||||||
@@ -34,7 +34,7 @@ export default function getComponentOption (opts, result = {}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// merge with existing options
|
// merge with existing options
|
||||||
result = deepmerge(result, data)
|
result = deepmerge(result, data, { arrayMerge })
|
||||||
}
|
}
|
||||||
|
|
||||||
// collect & aggregate child options if deep = true
|
// collect & aggregate child options if deep = true
|
||||||
@@ -42,7 +42,7 @@ export default function getComponentOption (opts, result = {}) {
|
|||||||
const { $children } = component
|
const { $children } = component
|
||||||
for (let i = 0, len = $children.length; i < len; i++) {
|
for (let i = 0, len = $children.length; i < len; i++) {
|
||||||
const component = $children[i]
|
const component = $children[i]
|
||||||
result = getComponentOption({ option, deep, component }, result)
|
result = getComponentOption({ option, deep, component, arrayMerge }, result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,41 @@ import getComponentOption from './getComponentOption'
|
|||||||
*/
|
*/
|
||||||
export default function getMetaInfo (component) {
|
export default function getMetaInfo (component) {
|
||||||
// collect & aggregate all metaInfo $options
|
// collect & aggregate all metaInfo $options
|
||||||
const info = getComponentOption({ component, option: 'metaInfo', deep: true })
|
const info = getComponentOption({
|
||||||
|
component,
|
||||||
|
option: 'metaInfo',
|
||||||
|
deep: true,
|
||||||
|
// In order to prevent certain tags from being overwritten,
|
||||||
|
// (like <meta name="description" ...> being overwritten by
|
||||||
|
// <meta name="keywords" ...>), we need to specify a different
|
||||||
|
// array merge strategy. This strategy exploits a trick
|
||||||
|
// with associative arrays in JS using O(1) lookup
|
||||||
|
|
||||||
|
/* eslint-disable no-labels */
|
||||||
|
arrayMerge (oldTags, newTags) {
|
||||||
|
const updatedTags = []
|
||||||
|
for (let oldTagIndex in oldTags) {
|
||||||
|
const oldTag = oldTags[oldTagIndex]
|
||||||
|
let sharedAttributes = false
|
||||||
|
ifTagsHaveEqualSharedAttributeValues: for (let newTagIndex in newTags) {
|
||||||
|
const newTag = newTags[newTagIndex]
|
||||||
|
for (let attribute in newTag) {
|
||||||
|
if (newTag.hasOwnProperty(attribute) && oldTag.hasOwnProperty(attribute)) {
|
||||||
|
if (oldTag[attribute] === newTag[attribute]) {
|
||||||
|
sharedAttributes = true
|
||||||
|
break ifTagsHaveEqualSharedAttributeValues
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!sharedAttributes) {
|
||||||
|
updatedTags.push(oldTag)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return updatedTags.concat(newTags)
|
||||||
|
}
|
||||||
|
/* eslint-enable no-labels */
|
||||||
|
})
|
||||||
|
|
||||||
// if any info options are a function, coerce them to the result of a call
|
// if any info options are a function, coerce them to the result of a call
|
||||||
for (let key in info) {
|
for (let key in info) {
|
||||||
|
|||||||
Reference in New Issue
Block a user