npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-headless-yoast

v0.2.2

Published

A React component that handles Yoast SEO in a Headless WordPress site.

Downloads

2,015

Readme

react-headless-yoast

Version

A React component that handles Yoast SEO in a Headless WordPress site.

The primary objective of this library is to make it easier to integrate Yoast SEO for WordPress in a headless React frontend.

Table of Contents

Current Status

This project is active and maintained. Feel free to submit issues and PRs!

  • Most recent build is linted according to .eslintrc.js

Installation

Using npm:

npm i react-headless-yoast

NOTE: add -S if you are using npm < 5.0.0

Using yarn:

yarn add react-headless-yoast

Getting Started

The recommended way to use react-headless-yoast is by using WPGraphQL combined with Yoast and WPGraphQL For Yoast to get SEO metadata related to Posts, Pages, and custom content types on your headless WordPress site. react-headless-yoast is setup to work well with the API definition that WPGraphQL For Yoast uses.

NOTE: react-headless-yoast does not provide a way of putting meta tags into the head element on your site. This is deliberate, as depending upon what frontend libraries you are using (e.g. Next.js, Gatsby, react-head, etc) you may have a specific way you want to add tags to the head.

Example WPGraphQL Query

In order to get the most out of this library you will want to craft a WPGraphQL query that includes the seo object published by WPGraphQL For Yoast. This partial query works on pages, posts, and other custom content types, so add it to your query for pages/posts as a returned property.

seo {
  breadcrumbs {
    text
    url
  }
  schema {
    articleType
    pageType
    raw
  }
  canonical
  cornerstone
  focuskw
  metaDesc
  metaKeywords
  metaRobotsNofollow
  metaRobotsNoindex
  opengraphAuthor
  opengraphDescription
  opengraphImage {
    altText
    srcSet
    sourceUrl
  }
  opengraphModifiedTime
  opengraphPublishedTime
  opengraphPublisher
  opengraphSiteName
  opengraphTitle
  opengraphType
  opengraphUrl
  readingTime
  title
  twitterDescription
  twitterImage {
    altText
    srcSet
    sourceUrl
  }
  twitterTitle
}

You will also want to get the SiteSchema for your WordPress site using the following query:

{
  seo {
    schema {
      companyName
      companyLogo {
        altText
        sourceUrl
        srcSet
      }
      inLanguage
      siteName
      siteUrl
    }
  }
}

Using The SEO Component

The <Seo /> component is the primary export from react-headless-seo. It will render title, meta, and script tags related to the SEO data you pass in as props. This means you will have to use a component designed to render those elements into the head tag for your site (i.e. something like react-head). The following code uses next/head, but you can use whatever head component you want:

import Seo, { SeoProps } from 'react-headless-yoast';
import Head from 'next/head';

export function MyComponent({
  seo,
}: {
  seo: Omit<SeoProps, 'MetaRenderElement'>;
}) {
  return (
    <Seo
      pageSchema={seo.pageSchema}
      siteSchema={seo.siteSchema}
      page={seo.page ?? { title: 'My WordPress Site' }}
      MetaRenderElement={Head}
      meta={
        <>
          <link
            rel="apple-touch-icon"
            sizes="180x180"
            href="/images/favicon/apple-touch-icon.png"
          />
          ,
          <link
            rel="icon"
            type="image/png"
            sizes="32x32"
            href="/images/favicon/favicon-32x32.png"
          />
          ,
          <link
            rel="icon"
            type="image/png"
            sizes="16x16"
            href="/images/favicon/favicon-16x16.png"
          />
          ,
          <link rel="manifest" href="/images/favicon/site.webmanifest" />,
          <link rel="shortcut icon" href="/images/favicon/favicon.ico" />,
          <meta name="msapplication-TileColor" content="#da532c" />,
          <meta
            name="msapplication-config"
            content="/images/favicon/browserconfig.xml"
          />
          ,
          <meta name="theme-color" content="#ffffff" />,
        </>
      }
    />
  );
}

The code above uses the SeoProps type to fill out the props for <Seo /> with the correct values. The type is defined in react-headless-yoast so you can see what it expects.

NOTE: The example above also passes in favicon meta tags using the optional meta prop. You can use this to put additional tags into the Head.

How can I Contribute?

react-headless-seo is open to contributions that improve the core functionality of the library while sticking to the primary objective listed above.

Test

Before you share your improvement with the world, make sure you add tests to validate your contribution. Testing is done using jest.

Make A Pull Request

Once you've tested your contribution, submit a pull request. Make sure to squash your commits first!.