React, structured data, and SEO

If you have a website, making it discoverable is of paramount importance. In this guide, we’ll show you how to add structured data to your site.

Adding structured data helps search engines such as Google understand your content and get it in front of more eyeballs. We’ll illustrate how this works by building a simple React app that incorporates structured data.

What is structured data?

Google, DuckDuckGo, and other search engines are proficient at understanding the content of websites. However, scraping HTML is not a particularly reliable way to categorize content. HTML is all about presentation and it can be structured in a variety of ways.

To make it easier for search engines to digest the content of your site, you can embed a standardized format known as structured data within a page. This standardized format allows you to explicitly declare the type of content the page contains.

Let’s say you’ve written an article. You can reliably state in a language that Google understands, “This page is an article that has this title, a description, and and image and was published on this date.”

There are hundreds of types of structured data available. You can read about all of them in depth at Schema.org, which is maintained by representatives of the search engine community.

There are many types of structured data available, from popular options to those that are more niche. For instance, you’re likely to use Article a great deal more often than, perhaps, MolecularEntity.

Just like there are many different types of structured data, there is a wide range of formats you can use to provide it, including JSON-LD, Microdata, and RDFa. Google explicitly prefers JSON-LD, so that’s what we’ll focus on on this tutorial.

JSON-LD is effectively a rending of a piece of JSON inside a script tag with the custom type of application/ld+json. For example:

<script type="application/ld+json">
{
    "@context": "https://schema.org/",
    "@type": "Recipe",
    "name": "Chocolate Brownie",
    "author": {
        "@type": "Person",
        "name": "John Reilly"
    },
    "datePublished": "2014-09-01",
    "description": "The most amazing chocolate brownie recipe",
    "prepTime": "PT60M"
}
</script>

Structured data in action

While structured data is helpful for search engines in general, it can also impact the way your content is rendered inside search results. For instance, let’s search for “best brownie recipe” in Google and see what shows up:

Rich Text Results in Google

When you look at the screenshot above, you’ll notice that at the top of the list (before the main search results) there’s a carousel that shows various brownie recipe links with dedicated pictures, titles and descriptions. Where did this come from? The answer, unsurprisingly, is structured data.

If we click on the first link, we’re taken to the recipe in question. Looking at the HTML of that page, we find a number of JSON-LD sections:

Structured Data
Screenshot of JSON-LD sections in the BBC Good Food website

If we grab the content of one JSON-LD section and paste it into the devtools console, it becomes much easier to read:

Single Structured Data as JSON
Screenshot of JSON-LD section transformed into a JavaScript Object Literal

If we look at the @type property, we can see it’s a "Recipe". This means it’s an example of the Recipe schema.

If we look further at the headline property, it reads "Best ever chocolate brownies recipe". That matches up with headline displayed in the search results.

Now we have a sense of what search engines are using to categorize the page and understand exactly what is powering the carousel in the Google search results.

Incidentally, there’s a special name for this carousel; it’s called a rich result. A rich result is a search result that the engine singles out for special treatment when it is displayed. Google provides a Rich Results Test tool that allows you to validate whether your site provides structured data that is eligible to be featured in rich results. More on this later.

Adding structured data to a website

To show how structured data works in practice, let’s create a React app and add structured data to it.

In the console, execute the following command:

npx create-react-app my-app

We now have a simple React app that consists of a single page. Let’s replace the content of the existing App.js file with the following:

//@ts-check
import "./App.css";

function App() {
  // https://schema.org/Article
  const articleStructuredData = {
    "@context": "https://schema.org",
    "@type": "Article",
    headline: "Structured data for you",
    description: "This is an article that demonstrates structured data.",
    image: "https://upload.wikimedia.org/wikipedia/commons/4/40/JSON-LD.svg",
    datePublished: new Date("2021-09-04T09:25:01.340Z").toISOString(),
    author: {
      "@type": "Person",
      name: "John Reilly",
      url: "https://twitter.com/johnny_reilly",
    },
  };

  return (
    <div className="App">
      <script type="application/ld+json">
        {JSON.stringify(articleStructuredData)}
      </script>

      <h1>{articleStructuredData.headline}</h1>
      <h3>
        by{" "}
        <a href={articleStructuredData.author.url}>
          {articleStructuredData.author.name}
        </a>{" "}
        on {articleStructuredData.datePublished}
      </h3>

      <img
        style={{ width: "5em" }}
        alt="https://json-ld.org/ - Website content released under a Creative Commons CC0 Public Domain Dedication except where an alternate is specified., CC0, via Wikimedia Commons"
        src={articleStructuredData.image}
      />

      <p>{articleStructuredData.description}</p>

      <p>Take a look at the source of this page and find the JSON-LD!</p>
    </div>
  );
}

export default App;

If we look at the code above, we can see we’re creating a JavaScript object literal named articleStructuredData that contains the data of an Article. articleStructuredData is then used to do two things:

  1. Contribute to the content of the page
  2. Render a JSON-LD script tag, <script type="application/ld+json">, which is populated by calling JSON.stringify(articleStructuredData).

When we run our site locally with npm start, we see a simple article site that looks like this:

Example Article With Structured Data

Now let’s see if it supports structured data in the way we hope.

Using the Rich Results Test

There are two two ways to test your structured data using the Rich Results Test tool: by either providing a URL or providing code. In our case, we don’t have a public-facing URL, so we’re going to use the HTML that React is rendering.

In devtools, we’ll use the copy outerHTML feature to grab the HTML, then we’ll paste it into Rich Results:

Google's Rich Results Test Tool

Hit the TEST CODE button and you should see results that look like this:

Google's Rich Results Test Tool

So we’ve been successful in building a website that renders structured data. More than that, we’re doing it in a way that we know Google will recognize and can use to render rich results in search. That’s a really useful way to drive traffic to a website.

Further reading

In this tutorial, we demonstrated what it looks like to create an Article. Google has some great resources on other types that it supports and prioritizes for rich results, which should help you build the structured data you need to amplify your content.

The post React, structured data, and SEO appeared first on LogRocket Blog.

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

React, structured data, and SEO

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.