Proposal :: Meta Tags :: Only render a tag when content is available #218
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
I'd like to propose an use case in which someone adds the base SEO component but doesn't provide any content to render the tags.
This scenario might be interesting for those who wish to leverage the default JSON LD functionality but have a need to provide the remaining tags in any other way.
For this, I'm suggesting that the component skip every tag without content.
Reasoning / Justification
Empty tags are the same as no tags. Because of that, its better to skip rendering a tag that has no content in it, even for those whose content might be inferred from context.
Also, several SO and device specific tags are not contemplated in the base component, a behavior I agree with. Those tags are usually defined using a custom call to Head since there are several icon sizes and tags for social networks that might not be interesting for every project.
Finally, by avoiding the need to duplicate a tag, the final HTML doc size will be reduced.
Code explanation
Overall, I've just added a check before rendering any tag. The tags are now unordered, but the order of elements in the head tag is (usually) not meaningful.
I've also removed most of the Twitter tags since they have fallbacks relying on OpenGraph tags. I've also added the
og:url
canonical tag. As a final change I've added a select for Twitter Card, but it might be best to just have a separate component specifically for Twitter or any other social network/service.Since this is mostly a proposal, theres still the need to go over the logic on displaying the previews.
Wrapping up
I've used this favicon generator to implement the custom tags on Zee.Dog, and it comes packed with all the files and tags needed. Creating a custom SEO component was then much easier, but still, I wanted to have access to the default JSON LD. Allowing the base SEO component to render only the existing content enabled me to create custom SEO components while still leveraging the LD implementation without the need to duplicate tags.