gatsbyjs/gatsby · error · Error

You've passed something other than string to the exclude arr

Error message

You've passed something other than string to the exclude array. This is supported, but you'll have to write a custom filter function.
Ignoring the input for now: ${JSON.stringify(excludedRoute, null, 2)}
https://www.gatsbyjs.com/plugins/gatsby-plugin-sitemap/#api-reference
      

What it means

gatsby-plugin-sitemap's defaultFilterPages applies each entry of the `exclude` option against page URLs via minimatch. The default implementation only handles string entries (glob patterns). If an entry is not a string (a RegExp, function, object, or number), it throws and points the user to the API reference to supply a custom `filterPages` function instead. The message says non-string entries are 'supported' only via a custom filter, not via the default.

Source

Thrown at packages/gatsby-plugin-sitemap/src/internals.js:110

 *
 * This allows filtering any data in any way.
 *
 * This Function is executed via allPages.filter((page) => !excludes.some((excludedRoute) => thisFunc(page, ecludedRoute, tools)))
 * allPages is the results of the resolvePages
 *
 * @param {object} page
 * @param {string} excludedRoute - Array from plugin config `options.exclude`
 * @param {object} tools - contains required tools for filtering
 *
 * @returns {boolean}
 */
export function defaultFilterPages(
  page,
  excludedRoute,
  { minimatch, withoutTrailingSlash, resolvePagePath }
) {
  if (typeof excludedRoute !== `string`) {
    throw new Error(
      `You've passed something other than string to the exclude array. This is supported, but you'll have to write a custom filter function.
Ignoring the input for now: ${JSON.stringify(excludedRoute, null, 2)}
https://www.gatsbyjs.com/plugins/gatsby-plugin-sitemap/#api-reference
      `
    )
  }

  // Minimatch is always scary without an example
  // TODO add example
  return minimatch(
    withoutTrailingSlash(resolvePagePath(page)),
    withoutTrailingSlash(excludedRoute)
  )
}

/**
 * @name serialize
 *

View on GitHub (pinned to 8b06340921)

Solutions

  1. Convert RegExp excludes to minimatch glob strings, e.g. use '/draft/**' instead of /\/draft\//.
  2. If you need RegExp/function logic, provide a custom options.filterPages function and do the matching yourself.
  3. Ensure every element of the exclude array is a string when using the default filter.

Example fix

// before
{
  resolve: `gatsby-plugin-sitemap`,
  options: { exclude: [`/secret`, /\?lang=/] }
}

// after (glob string)
{
  resolve: `gatsby-plugin-sitemap`,
  options: { exclude: [`/secret`, `/*?lang=*`] }
}
// or custom filter for regex logic
options: { filterPages: (page, excludedRoutes) => excludedRoutes.every(...) }
Defensive patterns

Strategy: type-guard

Validate before calling

function normalizeExcludes(exclude) {
  const arr = Array.isArray(exclude) ? exclude : [];
  const bad = arr.filter(e => typeof e !== 'string');
  if (bad.length) {
    throw new Error('gatsby-plugin-sitemap exclude entries must be strings; ' +
      'use a custom filterPages for RegExp/function logic.');
  }
  return arr;
}

Type guard

function isStringArray(arr) {
  return Array.isArray(arr) && arr.every(e => typeof e === 'string');
}

Prevention

When it happens

Trigger: Configuring the plugin with options.exclude containing a RegExp (e.g. /\/draft\//), a function, an object, or any non-string, e.g. { resolve: 'gatsby-plugin-sitemap', options: { exclude: ['/secret', /\?lang=/] } }.

Common situations: Wanting regex exclusion (common need) and assuming the default filter accepts RegExp; migrating from another sitemap plugin that allowed functions; passing an object like { path: '/x' } by mistake.

Related errors


AI-assisted analysis of gatsbyjs/gatsby@8b06340921 (2026-08-13). Data as JSON: /api/errors/34948fb4ca9f6048. Report an issue: GitHub.