Skip to content

Sitemap Integration Crash

  1. Root Cause & Error Mechanics

During a production build (npm run build or pnpm run build), the @astrojs/sitemap integration attempts to generate a sitemap.xml file by inspecting all generated pages and content collections.

  • The Crash Signature: The build fails with the error Cannot read properties of undefined (reading 'reduce') originating from node_modules/@astrojs/sitemap/dist/index.js.
  • The Underlying Bug: When a content collection directory defined in src/content/config.ts or referenced in dynamic routes (e.g., src/pages/store/[...page].astro) is completely missing or empty on disk (such as src/content/store/), the sitemap plugin receives an undefined array. Attempting to execute .reduce() on this undefined data set triggers an unhandled JavaScript exception, exiting the build process with code 1.

  1. Context Within Astro Production Build Failures

This specific error highlights several broader characteristics of Astro build failures observed in the sources:

  • Development vs. Production Discrepancy: The site runs without issues during local development (npm run dev) because the sitemap plugin is only invoked during static site compilation (npm run build inside Stage 1 of the Dockerfile).
  • Config Syntax & Import Mismatches: Build failures also occur if sitemap() is included in the integrations array inside astro.config.mjs without being properly imported at the top of the file (import sitemap from '@astrojs/sitemap') [82–84].
  • Related Build Warnings: A related build issue noted during static route generation is the RSS API route warning ([WARN] [router] No API Route handler exists for method "GET"), caused by using lowercase export async function get(...) instead of capitalized GET(...) in src/pages/rss.xml.js.

Empty Content Collections

  1. How Empty Content Collections Occur

During initial site cleanup or rebranding, developers often delete demo Markdown articles or products (such as cleaning out default posts or the template's demo store items).

However, if the underlying content collection folder (e.g., src/content/store/) is left completely empty or removed entirely while the site still contains dynamic page routes (src/pages/store/[...page].astro) or content schemas (src/content/config.ts), Astro enters an inconsistent state.


  1. The Chain Reaction to Build Failure

When executing npm run build or pnpm run build (such as during Stage 1 of the Docker build), an empty collection triggers a two-stage failure sequence:

  1. Route Generation Warning: During static route compilation, Astro flags the missing dataset:
  2. (At this point, static HTML page generation continues temporarily).
  3. Fatal Integration Crash (astro:build:done): Once page compilation finishes, post-build integrations execute. The @astrojs/sitemap integration attempts to generate sitemap.xml by mapping over all site pages and collections. When it attempts to iterate over the unpopulated store collection, it receives an undefined dataset and crashes:
  4. This unhandled JavaScript exception terminates the build with exit code 1, halting the entire Docker build stage.

  1. Why It Catches Developers Off Guard

This failure mode is particularly deceptive because local development (npm run dev) works perfectly fine. In development mode, Astro spins up a local Vite dev server for hot-module reloading and skips running production hooks like @astrojs/sitemap. The error only surfaces when running a production static build (npm run build) or inside containerized CI/CD pipelines.


  1. Resolution Strategies in the Sources

The sources outline two distinct ways to resolve empty collection failures:

  • Populate the Collection (If Keeping the Feature): Create the missing collection directory path (e.g., src/content/store/) and add at least one valid Markdown (.md) or JSON file so @astrojs/sitemap receives a valid array to process.
  • Clean Up Routes & Integrations (If Disabling the Feature):
    1. Delete unused route folders (e.g., delete src/pages/store/).
    2. Remove sitemap() from the integrations array and remove import sitemap from '@astrojs/sitemap' in astro.config.mjs.

Once applied, npm run build succeeds cleanly, compiling all 23 static pages into the output directory (/dist).