Skip to content

Commit 9d059f6

Browse files
Sekhmetarunoda
authored andcommitted
Add support for Fragments in Head (vercel#3502)
* Add support for Fragments in Head. * Add test cases * Check if Component has custom key * Upgrade to React 16.2.0
1 parent 337fb6a commit 9d059f6

File tree

5 files changed

+27
-10
lines changed

5 files changed

+27
-10
lines changed

lib/head.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ function reduceComponents (components) {
2121
.map((c) => c.props.children)
2222
.map((children) => React.Children.toArray(children))
2323
.reduce((a, b) => a.concat(b), [])
24+
.reduce((a, b) => {
25+
if (React.Fragment && b.type === React.Fragment) {
26+
return a.concat(React.Children.toArray(b.props.children))
27+
}
28+
return a.concat(b)
29+
}, [])
2430
.reverse()
2531
.concat(...defaultHead())
2632
.filter((c) => !!c)
@@ -54,7 +60,7 @@ function unique () {
5460
const metaCategories = {}
5561

5662
return (h) => {
57-
if (h.key) {
63+
if (h.key && h.key.startsWith('.$')) {
5864
if (keys.has(h.key)) return false
5965
keys.add(h.key)
6066
}

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
"webpack-hot-middleware": "2.19.1",
106106
"write-file-webpack-plugin": "4.2.0",
107107
"xss-filters": "1.2.7",
108-
"uglifyjs-webpack-plugin": "^1.1.1"
108+
"uglifyjs-webpack-plugin": "^1.1.1"
109109
},
110110
"devDependencies": {
111111
"@taskr/babel": "1.1.0",
@@ -131,8 +131,8 @@
131131
"node-notifier": "5.1.2",
132132
"nyc": "11.2.1",
133133
"portfinder": "1.0.13",
134-
"react": "16.0.0",
135-
"react-dom": "16.0.0",
134+
"react": "16.2.0",
135+
"react-dom": "16.2.0",
136136
"standard": "9.0.2",
137137
"taskr": "1.1.0",
138138
"wd": "1.4.1"

test/integration/basic/pages/head.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ export default () => <div>
1010

1111
<meta content='my meta' />
1212

13+
<React.Fragment>
14+
<title>Fragment title</title>
15+
<meta content='meta fragment' />
16+
</React.Fragment>
17+
1318
{/* the following 2 links tag will be rendered both */}
1419
<link rel='stylesheet' href='/dup-style.css' />
1520
<link rel='stylesheet' href='/dup-style.css' />

test/integration/basic/test/rendering.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ export default function ({ app }, suiteName, render, fetch) {
3838
expect(html).not.toContain('<link rel="stylesheet" href="dedupe-style.css" class="next-head"/><link rel="stylesheet" href="dedupe-style.css" class="next-head"/>')
3939
})
4040

41+
test('header helper renders Fragment children', async () => {
42+
const html = await (render('/head'))
43+
expect(html).toContain('<title class="next-head">Fragment title</title>')
44+
expect(html).toContain('<meta content="meta fragment" class="next-head"/>')
45+
})
46+
4147
it('should render the page with custom extension', async () => {
4248
const html = await render('/custom-extension')
4349
expect(html).toContain('<div>Hello</div>')

yarn.lock

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4803,9 +4803,9 @@ react-deep-force-update@^2.1.1:
48034803
version "2.1.1"
48044804
resolved "https://registry.yarnpkg.com/react-deep-force-update/-/react-deep-force-update-2.1.1.tgz#8ea4263cd6455a050b37445b3f08fd839d86e909"
48054805

4806-
react-dom@16.0.0:
4807-
version "16.0.0"
4808-
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-16.0.0.tgz#9cc3079c3dcd70d4c6e01b84aab2a7e34c303f58"
4806+
react-dom@16.2.0:
4807+
version "16.2.0"
4808+
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-16.2.0.tgz#69003178601c0ca19b709b33a83369fe6124c044"
48094809
dependencies:
48104810
fbjs "^0.8.16"
48114811
loose-envify "^1.1.0"
@@ -4828,9 +4828,9 @@ react-proxy@^3.0.0-alpha.0:
48284828
dependencies:
48294829
lodash "^4.6.1"
48304830

4831-
react@16.0.0:
4832-
version "16.0.0"
4833-
resolved "https://registry.yarnpkg.com/react/-/react-16.0.0.tgz#ce7df8f1941b036f02b2cca9dbd0cb1f0e855e2d"
4831+
react@16.2.0:
4832+
version "16.2.0"
4833+
resolved "https://registry.yarnpkg.com/react/-/react-16.2.0.tgz#a31bd2dab89bff65d42134fa187f24d054c273ba"
48344834
dependencies:
48354835
fbjs "^0.8.16"
48364836
loose-envify "^1.1.0"

0 commit comments

Comments
 (0)