Skip To Main Content

getSiteMapProps

getSitemapProps is a server side helper function that facilitates the proxying of sitemaps from your WordPress site to your Faust frontend. It is a function that is returned from inside of a getServerSideProps Next.js function.

Usage

The following code must be added to a sitemap.xml.js or sitemap.xml.tsx file in your pages directory:

// pages/sitemap.xml.js
import { getSitemapProps } from '@faustwp/core';

export default function Sitemap() {}

export function getServerSideProps(ctx) {
  return getSitemapProps(ctx, {
    frontendUrl: process.env.FRONTEND_URL, <em>// Define your frontend URL as an env var</em>
  });
}
Code language: JavaScript (javascript)

Configuration

Below is the getSitemapProps config object defined as a TypeScript type:

type GetSitemapPropsConfig = {
  /**
   * REQUIRED: The URL of your Faust site.
   *
   * @example http://localhost:3000
   * @example https://my-site.com
   */
  frontendUrl: string;

  /**
   * A list of relative URLs to ignore when proxying sitemaps.
   * Wild cards can be used to exclude certain types of sitemaps.
   *
   * @example ["/author-sitemap.xml"]
   * @example ["/sitemap-tags-*"]
   */
  sitemapPathsToIgnore?: string[];

  /**
   * Next.js pages you want included in you sitemap. When provided, an index
   * will be created specifically for these pages.
   *
   * @example [ { path: "/about" } ]
   */
  pages?: NextJSPage[];
};
Code language: PHP (php)

And the shape of a NextJSPage:

type NextJSPage = {
  <em>/**</em>
<em>   * The relative URL of the Next.js page.</em>
<em>   *</em>
<em>   * @example /about</em>
<em>   */</em>
  path: string;

  lastmod?: string;

  changefreq?:
    | 'always'
    | 'hourly'
    | 'daily'
    | 'weekly'
    | 'monthly'
    | 'yearly'
    | 'never';

  priority?: number;
};
Code language: PHP (php)

The following example resolves a sitemap page using a combination of configuration parameters:

// sitemap.xml.js
import { getSitemapProps } from '@faustwp/core';

export default function Sitemap() {}

export function getServerSideProps(ctx) {
  return getSitemapProps(ctx, {
    frontendUrl: 'http://localhost:3000',
    sitemapPathsToIgnore: ['/author-sitemap.xml'],
    pages: [{
      path: '/example',
      changefreq: "daily",
      priority: 0.7
    }]
  });
}

Code language: JavaScript (javascript)