Commit e153bcbb authored by Davor Santic's avatar Davor Santic Committed by Tim Neutkens

next/head allow duplicates if it has uniq keys (#4121)

resolves #4115

For now, I just added `'article:tag'` so it could be duplicated if we need more we have to extend:
```javascript
const ALLOWED_DUPLICATES = ['article:tag']
```
parent 098f3fd7
...@@ -49,9 +49,13 @@ function onStateChange (head) { ...@@ -49,9 +49,13 @@ function onStateChange (head) {
} }
const METATYPES = ['name', 'httpEquiv', 'charSet', 'itemProp', 'property'] const METATYPES = ['name', 'httpEquiv', 'charSet', 'itemProp', 'property']
const ALLOWED_DUPLICATES = ['article:tag']
// returns a function for filtering head child elements /*
// which shouldn't be duplicated, like <title/>. returns a function for filtering head child elements
which shouldn't be duplicated, like <title/>,
except we explicit allow it in ALLOWED_DUPLICATES array
*/
function unique () { function unique () {
const keys = new Set() const keys = new Set()
...@@ -81,7 +85,7 @@ function unique () { ...@@ -81,7 +85,7 @@ function unique () {
} else { } else {
const category = h.props[metatype] const category = h.props[metatype]
const categories = metaCategories[metatype] || new Set() const categories = metaCategories[metatype] || new Set()
if (categories.has(category)) return false if (categories.has(category) && ALLOWED_DUPLICATES.indexOf(category) === -1) return false
categories.add(category) categories.add(category)
metaCategories[metatype] = categories metaCategories[metatype] = categories
} }
......
...@@ -10,6 +10,12 @@ export default () => <div> ...@@ -10,6 +10,12 @@ export default () => <div>
<meta content='my meta' /> <meta content='my meta' />
{/* allow duplicates for specific tags */}
<meta property='article:tag' content='tag1' key='tag1key' />
<meta property='article:tag' content='tag2' key='tag2key' />
<meta property='dedupe:tag' content='tag3' key='tag3key' />
<meta property='dedupe:tag' content='tag4' key='tag4key' />
<React.Fragment> <React.Fragment>
<title>Fragment title</title> <title>Fragment title</title>
<meta content='meta fragment' /> <meta content='meta fragment' />
......
...@@ -43,6 +43,14 @@ export default function ({ app }, suiteName, render, fetch) { ...@@ -43,6 +43,14 @@ export default function ({ app }, suiteName, render, fetch) {
expect(html).not.toContain('<link rel="stylesheet" href="dedupe-style.css" class="next-head"/><link rel="stylesheet" href="dedupe-style.css" class="next-head"/>') expect(html).not.toContain('<link rel="stylesheet" href="dedupe-style.css" class="next-head"/><link rel="stylesheet" href="dedupe-style.css" class="next-head"/>')
}) })
test('header helper avoids dedupe of specific tags', async () => {
const html = await (render('/head'))
expect(html).toContain('<meta property="article:tag" content="tag1" class="next-head"/>')
expect(html).toContain('<meta property="article:tag" content="tag2" class="next-head"/>')
expect(html).not.toContain('<meta property="dedupe:tag" content="tag3" class="next-head"/>')
expect(html).toContain('<meta property="dedupe:tag" content="tag4" class="next-head"/>')
})
test('header helper renders Fragment children', async () => { test('header helper renders Fragment children', async () => {
const html = await (render('/head')) const html = await (render('/head'))
expect(html).toContain('<title class="next-head">Fragment title</title>') expect(html).toContain('<title class="next-head">Fragment title</title>')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment