How would you do it? modify the public folder doesn't seem to work... make your own build and host it how it is? maybe there is a function that I still don't know. Any help is appreciated.
4 Answers
Gatsby recommends using react helmet for this. You can find a lot of best practices around this topic if you look at the Gatsby documentation - how to add meta data.
Follow the step by step guide in the documentation. React helmet is really powerful. Coming back to your question, that's how you can alter the HTML language attribute:
<Helmet
htmlAttributes={{
lang: 'en',
}}
/>

- 4,185
- 5
- 22
- 32
-
5This is not the way to do it in 2022. Gatsby recommend using Gatsby Head which is built-in: https://www.gatsbyjs.com/docs/reference/built-in-components/gatsby-head/ – Worm Nov 23 '22 at 10:02
According to the new Head Api doc (added in gatsby@4.19.0
) you can avoid the Helmet
dependency including inside the gatsby-ssr.js
or gatsby-ssr.ts
file, using the setHtmlAttributes
function.
exports.onRenderBody = ({ setHtmlAttributes }) => {
setHtmlAttributes({ lang: "en" })
}

- 81
- 1
- 4
An alternative to Arthur Violy's answer using the Gatsby Head API and ES6:
export const onRenderBody = ({ setHtmlAttributes }) => {
setHtmlAttributes({ lang: "en" });
};

- 71
- 6
Just an add-in to @Andre's answer, in case you want to use the Open Graph protocol property, e.g: xmlns:og
, xmlns:fb
...
Just wrap the key with the single quote '
<Helmet htmlAttributes={
{
lang: 'en',
'xmlns:og': 'http://ogp.me/ns#',
'xmlns:fb': 'http://ogp.me/ns/fb#'
}
}>
<meta charSet="utf-8" />
<meta content="width=device-width, initial-scale=1.0" name="viewport" />
....
</Helmet>

- 2,272
- 2
- 19
- 30