Skip to content

Commit

Permalink
[gatsby-plugin-react-helmet] Add support for htmlAttributes and bodyA…
Browse files Browse the repository at this point in the history
…ttributes fixes #2447 (#2448)
  • Loading branch information
KyleAMathews authored Oct 13, 2017
1 parent 3f88690 commit 25e3c6a
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 13 deletions.
7 changes: 6 additions & 1 deletion packages/gatsby-plugin-react-helmet/src/gatsby-ssr.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import Helmet from "react-helmet"

exports.onRenderBody = ({ setHeadComponents }, pluginOptions) => {
exports.onRenderBody = (
{ setHeadComponents, setHtmlAttributes, setBodyAttributes },
pluginOptions
) => {
const helmet = Helmet.renderStatic()
setHtmlAttributes(helmet.htmlAttributes.toComponent())
setBodyAttributes(helmet.bodyAttributes.toComponent())
setHeadComponents([
helmet.title.toComponent(),
helmet.link.toComponent(),
Expand Down
21 changes: 19 additions & 2 deletions packages/gatsby/cache-dir/api-ssr-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
* @param {function} $0.setHeadComponents Takes an array of components as its
* first argument which are added to the `headComponents` array which is passed
* to the `html.js` component.
* @param {function} $0.setHtmlAttributes Takes an object of props which will
* spread into the `<html>` component.
* @param {function} $0.setBodyAttributes Takes an object of props which will
* spread into the `<body>` component.
* @param {function} $0.setPreBodyComponents Takes an array of components as its
* first argument which are added to the `preBodyComponents` array which is passed
* to the `html.js` component.
Expand Down Expand Up @@ -55,6 +59,10 @@ exports.replaceRenderer = true
* @param {function} $0.setHeadComponents Takes an array of components as its
* first argument which are added to the `headComponents` array which is passed
* to the `html.js` component.
* @param {function} $0.setHtmlAttributes Takes an object of props which will
* spread into the `<html>` component.
* @param {function} $0.setBodyAttributes Takes an object of props which will
* spread into the `<body>` component.
* @param {function} $0.setPreBodyComponents Takes an array of components as its
* first argument which are added to the `preBodyComponents` array which is passed
* to the `html.js` component.
Expand All @@ -67,11 +75,20 @@ exports.replaceRenderer = true
* @example
* import helmet from "react-helmet"
*
* exports.onRenderBody = ({ setHeadComponents }, pluginOptions) => {
* exports.onRenderBody = (
* { setHeadComponents, setHtmlAttributes, setBodyAttributes },
* pluginOptions
* ) => {
* const helmet = Helmet.renderStatic()
* setHtmlAttributes(helmet.htmlAttributes.toComponent())
* setBodyAttributes(helmet.bodyAttributes.toComponent())
* setHeadComponents([
* helmet.title.toComponent(),
* helmet.meta.toComponent(),
* helmet.link.toComponent(),
* helmet.meta.toComponent(),
* helmet.noscript.toComponent(),
* helmet.script.toComponent(),
* helmet.style.toComponent(),
* ])
* }
*/
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby/cache-dir/default-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = class HTML extends React.Component {
)
}
return (
<html>
<html {...this.props.htmlAttributes}>
<head>
<meta charSet="utf-8" />
<meta httpEquiv="x-ua-compatible" content="ie=edge" />
Expand All @@ -32,7 +32,7 @@ module.exports = class HTML extends React.Component {
{this.props.headComponents}
{css}
</head>
<body>
<body {...this.props.bodyAttributes}>
{this.props.preBodyComponents}
<div
key={`body`}
Expand Down
12 changes: 12 additions & 0 deletions packages/gatsby/cache-dir/develop-static-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ try {
module.exports = (locals, callback) => {
// const apiRunner = require(`${directory}/.cache/api-runner-ssr`)
let headComponents = []
let htmlAttributes = {}
let bodyAttributes = {}
let preBodyComponents = []
let postBodyComponents = []
let bodyProps = {}
Expand All @@ -28,6 +30,14 @@ module.exports = (locals, callback) => {
headComponents = headComponents.concat(components)
}

const setHtmlAttributes = attributes => {
htmlAttributes = merge(htmlAttributes, attributes)
}

const setBodyAttributes = attributes => {
bodyAttributes = merge(bodyAttributes, attributes)
}

const setPreBodyComponents = components => {
preBodyComponents = preBodyComponents.concat(components)
}
Expand All @@ -42,6 +52,8 @@ module.exports = (locals, callback) => {

apiRunner(`onRenderBody`, {
setHeadComponents,
setHtmlAttributes,
setBodyAttributes,
setPreBodyComponents,
setPostBodyComponents,
setBodyProps,
Expand Down
28 changes: 23 additions & 5 deletions packages/gatsby/cache-dir/static-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from "react"
import { renderToString, renderToStaticMarkup } from "react-dom/server"
import { StaticRouter, Route, withRouter } from "react-router-dom"
import { kebabCase, get, merge, isArray, isString } from "lodash"

import apiRunner from "./api-runner-ssr"
import pages from "./pages.json"
import syncRequires from "./sync-requires"
Expand Down Expand Up @@ -44,20 +45,30 @@ module.exports = (locals, callback) => {
pathPrefix = `${__PATH_PREFIX__}/`
}

let bodyHTML = ``
let bodyHtml = ``
let headComponents = []
let htmlAttributes = {}
let bodyAttributes = {}
let preBodyComponents = []
let postBodyComponents = []
let bodyProps = {}

const replaceBodyHTMLString = body => {
bodyHTML = body
bodyHtml = body
}

const setHeadComponents = components => {
headComponents = headComponents.concat(components)
}

const setHtmlAttributes = attributes => {
htmlAttributes = merge(htmlAttributes, attributes)
}

const setBodyAttributes = attributes => {
bodyAttributes = merge(bodyAttributes, attributes)
}

const setPreBodyComponents = components => {
preBodyComponents = preBodyComponents.concat(components)
}
Expand Down Expand Up @@ -103,22 +114,27 @@ module.exports = (locals, callback) => {
bodyComponent,
replaceBodyHTMLString,
setHeadComponents,
setHtmlAttributes,
setBodyAttributes,
setPreBodyComponents,
setPostBodyComponents,
setBodyProps,
})

// If no one stepped up, we'll handle it.
if (!bodyHTML) {
bodyHTML = renderToString(bodyComponent)
if (!bodyHtml) {
bodyHtml = renderToString(bodyComponent)
}

apiRunner(`onRenderBody`, {
setHeadComponents,
setHtmlAttributes,
setBodyAttributes,
setPreBodyComponents,
setPostBodyComponents,
setBodyProps,
pathname: locals.path,
bodyHtml,
})

// Add the chunk-manifest as a head component.
Expand Down Expand Up @@ -204,9 +220,11 @@ module.exports = (locals, callback) => {
<Html
{...bodyProps}
headComponents={headComponents}
htmlAttributes={htmlAttributes}
bodyAttributes={bodyAttributes}
preBodyComponents={preBodyComponents}
postBodyComponents={postBodyComponents}
body={bodyHTML}
body={bodyHtml}
path={locals.path}
/>
)}`
Expand Down
3 changes: 2 additions & 1 deletion packages/gatsby/src/commands/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ module.exports = async function build(program: BuildArgs) {
// an equivalent static directory within public.
copyStaticDirectory()

let activity = report.activityTimer(`Building CSS`)
let activity
activity = report.activityTimer(`Building CSS`)
activity.start()
await buildCSS(program).catch(err => {
reportFailure(`Generating CSS failed`, err)
Expand Down
4 changes: 2 additions & 2 deletions www/src/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default class HTML extends React.Component {
}

return (
<html lang="en">
<html {...this.props.htmlAttributes}>
<head>
<link
rel="preload"
Expand Down Expand Up @@ -74,7 +74,7 @@ export default class HTML extends React.Component {
/>
{css}
</head>
<body>
<body {...this.props.bodyAttributes}>
<div
id="___gatsby"
dangerouslySetInnerHTML={{ __html: this.props.body }}
Expand Down
1 change: 1 addition & 0 deletions www/src/layouts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class DefaultLayout extends React.Component {
<meta name="twitter:site" content="@gatsbyjs" />
<meta name="og:type" content="website" />
<meta name="og:site_name" content="GatsbyJS" />
<html lang="en" />
</Helmet>
<Navigation pathname={this.props.location.pathname} />
<div
Expand Down

0 comments on commit 25e3c6a

Please sign in to comment.