Gatsby Plugin Sitemap is a Gatsby plugin that automatically generates XML sitemaps for your site during the production build process. It creates a sitemap-index.xml file at the site root and individual sitemap files for every 45,000 URLs, following the sitemaps.org protocol.
npm install gatsby-plugin-sitemap// Plugin is configured in gatsby-config.js - no direct imports needed
// The plugin operates entirely through Gatsby's plugin system
// Utilities are internal and not exported for direct import// gatsby-config.js
module.exports = {
siteMetadata: {
siteUrl: 'https://www.example.com',
},
plugins: ['gatsby-plugin-sitemap']
}Gatsby Plugin Sitemap operates through Gatsby's plugin lifecycle hooks:
onPostBuild hook to generate sitemaps after all pages are createdonRenderBody hook to optionally add sitemap link to HTML headsitemap library for XML generation and minimatch for glob pattern filteringComplete configuration options for customizing sitemap generation behavior, including output location, entry limits, custom queries, and filtering options.
// Plugin options interface
interface PluginOptions {
output?: string;
createLinkInHead?: boolean;
entryLimit?: number;
query?: string;
excludes?: string[];
resolveSiteUrl?: (data: any) => string | Promise<string>;
resolvePagePath?: (page: any) => string;
resolvePages?: (data: any) => any[] | Promise<any[]>;
filterPages?: (page: any, excludedRoute: any, tools: FilterTools) => boolean;
serialize?: (page: any, tools: SerializeTools) => any | Promise<any>;
}Implementation of Gatsby's plugin lifecycle hooks for sitemap generation and HTML head integration.
// Main build hook
exports.onPostBuild = async (context, options) => Promise<void>;
// HTML head integration hook
exports.onRenderBody = (context, pluginOptions) => void;
// Plugin options validation schema
exports.pluginOptionsSchema = (joi) => JoiSchema;Helper functions for site URL resolution, page processing, filtering, and serialization that can be customized or used independently.
// Core resolver functions
function resolveSiteUrl(data: any): string;
function resolvePagePath(page: any): string;
function resolvePages(data: any): any[];
// Filtering and processing
function defaultFilterPages(page: any, excludedRoute: string, tools: FilterTools): boolean;
function pageFilter(options: PageFilterOptions): { filteredPages: any[], messages: string[] };
function serialize(page: any, tools: SerializeTools): any;interface FilterTools {
minimatch: (path: string, pattern: string) => boolean;
withoutTrailingSlash: (path: string) => string;
resolvePagePath: (page: any) => string;
}
interface SerializeTools {
resolvePagePath: (page: any) => string;
}
interface PageFilterOptions {
allPages: any[];
filterPages: (page: any, excludedRoute: any, tools: FilterTools) => boolean;
excludes: any[];
}