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

Dedupe meta tags #8960

Merged
merged 7 commits into from
Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions packages/next/next-server/lib/head.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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 not render */}
Timer marked this conversation as resolved.
Show resolved Hide resolved
<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>
)
10 changes: 10 additions & 0 deletions test/integration/client-navigation/test/rendering.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Copy link
Contributor

Choose a reason for hiding this comment

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

You render wrong page

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"/>')
Expand Down