Sitemaps in Headless WordPress with Faust.js

Francis Agulto Avatar

·

In this article, I will discuss Sitemaps in Headless WordPress using the open-source framework for Headless WordPress – Faust.js.

XML Sitemaps

An XML sitemap is a list of URLs that you want to be publicly available. It helps search engines crawl your website by giving them an exact map of all your content. There is also metadata you can pull into your sitemap which is listed in the sitemaps protocol such as when a page was last modified, video information, and images.

Below is an image of a traditional WordPress XML sitemap URL. The default is your WordPress URL with the added path /wp-sitemap.xml and this is baked into WordPress core from version 5 and above:

XML Sitemap Benefits

  • Faster Indexing: XML sitemaps will communicate with search engines. You can submit your sitemap into the Google Search Console and that helps get pages on your site indexed much faster with search engines. This increases your rank in search engine page results.
  • Automatic Update Notification: Google will receive automatic notifications when you publish new content or modify existing content.
  • Categorization of Your Content: Allows Google to know which pages go under which category so there is no guesswork.

XML Sitemaps in the Faust.js framework

Sitemaps can be complicated to create and process when you are using WordPress in a headless architecture. Thankfully, the Faust.js team at WP Engine created a feature within the framework that makes it work within a single file within the pages directory.

Setup

The first step is to install the Faust.js framework with the following command in your terminal:

npx create-next-app \
    -e https://github.com/wpengine/faustjs/tree/main \
    --example-path examples/next/faustwp-getting-started \
    --use-npm
Code language: PHP (php)

Copy the sample environment template:

cp .env.local.sample .env.local
Code language: CSS (css)

Connect your WordPress site:

Navigate to your .env.local file in the root of your project and paste your WP URL as the value:

# Your WordPress site URL
NEXT_PUBLIC_WORDPRESS_URL=https://headlessfw.wpengine.com
Code language: PHP (php)

Setting Permalinks and Reading Visibility:

Set permalinks In WP Admin >> Settings >> Permalinks to: /posts/%postname%/

Set reading settings in WP Admin >> Settings >> Reading >> Search Engine Visibility uncheck box

Faust.js

Next, navigate back to your Faust.js app, and in the root of the project, go to the /pages directory. Within the pages directory, create a file and name it sitemap.xml.js.

In the sitemap.xml.js you just created, copy and paste this code block:

import { getSitemapProps } from '@faustwp/core';

export default function Sitemap() {}

export function getServerSideProps(ctx) {
  return getSitemapProps(ctx, {
    frontendUrl: process.env.FRONTEND_URL,
  });
}

Code language: JavaScript (javascript)

In this file, the first thing that is happening is the importing of the sitemap properties on the Faust.js server framework as well as importing the Server Side properties context from Next.js.

Next, we export a default function that is the Sitemap with an empty object. Once that is done, below is a function in Next.js that allows for server-side rendering. Within this function, we pass in the server-side props context and then return a config object with the sitemap props.

The config object requires only one property and that is your frontendUrl.

Now that I have this file set up in my pages directory within Faust, I can run npm run dev to start the dev server and I am able to access my WordPress content dynamically to my Headless site. Stoked!! 🎉

Defining Next.js Pages for Sitemaps

The last thing we need to account for in our sitemap is the Next.js pages. Since we are using Headless WordPress, the front-end URLs need to be on the sitemap as well. Within the same sitemap.xml.js file, simply add a pages property array and place the relative paths of Next.js in the objects within the array like so:

import { getSitemapProps } from '@faustwp/core';
export default function Sitemap() {}

export function getServerSideProps(ctx) {
  return getSitemapProps(ctx, {
    frontendUrl: process.env.FRONTEND_URL,
    pages: [
      {
        path: '/about',
        changefreq: 'monthly',
      },
      {
        path: '/',
      },
    ],
  });
}

Code language: JavaScript (javascript)

Running my development server again and visiting the sitemap URL, I now have my WordPress content and my Next.js content in Faust-pages:

Super Jamstoked!! âš¡ of the many benefits of using a framework like Faust.js is the developer toil it removes from your plate, especially in Headless WordPress. Features like sitemaps should just work simply out of the box with the ability to account for your URLs from WordPress and your front-end without too much heavy lifting.

Additional Features

Please visit the new Faust.js docs for features I did not discuss in this article such as ignoring paths in sitemaps and using them with Yoast.

Done! What Next?

If Faust.js is not your thing, I created another blog post in relation to this using only Next.js and Headless WordPress!! Check it out here!