-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
chore(docs): gatsby-head example using pageContext and setHtmlAttributes #36354
Conversation
Where do you get the |
fc9a344
to
801c6f4
Compare
🤦 there was a copy-paste mistake. I've updated the example, everything should be clearer now. Also note that the function is only populated when building production, I'm not sure if this should be reported as a bug, and fixed, or it's part of the expected behavior. |
Signed-off-by: Marc Nuri <[email protected]>
801c6f4
to
cf11d47
Compare
This PR cannot be merged as {
setHeadComponents: [Function: setHeadComponents],
setHtmlAttributes: [Function: setHtmlAttributes],
setBodyAttributes: [Function: setBodyAttributes],
setPreBodyComponents: [Function: setPreBodyComponents],
setPostBodyComponents: [Function: setPostBodyComponents],
setBodyProps: [Function: setBodyProps],
pathname: '/'
} Or this PR should mark it as optional as it's not present in non |
Does this mean there's no way to change the language based on the page being rendered? |
Yes, you can do it. It will just won't show up in dev, or type check correctly: export const onRenderBody: GatsbySSR["onRenderBody"] = (props) => {
// only exists when NODE_ENV === "production" in v4.24.0
if (props.loadPageDataSync) {
const { loadPageDataSync, setHtmlAttributes, pathname } = props;
const { pageContext } = loadPageDataSync(pathname).result as Record<
string,
PageContext
>;
setHtmlAttributes({ lang: pageContext.locale });
}
} |
Hi! With #37449 merged there won't be a need anymore for this workaround using an API we don't really want people to use. It'll be released in Gatsby 5.5 |
Description
New example in https://www.gatsbyjs.com/docs/reference/built-in-components/gatsby-head/#editing-html-and-body showing how to use setHtmlAttributes with dynamic values provided by the
pageContext
.Documentation
Related Issues
Related to #35841 (reply in thread)