1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00

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']
```
This commit is contained in:
Davor Santic 2018-06-01 13:12:33 +02:00 committed by Tim Neutkens
parent 098f3fd7e9
commit e153bcbb9a
3 changed files with 21 additions and 3 deletions

View file

@ -49,9 +49,13 @@ function onStateChange (head) {
}
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 () {
const keys = new Set()
@ -81,7 +85,7 @@ function unique () {
} else {
const category = h.props[metatype]
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)
metaCategories[metatype] = categories
}

View file

@ -10,6 +10,12 @@ export default () => <div>
<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>
<title>Fragment title</title>
<meta content='meta fragment' />

View file

@ -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"/>')
})
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 () => {
const html = await (render('/head'))
expect(html).toContain('<title class="next-head">Fragment title</title>')