Avoid polluting header html tags when using Gatsby Head API
Opened this issue · 0 comments
AnderUstarroz commented
Preliminary Checks
- This issue is not a duplicate. Before opening a new issue, please search existing issues: https://github.com/gatsbyjs/gatsby/issues
- This issue is not a question, feature request, RFC, or anything other than a bug report directly related to Gatsby. Please post those things in GitHub Discussions: https://github.com/gatsbyjs/gatsby/discussions
Description
The addition of the Gatsby Head API was a great feature, but as oppose to React Helmet it pollutes the inserted scripts with attributes like data-gatsby-head="true"
causing issues on search engines.
For instance on the following implementation:
export const Head = () => (
<>
<script type="application/ld+json">
{JSON.stringify({"@context":"http://schema.org","@type":"Hotel"})}
</script>
</>
)
I would expect a clean output on the header:
<head>
<script type="application/ld+json">{"@context":"http://schema.org","@type":"Hotel"}</script>
</head>
Nonetheless the output is:
<head>
<script type="application/ld+json" data-gatsby-head="true">{"@context":"http://schema.org","@type":"Hotel"}</script>
</head>
Note the data-gatsby-head="true"
attribute, this messes up Google's structured data recognition, although it is recognized by structured data validators it doesn't work well on Google's search results, probably because they use some kind of regex to parse the schemas.
Reproduction Link
Steps to Reproduce
- Go to the provided sandbox link.
- Take a look at the
src/pages/index.js
and see how the API pollutes the HEAD script addingdata-gatsby-head="true"
to the generated script, making it useless for Search engines, or at least for Google's structured data/enriched search results).
Expected Result
<head>
<script type="application/ld+json">{"@context":"http://schema.org","@type":"Hotel"}</script>
</head>
(Expecting a clean output, not made up attributes like:data-gatsby-head="true"
)
Actual Result
<head>
<script type="application/ld+json" data-gatsby-head="true">{"@context":"http://schema.org","@type":"Hotel"}</script>
</head>
Environment
System:
OS: macOS 14.5
CPU: (10) arm64 Apple M1 Max
Shell: 5.9 - /bin/zsh
Binaries:
Node: 18.15.0 - /usr/local/bin/node
Yarn: 1.22.19 - /opt/homebrew/bin/yarn
npm: 8.4.1 - /opt/homebrew/bin/npm
Browsers:
Chrome: 127.0.6533.100
Edge: 127.0.2651.98
Firefox: 129.0
Safari: 17.5
npmPackages:
gatsby: ^5.13.3 => 5.13.4
gatsby-plugin-google-tagmanager: ^5.13.1 => 5.13.1
gatsby-plugin-image: ^3.13.1 => 3.13.1
gatsby-plugin-manifest: ^5.13.1 => 5.13.1
gatsby-plugin-newrelic: ^2.5.0 => 2.6.0
gatsby-plugin-perf-budgets: ^0.0.18 => 0.0.18
gatsby-plugin-s3: ^0.4.1 => 0.4.1
gatsby-plugin-sass: ^6.13.1 => 6.13.1
gatsby-plugin-sharp: ^5.13.1 => 5.13.1
gatsby-plugin-sitemap: ^6.13.1 => 6.13.1
gatsby-plugin-svgr: ^3.0.0-beta.0 => 3.0.0-beta.0
gatsby-plugin-use-query-params: ^1.0.1 => 1.0.1
gatsby-plugin-webpack-bundle-analyser-v2: ^1.1.32 => 1.1.32
gatsby-source-apiserver: ^2.1.8 => 2.1.8
gatsby-source-filesystem: ^5.13.1 => 5.13.1
gatsby-transformer-sharp: ^5.13.1 => 5.13.1
npmGlobalPackages:
gatsby-cli: 5.8.0
Config Flags
Not relevant here