Skip to content

Commit 25e3c6a

Browse files
authored
[gatsby-plugin-react-helmet] Add support for htmlAttributes and bodyAttributes fixes #2447 (#2448)
1 parent 3f88690 commit 25e3c6a

File tree

8 files changed

+67
-13
lines changed

8 files changed

+67
-13
lines changed

packages/gatsby-plugin-react-helmet/src/gatsby-ssr.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import Helmet from "react-helmet"
22

3-
exports.onRenderBody = ({ setHeadComponents }, pluginOptions) => {
3+
exports.onRenderBody = (
4+
{ setHeadComponents, setHtmlAttributes, setBodyAttributes },
5+
pluginOptions
6+
) => {
47
const helmet = Helmet.renderStatic()
8+
setHtmlAttributes(helmet.htmlAttributes.toComponent())
9+
setBodyAttributes(helmet.bodyAttributes.toComponent())
510
setHeadComponents([
611
helmet.title.toComponent(),
712
helmet.link.toComponent(),

packages/gatsby/cache-dir/api-ssr-docs.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
* @param {function} $0.setHeadComponents Takes an array of components as its
1010
* first argument which are added to the `headComponents` array which is passed
1111
* to the `html.js` component.
12+
* @param {function} $0.setHtmlAttributes Takes an object of props which will
13+
* spread into the `<html>` component.
14+
* @param {function} $0.setBodyAttributes Takes an object of props which will
15+
* spread into the `<body>` component.
1216
* @param {function} $0.setPreBodyComponents Takes an array of components as its
1317
* first argument which are added to the `preBodyComponents` array which is passed
1418
* to the `html.js` component.
@@ -55,6 +59,10 @@ exports.replaceRenderer = true
5559
* @param {function} $0.setHeadComponents Takes an array of components as its
5660
* first argument which are added to the `headComponents` array which is passed
5761
* to the `html.js` component.
62+
* @param {function} $0.setHtmlAttributes Takes an object of props which will
63+
* spread into the `<html>` component.
64+
* @param {function} $0.setBodyAttributes Takes an object of props which will
65+
* spread into the `<body>` component.
5866
* @param {function} $0.setPreBodyComponents Takes an array of components as its
5967
* first argument which are added to the `preBodyComponents` array which is passed
6068
* to the `html.js` component.
@@ -67,11 +75,20 @@ exports.replaceRenderer = true
6775
* @example
6876
* import helmet from "react-helmet"
6977
*
70-
* exports.onRenderBody = ({ setHeadComponents }, pluginOptions) => {
78+
* exports.onRenderBody = (
79+
* { setHeadComponents, setHtmlAttributes, setBodyAttributes },
80+
* pluginOptions
81+
* ) => {
82+
* const helmet = Helmet.renderStatic()
83+
* setHtmlAttributes(helmet.htmlAttributes.toComponent())
84+
* setBodyAttributes(helmet.bodyAttributes.toComponent())
7185
* setHeadComponents([
7286
* helmet.title.toComponent(),
73-
* helmet.meta.toComponent(),
7487
* helmet.link.toComponent(),
88+
* helmet.meta.toComponent(),
89+
* helmet.noscript.toComponent(),
90+
* helmet.script.toComponent(),
91+
* helmet.style.toComponent(),
7592
* ])
7693
* }
7794
*/

packages/gatsby/cache-dir/default-html.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module.exports = class HTML extends React.Component {
2121
)
2222
}
2323
return (
24-
<html>
24+
<html {...this.props.htmlAttributes}>
2525
<head>
2626
<meta charSet="utf-8" />
2727
<meta httpEquiv="x-ua-compatible" content="ie=edge" />
@@ -32,7 +32,7 @@ module.exports = class HTML extends React.Component {
3232
{this.props.headComponents}
3333
{css}
3434
</head>
35-
<body>
35+
<body {...this.props.bodyAttributes}>
3636
{this.props.preBodyComponents}
3737
<div
3838
key={`body`}

packages/gatsby/cache-dir/develop-static-entry.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ try {
1919
module.exports = (locals, callback) => {
2020
// const apiRunner = require(`${directory}/.cache/api-runner-ssr`)
2121
let headComponents = []
22+
let htmlAttributes = {}
23+
let bodyAttributes = {}
2224
let preBodyComponents = []
2325
let postBodyComponents = []
2426
let bodyProps = {}
@@ -28,6 +30,14 @@ module.exports = (locals, callback) => {
2830
headComponents = headComponents.concat(components)
2931
}
3032

33+
const setHtmlAttributes = attributes => {
34+
htmlAttributes = merge(htmlAttributes, attributes)
35+
}
36+
37+
const setBodyAttributes = attributes => {
38+
bodyAttributes = merge(bodyAttributes, attributes)
39+
}
40+
3141
const setPreBodyComponents = components => {
3242
preBodyComponents = preBodyComponents.concat(components)
3343
}
@@ -42,6 +52,8 @@ module.exports = (locals, callback) => {
4252

4353
apiRunner(`onRenderBody`, {
4454
setHeadComponents,
55+
setHtmlAttributes,
56+
setBodyAttributes,
4557
setPreBodyComponents,
4658
setPostBodyComponents,
4759
setBodyProps,

packages/gatsby/cache-dir/static-entry.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from "react"
22
import { renderToString, renderToStaticMarkup } from "react-dom/server"
33
import { StaticRouter, Route, withRouter } from "react-router-dom"
44
import { kebabCase, get, merge, isArray, isString } from "lodash"
5+
56
import apiRunner from "./api-runner-ssr"
67
import pages from "./pages.json"
78
import syncRequires from "./sync-requires"
@@ -44,20 +45,30 @@ module.exports = (locals, callback) => {
4445
pathPrefix = `${__PATH_PREFIX__}/`
4546
}
4647

47-
let bodyHTML = ``
48+
let bodyHtml = ``
4849
let headComponents = []
50+
let htmlAttributes = {}
51+
let bodyAttributes = {}
4952
let preBodyComponents = []
5053
let postBodyComponents = []
5154
let bodyProps = {}
5255

5356
const replaceBodyHTMLString = body => {
54-
bodyHTML = body
57+
bodyHtml = body
5558
}
5659

5760
const setHeadComponents = components => {
5861
headComponents = headComponents.concat(components)
5962
}
6063

64+
const setHtmlAttributes = attributes => {
65+
htmlAttributes = merge(htmlAttributes, attributes)
66+
}
67+
68+
const setBodyAttributes = attributes => {
69+
bodyAttributes = merge(bodyAttributes, attributes)
70+
}
71+
6172
const setPreBodyComponents = components => {
6273
preBodyComponents = preBodyComponents.concat(components)
6374
}
@@ -103,22 +114,27 @@ module.exports = (locals, callback) => {
103114
bodyComponent,
104115
replaceBodyHTMLString,
105116
setHeadComponents,
117+
setHtmlAttributes,
118+
setBodyAttributes,
106119
setPreBodyComponents,
107120
setPostBodyComponents,
108121
setBodyProps,
109122
})
110123

111124
// If no one stepped up, we'll handle it.
112-
if (!bodyHTML) {
113-
bodyHTML = renderToString(bodyComponent)
125+
if (!bodyHtml) {
126+
bodyHtml = renderToString(bodyComponent)
114127
}
115128

116129
apiRunner(`onRenderBody`, {
117130
setHeadComponents,
131+
setHtmlAttributes,
132+
setBodyAttributes,
118133
setPreBodyComponents,
119134
setPostBodyComponents,
120135
setBodyProps,
121136
pathname: locals.path,
137+
bodyHtml,
122138
})
123139

124140
// Add the chunk-manifest as a head component.
@@ -204,9 +220,11 @@ module.exports = (locals, callback) => {
204220
<Html
205221
{...bodyProps}
206222
headComponents={headComponents}
223+
htmlAttributes={htmlAttributes}
224+
bodyAttributes={bodyAttributes}
207225
preBodyComponents={preBodyComponents}
208226
postBodyComponents={postBodyComponents}
209-
body={bodyHTML}
227+
body={bodyHtml}
210228
path={locals.path}
211229
/>
212230
)}`

packages/gatsby/src/commands/build.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ module.exports = async function build(program: BuildArgs) {
2929
// an equivalent static directory within public.
3030
copyStaticDirectory()
3131

32-
let activity = report.activityTimer(`Building CSS`)
32+
let activity
33+
activity = report.activityTimer(`Building CSS`)
3334
activity.start()
3435
await buildCSS(program).catch(err => {
3536
reportFailure(`Generating CSS failed`, err)

www/src/html.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export default class HTML extends React.Component {
2323
}
2424

2525
return (
26-
<html lang="en">
26+
<html {...this.props.htmlAttributes}>
2727
<head>
2828
<link
2929
rel="preload"
@@ -74,7 +74,7 @@ export default class HTML extends React.Component {
7474
/>
7575
{css}
7676
</head>
77-
<body>
77+
<body {...this.props.bodyAttributes}>
7878
<div
7979
id="___gatsby"
8080
dangerouslySetInnerHTML={{ __html: this.props.body }}

www/src/layouts/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class DefaultLayout extends React.Component {
5757
<meta name="twitter:site" content="@gatsbyjs" />
5858
<meta name="og:type" content="website" />
5959
<meta name="og:site_name" content="GatsbyJS" />
60+
<html lang="en" />
6061
</Helmet>
6162
<Navigation pathname={this.props.location.pathname} />
6263
<div

0 commit comments

Comments
 (0)