Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: dedupe meta objects by VMID #282

Merged
merged 2 commits into from
Dec 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
"dependencies": {
"deepmerge": "^2.2.1",
"lodash.isplainobject": "^4.0.6",
"lodash.uniqby": "^4.7.0",
"lodash.uniqueid": "^4.0.1",
"object-assign": "^4.1.1"
},
"devDependencies": {
Expand Down
8 changes: 7 additions & 1 deletion src/shared/getComponentOption.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import deepmerge from 'deepmerge'
import uniqBy from 'lodash.uniqby'
import uniqueId from 'lodash.uniqueid'

/**
* Returns the `opts.option` $option value of the given `opts.component`.
Expand All @@ -15,7 +17,7 @@ import deepmerge from 'deepmerge'
* @return {Object} result - final aggregated result
*/
export default function getComponentOption (opts, result = {}) {
const { component, option, deep, arrayMerge, metaTemplateKeyName, contentKeyName } = opts
const { component, option, deep, arrayMerge, metaTemplateKeyName, tagIDKeyName, contentKeyName } = opts
const { $options } = component

if (component._inactive) return result
Expand Down Expand Up @@ -64,6 +66,10 @@ export default function getComponentOption (opts, result = {}) {

return metaObject
})
result.meta = uniqBy(
result.meta.reverse(),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to reverse the list to always consider the last meta object as the "true" one

metaObject => metaObject.hasOwnProperty(tagIDKeyName) ? metaObject[tagIDKeyName] : uniqueId()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to implement a workaround for tags without vmid set at all (which is common if you want to multiple tags of the same type)

My first idea was to use Symbol but it is not supported in IE :(

So I had to use another lodash fn

)
}
return result
}
1 change: 1 addition & 0 deletions src/shared/getMetaInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export default function _getMetaInfo (options = {}) {
option: keyName,
deep: true,
metaTemplateKeyName,
tagIDKeyName,
contentKeyName,
arrayMerge (target, source) {
// we concat the arrays without merging objects contained in,
Expand Down
41 changes: 41 additions & 0 deletions test/getMetaInfo.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,47 @@ describe('getMetaInfo', () => {
__dangerouslyDisableSanitizersByTagID: {}
})
})
it('removes duplicate metaInfo in same component', () => {
component = new Vue({
metaInfo: {
title: 'Hello',
meta: [
{
vmid: 'a',
property: 'a',
content: 'a'
},
{
vmid: 'a',
property: 'a',
content: 'b'
}
]
}
})
expect(getMetaInfo(component)).to.eql({
title: 'Hello',
titleChunk: 'Hello',
titleTemplate: '%s',
htmlAttrs: {},
headAttrs: {},
bodyAttrs: {},
meta: [
{
vmid: 'a',
property: 'a',
content: 'b'
}
],
base: [],
link: [],
style: [],
script: [],
noscript: [],
__dangerouslyDisableSanitizers: [],
__dangerouslyDisableSanitizersByTagID: {}
})
})

it('properly uses string titleTemplates', () => {
component = new Vue({
Expand Down