Replies: 7 comments 5 replies
-
I wonder if there's features/code in React Helmet we could drop by forking. This would make the default Gatsby runtime smaller. |
Beta Was this translation helpful? Give feedback.
-
In the "How we teach this" section there might be room for local development-time warnings that could prompt the user to use the API that could make improvements to their SEO like:
|
Beta Was this translation helpful? Give feedback.
-
An official |
Beta Was this translation helpful? Give feedback.
-
Before jumping on my personal preferences, I would like to thank you so much for this amazing RFC! I love the underlying idea, this is amazing 🚀 🎸
This would be okay to me as a person using Gatsby to only rely on the Gatsby A big advantage would be that it scopes the That's a very personal opinion I would say, I'm sharing in case other may feel the same 😊
If possible, I would warn anytime when people are setting a "raw meta tags" that is already covered by the Gatsby one. This will help people understand the way Gatsby Head is built and drive them in the right direction. That would be very interesting for the DX, I love that idea :D. Also, as mentioned last time, I really like the composition approach using a specific Gatsby
I would say yes from what @KyleAMathews has mentioned, we can get rid off some overhead and rethink the module according to our use cases. More work, but more freedom :).
I would sign for this one 🗒️ ✍🏻 🚀 |
Beta Was this translation helpful? Give feedback.
-
Would this support multiple The primary use case for this is being able to ensure that components are responsible for their own Head-data. Eg, a pagination component being responsible for the next/prev page hrefs in the head. |
Beta Was this translation helpful? Give feedback.
-
Having a HEAD component in Gatsby adds a lot of benefits. We can capture preload/preconnect headers used deep inside pages and add them as an HTTP header (it would improve gatsby-plugin-image even more). As mentioned in the RFC, a HEAD component will make it easier for our users to add tags to their websites. I want to push back on forking/using react-helmet as the base library. React-helmet has served the react helmet well, and the API is 👍. The sad part is that it's not a small library (https://bundlephobia.com/[email protected]) and not concurrent mode ready. This is not react-helmet's fault as it has to work in every React app. The great thing about Gatsby is that we control the whole lifecycle. We can optimize it for our use case. I suggest also checking out to draw ideas from: Questions
Feature requestsI would highly encourage you to move to create custom components for all supported tags. Why? We can add some built-in optimizations that way.
|
Beta Was this translation helpful? Give feedback.
-
I like this idea. +1 to Wardpeets comment about being able to pipe in data from GraphQL. I have an SEO component that I pipe data in from SANITY by way of component shadowing a useSiteMetata hook. So I have a “core” theme with a base SEO component that gets data from Gatsby-config. Then I have a sanity theme which layers on top of the core theme and shadows this SEO component and also shadows my useSitemetadata hook as well to pipe information from SANITY instead of from Gatsby-config. You can see that here: https://github.com/ehowey/gatsby-theme-catalyst/tree/main/themes/gatsby-theme-catalyst-sanity/src/gatsby-theme-catalyst-core/utils |
Beta Was this translation helpful? Give feedback.
-
Summary
Add a
<Head>
component to Gatsby as the blessed way of adding meta tags to pages.Basic example
Motivation
Gatsby is fantastic for SEO thanks to its outstanding performance. However, we tell users to add page metadata by setting up a non-core plugin (react-helmet) and copy-and-pasting code (!) from our docs.
This is not only confusing and more complex than it needs to be, but also not maintainable. We can never update or improve code users copy-and-paste. We also cannot bake best practices into our SEO setup to make every Gatsby site shine across search and social media by default.
Detailed design
In general, this component should work similarly to the
<SEO>
component from our docs and be based on react-helmet, the standard in the React community.Compared to normal react-helmet, this
<Head>
component can leverage the information Gatsby has about a website (specifically, thesiteMetadata
and current path). On top of that, basic information, such as the page title, is provided via props (instead of manually via meta tags) allowing us to generate a set of "best practice" meta tags that work well across search and social media automatically.API
Users define the default metadata in the
gatsby-config.js
via thesiteMetadata
field:The component is automatically rendered on every page with the default data. That means in the example above, all pages will show "Dick's Flower Shop" as the title.
To override the defaults for a specific page, users import and render the
<Head>
component on a page and provide different data:To provide a great experience across search and social media, four pieces of information need to be present on every page:
With these four basic pieces of information we can create a set of meta tags that work across search and social media:
To ensure we can always inject these (duplicate) meta tags for the basic information, we let users provide the defaults from siteMetadata (see example above) and users provide the overrides for certain pages via props:
Drawbacks
Alternatives
Transparent react-helmet wrapper
Rather than owning the entire API surface area (including bugs) of react-helmet I considered "react-helmet built into Gatsby". That could look like this:
The upsides of doing this would be that documentation would be much less of a chore (we could point to the react-helmet documentation for advanced use cases) and we wouldn't own react-helmets' bugs (users would know it's a react-helmet bug rather than a Gatsby bug).
However, that could also backfire as it wouldn't be just react-helmet. Instead, it would have to be a wrapper around react-helmet that supports the
siteMetadata
defaulting and basic information support described above. That means the real API for users would look like this:That is confusing, as the
title
prop is not part of the react-helmet API, and neither is thesiteMetadata
support. So we would have to documentat that yes, this is react-helmet, however it's our special version of it.That feels like more trouble than it's worth to me. I think the tradeoff of owning that API surface area and the bugs are preferable.
Transparent react-helmet with
Meta
componentsTo combat the weirdness of wrapping react-helmet, we could also consider introducing special
<Meta>
(and<MetaTitle>
?) components that would be used in place of the standard<meta>
elements.This would allow us to turn
<MetaTitle>
and other<Meta>
components into the recommended set of meta tags automatically. Example usage would look like this:The upside is that this makes the boundary between the (now completely bog standard) react-helmet API and the Gatsby-specific additions very clear. We could again refer straight to the react-helmet documentation.
The downside is that it's more verbose. It could lead to users adding redundant meta tags that we theoretically already add automatically, for example:
Adoption strategy
This will work out of the box in all existing Gatsby apps and will be interoperable with the standard
<SEO>
component highlighted in our docs as it also uses react-helmet.How we teach this
We must add dedicated documentation for this feature and API surface area, as well as write blog posts on migrating to this new setup.
Unresolved questions
1. How do we let users set default meta tags that should be rendered on every page but not in the
siteMetadata
?For example,
<meta name="twitter:site">
should be set to the site's twitter username on every page (e.g. "@dicksflowers"). Sure, we could add fields for every single potential meta tag (twitterUsername
) but that feels unergonomic as there are a lot of meta tags.Instead, I think we should add a way for users to specify additional "default" meta tags somehow. Maybe we should tell them to render the
<Head>
component in thegatsby-browser/ssr.js
wrapPageElement
method like so?2. Do we enforce that users pass the basic information via props, rather than rendering the meta tags for it?
Even though it would work, we don't want users to render the basic information meta tags themselves as they could forget some parts of the combination:
Rather, they should pass the basic information via props so it turns into the correct set of meta tags automatically:
Do we want to enforce that by warning/erroring in the console if they provide meta tags that are also in the basic information?
3. Do we want to fork react-helmet?
We could fork react-helmet to avoid blocking users by upstream bugs we cannot fix. However, we might not be able to benefit from upstream fixes then as our code could diverge from theirs.
4. Do we want to go for the "Transparent react-helmet wrapper with Meta components" API?
See above.
Beta Was this translation helpful? Give feedback.
All reactions