Sitemap Integration Crash
- 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 fromnode_modules/@astrojs/sitemap/dist/index.js. - The Underlying Bug: When a content collection directory defined in
src/content/config.tsor referenced in dynamic routes (e.g.,src/pages/store/[...page].astro) is completely missing or empty on disk (such assrc/content/store/), the sitemap plugin receives anundefinedarray. Attempting to execute.reduce()on this undefined data set triggers an unhandled JavaScript exception, exiting the build process with code1.
- 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 buildinside Stage 1 of the Dockerfile). - Config Syntax & Import Mismatches: Build failures also occur if
sitemap()is included in theintegrationsarray insideastro.config.mjswithout 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 lowercaseexport async function get(...)instead of capitalizedGET(...)insrc/pages/rss.xml.js.
Empty Content Collections
- 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.
- 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:
- Route Generation Warning: During static route compilation, Astro flags the missing dataset:
- (At this point, static HTML page generation continues temporarily).
- Fatal Integration Crash (astro:build:done): Once page compilation finishes, post-build integrations execute. The
@astrojs/sitemapintegration attempts to generatesitemap.xmlby mapping over all site pages and collections. When it attempts to iterate over the unpopulatedstorecollection, it receives anundefineddataset and crashes: - This unhandled JavaScript exception terminates the build with exit code
1, halting the entire Docker build stage.
- 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.
- 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/sitemapreceives a valid array to process. - Clean Up Routes & Integrations (If Disabling the Feature):
- Delete unused route folders (e.g., delete
src/pages/store/). - Remove
sitemap()from theintegrationsarray and removeimport sitemap from '@astrojs/sitemap'inastro.config.mjs.
- Delete unused route folders (e.g., delete
Once applied, npm run build succeeds cleanly, compiling all 23 static pages into the output directory (/dist).