Unverified Commit 489b13d0 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix empty title in head (#17430)

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: https://github.com/vercel/next.js/issues/17364  
Fixes: https://github.com/vercel/next.js/issues/6388
Closes: https://github.com/vercel/next.js/pull/16751
parent b009ebbe
...@@ -25,7 +25,12 @@ function reactElementToDOM({ type, props }: JSX.Element): HTMLElement { ...@@ -25,7 +25,12 @@ function reactElementToDOM({ type, props }: JSX.Element): HTMLElement {
if (dangerouslySetInnerHTML) { if (dangerouslySetInnerHTML) {
el.innerHTML = dangerouslySetInnerHTML.__html || '' el.innerHTML = dangerouslySetInnerHTML.__html || ''
} else if (children) { } else if (children) {
el.textContent = typeof children === 'string' ? children : children.join('') el.textContent =
typeof children === 'string'
? children
: Array.isArray(children)
? children.join('')
: ''
} }
return el return el
} }
...@@ -43,7 +48,12 @@ function updateElements( ...@@ -43,7 +48,12 @@ function updateElements(
let title = '' let title = ''
if (tag) { if (tag) {
const { children } = tag.props 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 if (title !== document.title) document.title = title
return return
......
...@@ -11,5 +11,8 @@ export default (props) => ( ...@@ -11,5 +11,8 @@ export default (props) => (
<Link href="/nav/head-2"> <Link href="/nav/head-2">
<a id="to-head-2">to head 2</a> <a id="to-head-2">to head 2</a>
</Link> </Link>
<Link href="/nav/head-3">
<a id="to-head-3">to head 3</a>
</Link>
</div> </div>
) )
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>
)
...@@ -1237,6 +1237,27 @@ describe('Client Navigation', () => { ...@@ -1237,6 +1237,27 @@ describe('Client Navigation', () => {
.elementByCss('meta[name="description"]') .elementByCss('meta[name="description"]')
.getAttribute('content') .getAttribute('content')
).toBe('Head One') ).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 { } finally {
if (browser) { if (browser) {
await browser.close() await browser.close()
......
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