Skip to content

Commit

Permalink
Fix empty title in head (#17430)
Browse files Browse the repository at this point in the history
This handles the case where the children on a head element are undefined and not a string or an array of strings. This doesn't currently handle sub-children on head elements so additional handling will be needed if this is a feature we would like to support although can be discussed/investigated separately from this fix. 

Fixes: #17364  
Fixes: #6388
Closes: #16751
  • Loading branch information
ijjk authored Sep 28, 2020
1 parent b009ebb commit 489b13d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/next/client/head-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ function reactElementToDOM({ type, props }: JSX.Element): HTMLElement {
if (dangerouslySetInnerHTML) {
el.innerHTML = dangerouslySetInnerHTML.__html || ''
} else if (children) {
el.textContent = typeof children === 'string' ? children : children.join('')
el.textContent =
typeof children === 'string'
? children
: Array.isArray(children)
? children.join('')
: ''
}
return el
}
Expand All @@ -43,7 +48,12 @@ function updateElements(
let title = ''
if (tag) {
const { children } = tag.props
title = typeof children === 'string' ? children : children.join('')
title =
typeof children === 'string'
? children
: Array.isArray(children)
? children.join('')
: ''
}
if (title !== document.title) document.title = title
return
Expand Down
3 changes: 3 additions & 0 deletions test/integration/client-navigation/pages/nav/head-1.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@ export default (props) => (
<Link href="/nav/head-2">
<a id="to-head-2">to head 2</a>
</Link>
<Link href="/nav/head-3">
<a id="to-head-3">to head 3</a>
</Link>
</div>
)
15 changes: 15 additions & 0 deletions test/integration/client-navigation/pages/nav/head-3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react'
import Head from 'next/head'
import Link from 'next/link'

export default (props) => (
<div id="head-3">
<Head>
<meta name="description" content="Head Three" />
<title></title>
</Head>
<Link href="/nav/head-1">
<a id="to-head-1">to head 1</a>
</Link>
</div>
)
21 changes: 21 additions & 0 deletions test/integration/client-navigation/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,27 @@ describe('Client Navigation', () => {
.elementByCss('meta[name="description"]')
.getAttribute('content')
).toBe('Head One')

await browser
.elementByCss('#to-head-3')
.click()
.waitForElementByCss('#head-3', 3000)
expect(
await browser
.elementByCss('meta[name="description"]')
.getAttribute('content')
).toBe('Head Three')
expect(await browser.eval('document.title')).toBe('')

await browser
.elementByCss('#to-head-1')
.click()
.waitForElementByCss('#head-1', 3000)
expect(
await browser
.elementByCss('meta[name="description"]')
.getAttribute('content')
).toBe('Head One')
} finally {
if (browser) {
await browser.close()
Expand Down

0 comments on commit 489b13d

Please sign in to comment.