How to add Tags to your Gatsby Blog
Tags are an essential part of Web 2.0

Originally published at https://fek.io.
Summary
The webpage provides a guide on implementing tags in a Gatsby blog, detailing the importance of proper FrontMatter formatting for tags, and how to query and render them within Gatsby using GraphQL.
Abstract
The article titled "How to add Tags to your Gatsby Blog" emphasizes the significance of tags as a Web 2.0 element that enhances content organization and SEO. It outlines the process of setting up tags in Gatsby by correctly defining them in the FrontMatter section of markdown files. The author explains the common mistake of using a string instead of an array for multiple tags and provides examples of proper YAML syntax for defining tags. The article also demonstrates how to query these tags in GraphQL and handle cases where posts may not have tags assigned. It concludes with the author's intention to create a template for displaying posts by tag, further emphasizing the utility of tags for content discovery and SEO.
Opinions
Tags are an essential part of Web 2.0

Originally published at https://fek.io.
I moved my site to Gatsby a couple of years ago. I have used several different blogging engines in the past including Orchard for .NET and BlogCFC. Both of these blogging engines support adding tags to your posts.
When I setup my blog I never bothered setting up tags because I could never get them to show up in my GraphQL queries. Going through my build logs today I discovered what the problem was, I was using two different types of tags in my FrontMatter.
FrontMatter is a header that is placed at the beginning of your markdown files. FrontMatter usually contains properties for title and date, but you can also include other meta information that can be used by GraphQL when you are querying your markdown files.
---title: "My Title Here!"date: 2021-06-21---Usually in my markdown files I include a category, description and cover_image field. All of these fields are generally strings, a fairly simple type. For the markdown files I created, I did have a tag property for some of my posts, but I was not setting the value properly and consistently in my FrontMatter.
Since you can have more than one tag per post, it really is a list. In FrontMatter, if you have a property that requires a list, that is really an array.
FrontMatter is based on YAML or YML. If you want to express an array of data in FrontMatter, there are two ways you can do it. One way is to put your values inside of square brackets. This can look like the following example.
---title: "My Title Here!"date: 2021-06-21tags: ["Gatsby", "JavaScript", "GraphQL"]---Another way of expressing this would be to add a new line and a dash for each item in your array.
---title: "My Title Here!"date: 2021-06-21tags: - "Gatsby" - "JavaScript" - "GraphQL"---Some of my posts had all of my tags inside of a single string. This was causing a conflict in the ‘gatsby-transformer-remark’ plugin when it was trying to resolve what type to render the tag.
---title: "My Title Here!"date: 2021-06-21tags: "Gatsby JavaScript GraphQL"---Once I had my tags changed to the correct type, I was able to query them in my Graph iql query tool. Here is the query I used for listing the posts I had with the tags included. You can find it in the edges.node.frontmatter.tags.
In the data being fed by the query into the template component, I can’t count on always having a tag because many of my previous posts did not have any tags. So I set up a default of an empty array when that is the case.
This creates an array that I can use in my render function to display the tags in my post.
This checks to see if I have any tags, then uses && operator to display my tags. In React you can do this to conditionally display something in your render method.
Gatsby actually does a pretty good job of documenting how to query FrontMatter in your Gatsby site. Check out their page on adding tags and categories to blog posts.
Tagging is considered an essential feature in Web 2.0. It gives users instant insights into what king of content is in your post, and it can also help with SEO.
I plan on adding a template to display all posts that match a particular tag listed in my post in an upcoming release.
asierr.devElevating Web Performance for a Seamless User Experience
Xiuer OldJavascript developers, rejoice! We’ve got new tools in our toolkit. Let’s explore the awesome additions to the Set object, making set…
Giorgos MyrianthousManaging Python project dependencies and metadata with Poetry