fix: Update help center sitemap XML structure (#13357)

# Pull Request Template

## Description
The Help Center sitemap endpoint (`/hc/:portal_slug/sitemap.xml`)
previously rendered a `<sitemapindex>` element while embedding article
URLs directly, which does not align with the sitemap specification.

This change fixes the structure by:
- Replacing `<sitemapindex>` with `<urlset>`
- Adding the required sitemap XML namespace
- Rendering each published article as a `<url>` entry with `<loc>` and
`<lastmod>`

This ensures the endpoint outputs a valid, self-contained sitemap
document.

Fixes #13334

## Type of change

Please delete options that are not relevant.

- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality not to work as expected)
- [ ] This change requires a documentation update

## How Has This Been Tested?
- Updated the existing `portals_controller_spec.rb`
- Adjusted assertions to validate a `<urlset>` root element and the
sitemap XML namespace
- Verified that the sitemap returns only published article URLs
- Ran the updated RSpec controller specs locally


## Checklist:

- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my code
- [x] I have commented on my code, particularly in hard-to-understand
areas
- [x] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [x] Any dependent changes have been merged and published in downstream
modules
This commit is contained in:
TheDanniCraft
2026-01-27 03:08:20 +01:00
committed by GitHub
parent ad2329c237
commit 885b041a83
2 changed files with 26 additions and 15 deletions

View File

@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<% @portal.articles.where(status: :published).each do |article| %>
<sitemap>
<url>
<loc><%= @help_center_url %><%= generate_article_link(@portal.slug, article.slug, false, false) %></loc>
<lastmod><%= article.updated_at.strftime("%Y-%m-%d") %></lastmod>
</sitemap>
<lastmod><%= article.updated_at.to_date.iso8601 %></lastmod>
</url>
<% end %>
</sitemapindex>
</urlset>

View File

@@ -60,24 +60,35 @@ RSpec.describe Public::Api::V1::PortalsController, type: :request do
describe 'GET /public/api/v1/portals/{portal_slug}/sitemap' do
context 'when custom_domain is present' do
it 'gets a valid sitemap' do
it 'returns a valid urlset sitemap with the correct namespace' do
get "/hc/#{portal.slug}/sitemap.xml"
expect(response).to have_http_status(:success)
expect(response.body).to match(/<sitemap/)
expect(Nokogiri::XML(response.body).errors).to be_empty
doc = Nokogiri::XML(response.body)
expect(doc.errors).to be_empty
expect(doc.root.name).to eq('urlset')
expect(doc.root.namespace&.href).to eq('http://www.sitemaps.org/schemas/sitemap/0.9')
end
it 'has valid sitemap links' do
it 'contains valid article URLs for the portal' do
get "/hc/#{portal.slug}/sitemap.xml"
expect(response).to have_http_status(:success)
parsed_xml = Nokogiri::XML(response.body)
links = parsed_xml.css('loc')
links.each do |link|
expect(link.text).to match(%r{https://www\.example\.com/hc/test-portal/articles/\d+})
end
doc = Nokogiri::XML(response.body)
doc.remove_namespaces!
expect(links.length).to eq 3
# ensure we are NOT returning a sitemapindex
expect(doc.xpath('//sitemapindex')).to be_empty
links = doc.xpath('//url/loc').map(&:text)
expect(links.length).to eq(3)
expect(links).to all(
match(%r{\Ahttps://www\.example\.com/hc/#{Regexp.escape(portal.slug)}/articles/\d+})
)
end
end
end