Skip to content

Conversation

@ToddHebebrand
Copy link

Summary

Fixes a crash when using @astrojs/sitemap with output: 'server' mode that results in:

Cannot read properties of undefined (reading 'reduce')

Problem

When using output: 'server' in Astro config, the astro:routes:resolved hook may not fire before astro:build:done, leaving the _routes variable undefined. When the sitemap integration tries to call _routes.reduce(), it crashes.

Solution

Added a nullish coalescing operator to default _routes to an empty array:

// Before
const routeUrls = _routes.reduce<string[]>((urls, r) => {

// After  
const routeUrls = (_routes ?? []).reduce<string[]>((urls, r) => {

This prevents the crash while maintaining existing behavior - if routes aren't available, the sitemap will simply not include route-based URLs (falling back to pages and customPages).

Testing

Tested with an Astro project using:

  • output: 'server'
  • @astrojs/node adapter
  • Build previously crashed, now completes successfully

Related Issues

Changeset

Included a patch changeset for @astrojs/sitemap.

When using output: 'server', the astro:routes:resolved hook may not fire
before astro:build:done, leaving _routes undefined. This causes a
'Cannot read properties of undefined (reading reduce)' error.

This fix adds a nullish coalescing operator to default to an empty array,
preventing the crash while maintaining existing behavior.
@changeset-bot
Copy link

changeset-bot bot commented Jan 11, 2026

🦋 Changeset detected

Latest commit: 063f229

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the pkg: integration Related to any renderer integration (scope) label Jan 11, 2026
Copy link
Member

@florian-lefebvre florian-lefebvre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hooks should always fire, can you open a new issue with a minimal reproduction first?

@Princesseuh Princesseuh added the needs response Issue needs response from OP label Jan 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs response Issue needs response from OP pkg: integration Related to any renderer integration (scope)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants