Commit c650ed9a authored by Melanie Seltzer's avatar Melanie Seltzer Committed by Joe Haddad

Dedupe meta tags (#8960)

* Dedupe meta tag in favor of custom tag defined in _app

* Add test

* Fix comments
parent 16e0ff9d
......@@ -72,6 +72,12 @@ function unique() {
keys.add(h.key)
return true
}
// If custom meta tag has been added the key will be prepended with `.$`, we can
// check for this and dedupe in favor of the custom one, so the default
// is not rendered as well
if (keys.has(`.$${h.key}`)) return false
switch (h.type) {
case 'title':
case 'base':
......
import React from 'react'
import Head from 'next/head'
export default () => (
<div>
<Head>
{/* this will not render */}
<meta charSet='utf-8' key='charSet' />
{/* this will override the default (same key as the default) */}
<meta charSet='iso-8859-5' key='charSet' />
{/* this will not render */}
<meta name='viewport' content='width=device-width' key='viewport' />
{/* this will override the default (same key as the default) */}
<meta
name='viewport'
content='width=device-width,initial-scale=1'
key='viewport'
/>
</Head>
<h1>Meta tags with same keys as default get deduped</h1>
</div>
)
......@@ -9,7 +9,7 @@ export default () => (
{/* this will get rendered */}
<meta charSet='iso-8859-5' />
{/* this not render */}
{/* this will not render */}
<meta name='viewport' content='width=device-width' />
{/* this will override the default */}
<meta name='viewport' content='width=device-width,initial-scale=1' />
......
......@@ -82,6 +82,16 @@ export default function (render, fetch) {
)
})
test('header helper dedupes tags with the same key as the default', async () => {
const html = await render('/head')
expect(html).toContain('<meta charSet="iso-8859-5"/>')
expect(html).not.toContain('<meta charSet="utf-8"/>')
expect(html).toContain(
'<meta name="viewport" content="width=device-width,initial-scale=1"/>'
)
expect(html).not.toContain('<meta name="width=device-width"/>')
})
test('header helper avoids dedupe of specific tags', async () => {
const html = await render('/head')
expect(html).toContain('<meta property="article:tag" content="tag1"/>')
......
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