diff --git a/.changeset/long-humans-type.md b/.changeset/long-humans-type.md new file mode 100644 index 000000000..68f1c16a6 --- /dev/null +++ b/.changeset/long-humans-type.md @@ -0,0 +1,19 @@ +--- +'@faustwp/core': minor +--- + +- Added support for configuring a custom sitemap index path via the `sitemapIndexPath` option in `getSitemapProps`, enhancing compatibility with plugins like RankMath that modify the default sitemap path. + + ```javascript + import { getSitemapProps } from '@faustwp/core'; + + export default function Sitemap() {} + + export function getServerSideProps(ctx) { + return getSitemapProps(ctx, { + sitemapIndexPath: '/sitemap_index.xml', // RankMath changes the default sitemap path to this + frontendUrl: process.env.NEXT_PUBLIC_SITE_URL, + sitemapPathsToIgnore: ['/wp-sitemap-users-*'], + }); + } + ``` diff --git a/examples/next/block-support/.env.local.sample b/examples/next/block-support/.env.local.sample index dc631ae73..46782dd61 100644 --- a/examples/next/block-support/.env.local.sample +++ b/examples/next/block-support/.env.local.sample @@ -3,3 +3,6 @@ NEXT_PUBLIC_WORDPRESS_URL=https://faustexample.wpengine.com # Plugin secret found in WordPress Settings->Faust # FAUST_SECRET_KEY=YOUR_PLUGIN_SECRET + +# Needed for sitemaps (no naming requirements for this, NEXT_PUBLIC_SITE_URL is just an example) +NEXT_PUBLIC_SITE_URL=http://localhost:3000 diff --git a/examples/next/block-support/pages/sitemap.xml.js b/examples/next/block-support/pages/sitemap.xml.js new file mode 100644 index 000000000..2fba496a4 --- /dev/null +++ b/examples/next/block-support/pages/sitemap.xml.js @@ -0,0 +1,11 @@ +import { getSitemapProps } from '@faustwp/core'; + +export default function Sitemap() {} + +export function getServerSideProps(ctx) { + return getSitemapProps(ctx, { + // sitemapIndexPath: '/sitemap_index.xml', // Update the sitemap path if a WordPress plugin is used that changes the default path. + frontendUrl: process.env.NEXT_PUBLIC_SITE_URL, + sitemapPathsToIgnore: ['/wp-sitemap-users-*'], + }); +} diff --git a/packages/faustwp-core/src/server/sitemaps/createSitemaps.ts b/packages/faustwp-core/src/server/sitemaps/createSitemaps.ts index 90ff37f8a..2fd92187b 100644 --- a/packages/faustwp-core/src/server/sitemaps/createSitemaps.ts +++ b/packages/faustwp-core/src/server/sitemaps/createSitemaps.ts @@ -56,7 +56,7 @@ export async function createRootSitemapIndex( req: NextRequest | IncomingMessage, config: GetSitemapPropsConfig, ): Promise { - const { pages, sitemapPathsToIgnore, frontendUrl } = config; + const { pages, sitemapPathsToIgnore, frontendUrl, sitemapIndexPath } = config; if (!req.url) { throw new Error('Request object must have URL'); @@ -66,7 +66,10 @@ export async function createRootSitemapIndex( // fetch sitemap from WP const trimmedWpUrl = trim(getWpUrl(), '/'); const trimmedFrontendUrl = trim(frontendUrl, '/'); - const trimmedSitemapIndexPath = trim(SITEMAP_INDEX_PATH, '/'); + const trimmedSitemapIndexPath = trim( + sitemapIndexPath || SITEMAP_INDEX_PATH, + '/', + ); const wpSitemapUrl = `${trimmedWpUrl}/${trimmedSitemapIndexPath}`; let sitemaps: SitemapSchemaSitemapElement[] = []; @@ -90,6 +93,15 @@ export async function createRootSitemapIndex( // Don't proxy the sitemap index if the response was not ok. if (!res.ok) { + console.error( + `Failed to fetch sitemap index from WordPress at ${wpSitemapUrl}`, + ); + console.error('Possible solutions:'); + console.error( + '- Check that sitemapIndexPath is correct in the options passed to getSitemapProps. (some WordPress plugins change the default sitemap path)', + ); + console.error(`- Consider flushing permalinks in WordPress.`); + return undefined; } diff --git a/packages/faustwp-core/src/server/sitemaps/getSitemapProps.tsx b/packages/faustwp-core/src/server/sitemaps/getSitemapProps.tsx index b0f329b9a..86d7e1d8b 100644 --- a/packages/faustwp-core/src/server/sitemaps/getSitemapProps.tsx +++ b/packages/faustwp-core/src/server/sitemaps/getSitemapProps.tsx @@ -43,6 +43,10 @@ export type GetSitemapPropsConfig = { * A list of pathnames to ignore when proxying sitemaps. */ sitemapPathsToIgnore?: string[]; + /** + * The path to the sitemap index file in WordPress. + */ + sitemapIndexPath?: string; /** * Next.js pages you want included in you sitemap. When provided, an index * will be created specifically for these pages.