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

chore(docs): gatsby-head example using pageContext and setHtmlAttributes #36354

Closed
wants to merge 1 commit into from

Conversation

manusa
Copy link
Contributor

@manusa manusa commented Aug 10, 2022

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)

@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Aug 10, 2022
@marvinjude marvinjude added type: documentation An issue or pull request for improving or updating Gatsby's documentation and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer labels Aug 10, 2022
@kalebheitzman
Copy link

Where do you get the loadPageDataSync function? I desperately need this for a site I'm working on.

@manusa
Copy link
Contributor Author

manusa commented Aug 12, 2022

Where do you get the loadPageDataSync function? I desperately need this for a site I'm working on.

🤦 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.

@byronwilliams
Copy link

byronwilliams commented Dec 7, 2022

This PR cannot be merged as loadPageDataSync does not exist as a prop in 4.24.0.
The PR that was merged - #36492 - has updated the types but not actually added loadPageDataSync as a prop to onRenderBody. As of 4.24.0 this is what is sent in to onRenderBody as props:

{
  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 production envs

@xavivars
Copy link
Contributor

xavivars commented Jan 4, 2023

Does this mean there's no way to change the language based on the page being rendered?

@byronwilliams
Copy link

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 });
  }
}

@LekoArts
Copy link
Contributor

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

@LekoArts LekoArts closed this Jan 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: documentation An issue or pull request for improving or updating Gatsby's documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants