Skip to content

feat: add dynamic sitemap generation and API endpoint#1459

Open
kelvinkipruto wants to merge 6 commits intomainfrom
ft/sitemap
Open

feat: add dynamic sitemap generation and API endpoint#1459
kelvinkipruto wants to merge 6 commits intomainfrom
ft/sitemap

Conversation

@kelvinkipruto
Copy link
Copy Markdown
Contributor

Description

Implement sitemap generation functionality with a new API endpoint at /api/v1/sitemap. The sitemap includes all published pages (excluding error pages).

  • Add sitemap.xml rewrite rule in Next.js config
  • Create sitemap API endpoint handler
  • Implement sitemap generation utilities including URL normalization and deduplication

Fixes # (issue)

Type of change

  • New feature (non-breaking change which adds functionality)

Screenshots

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation

Implement sitemap generation functionality with a new API endpoint at `/api/v1/sitemap`. The sitemap includes all published pages (excluding error pages) with proper URL formatting, last modified dates, and XML escaping. This improves SEO by providing search engines with an up-to-date sitemap.

- Add sitemap.xml rewrite rule in Next.js config
- Create sitemap API endpoint handler
- Implement sitemap generation utilities including URL normalization and deduplication
- Export getSitemapXml function from data layer
@kelvinkipruto

This comment was marked as resolved.

@kelvinkipruto kelvinkipruto marked this pull request as ready for review April 16, 2026 09:54
@claude

This comment was marked as resolved.

@chatgpt-codex-connector

This comment was marked as resolved.

…dpoint

- Validate that only GET requests are allowed, returning 405 for other methods
- Add Cache-Control headers to improve performance and reduce server load
- Wrap sitemap generation in try-catch to handle errors gracefully
- Remove unnecessary escapeXml call for lastmod dates and simplify dedupe logic
- Make getSitemapEntries function private to module
@kelvinkipruto

This comment was marked as resolved.

@claude

This comment was marked as resolved.

Change getSitemapXml from named to default export for consistent import patterns
Add Sentry error capture to sitemap API endpoint for improved monitoring
@kelvinkipruto

This comment was marked as resolved.

@claude

This comment was marked as resolved.

@kelvinkipruto

This comment was marked as resolved.

@claude

This comment was marked as resolved.

Copy link
Copy Markdown
Member

@kilemensi kilemensi left a comment

Choose a reason for hiding this comment

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

👍🏽


  1. We have more pages than just pages collection e.g. most of opportunities collection items also become pages.
  2. If you've addressed all comments, please hide them/mark them as resolved. Otherwise, it's hard to review not knowing if you should comment on something that already has a comment or not.

Comment thread apps/trustlab/src/lib/data/common/sitemap.js Outdated
Comment thread apps/trustlab/src/lib/data/common/sitemap.js
Comment thread apps/trustlab/src/lib/data/common/sitemap.js Outdated
Comment on lines +121 to +131
const xmlEntries = entries
.map(({ url, lastModified }) => {
const lastModifiedNode = lastModified
? `\n <lastmod>${lastModified}</lastmod>`
: "";

return ` <url>\n <loc>${escapeXml(url)}</loc>${lastModifiedNode}\n </url>`;
})
.join("\n");

return `<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n${xmlEntries}\n</urlset>\n`;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

  1. Do we need to escape XML? e.g. https://nextjs.org/learn/seo/xml-sitemaps doesn't (and do we need this to be in pages/api instead of just pages?
  2. If it's going to be too big, we may need to look into streaming instead of creating the whole file in memory.

@kilemensi kilemensi added the enhancement New feature or request label Apr 16, 2026
@github-project-automation github-project-automation bot moved this to 🚧 In Progress in COMMONS Apr 16, 2026
- Replace dynamic pathname generation with stored `pathname` field from CMS
- Filter pages by `_status: "published"` instead of just excluding error pages
- Remove XML escaping as URLs are already validated and safe
- Add Sentry warning for pages missing pathname to aid debugging
- Simplify code by removing deduplication and custom escape functions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

Status: 🚧 In Progress

Development

Successfully merging this pull request may close these issues.

2 participants