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

Skip undefined attribute in Head #9856

Merged
merged 22 commits into from Feb 3, 2020
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
d2e2efb
Fix #8655, skip rendering meta tags with undefined props
lachlanjc Dec 28, 2019
69c19b7
Filter all tags, not just meta
lachlanjc Dec 28, 2019
7a35d88
Only render defined props
lachlanjc Dec 29, 2019
3756877
Merge branch 'canary' into fix-meta-undefined
lachlanjc Dec 29, 2019
c247ee6
Remove filtering of undefined strings
lachlanjc Dec 30, 2019
ef8c6b0
Merge branch 'canary' into fix-meta-undefined
Timer Dec 30, 2019
f317700
Merge branch 'canary' into fix-meta-undefined
ijjk Dec 31, 2019
dfa1a6a
Merge branch 'canary' into fix-meta-undefined
lachlanjc Jan 6, 2020
ec22d86
Replace Object.entries
lachlanjc Jan 6, 2020
08ab007
Remove filtering code
lachlanjc Jan 6, 2020
a77ffeb
Simplify code
lachlanjc Jan 7, 2020
58f1cb1
Merge branch 'canary' into fix-meta-undefined
lachlanjc Jan 7, 2020
f05c2b3
Merge branch 'canary' into fix-meta-undefined
lachlanjc Jan 14, 2020
8050b7c
Merge branch 'canary' into fix-meta-undefined
lachlanjc Jan 31, 2020
69aef18
Add test
lachlanjc Jan 31, 2020
9e4e016
Merge remote-tracking branch 'upstream/canary' into fix-meta-undefined
ijjk Feb 2, 2020
ddbd3c5
Add tests for undefined head prop value and tweak check
ijjk Feb 2, 2020
6cc1c9e
Merge remote-tracking branch 'upstream/canary' into fix-meta-undefined
ijjk Feb 2, 2020
6ed862d
Merge remote-tracking branch 'upstream/canary' into fix-meta-undefined
ijjk Feb 3, 2020
dff4aa5
Update to strip undefined prop values to match react
ijjk Feb 3, 2020
57b7408
Update head.js
Timer Feb 3, 2020
2af71a2
Merge branch 'canary' into fix-meta-undefined
Timer Feb 3, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions packages/next/next-server/lib/head.tsx
Expand Up @@ -140,8 +140,19 @@ function reduceComponents(
.filter(unique())
.reverse()
.map((c: React.ReactElement<any>, i: number) => {
const key = c.key || i
return React.cloneElement(c, { key })
const cleanProps: { [key: string]: string } = {}

Object.keys(c.props).forEach(key => {
let val = c.props[key]

// TODO: do we want to normalize null/true/false here also?
ijjk marked this conversation as resolved.
Show resolved Hide resolved
if (val === undefined) {
val = ''
}
cleanProps[key] = val
})

return React.cloneElement(c, { key: c.key || i, ...cleanProps })
})
}

Expand Down
3 changes: 3 additions & 0 deletions test/integration/client-navigation/pages/head.js
Expand Up @@ -16,6 +16,9 @@ export default () => (

<meta content="my meta" />

{/* this will not render */}
<meta name="empty-content" content={undefined} />

{/* allow duplicates for specific tags */}
<meta property="article:tag" content="tag1" key="tag1key" />
<meta property="article:tag" content="tag2" key="tag2key" />
Expand Down
9 changes: 9 additions & 0 deletions test/integration/client-navigation/test/index.test.js
Expand Up @@ -1097,6 +1097,15 @@ describe('Client Navigation', () => {
await browser.close()
})

it('should handle undefined prop in head client-side', async () => {
const browser = await webdriver(context.appPort, '/head')
const value = await browser
.elementByCss('meta[name="empty-content"]')
.getAttribute('content')

expect(value).toBe('')
})

renderingSuite(
(p, q) => renderViaHTTP(context.appPort, p, q),
(p, q) => fetchViaHTTP(context.appPort, p, q)
Expand Down
8 changes: 8 additions & 0 deletions test/integration/client-navigation/test/rendering.js
Expand Up @@ -17,6 +17,14 @@ export default function(render, fetch) {
expect(html.includes('My component!')).toBeTruthy()
})

it('should handle undefined prop in head server-side', async () => {
const html = await render('/head')
const $ = cheerio.load(html)
const value = $('meta[name="empty-content"]').attr('content')

expect(value).toBe('')
})

test('renders with fragment syntax', async () => {
const html = await render('/fragment-syntax')
expect(html.includes('My component!')).toBeTruthy()
Expand Down