Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sitemap-file-format.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/sitemap': patch
---

Fix sitemap URLs missing `.html` extension when `build.format` is set to `file`
21 changes: 19 additions & 2 deletions packages/integrations/sitemap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,15 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => {
finalSiteUrl.pathname += '/';
if (p.pathname.startsWith('/')) p.pathname = p.pathname.slice(1);
const fullPath = finalSiteUrl.pathname + p.pathname;
return new URL(fullPath, finalSiteUrl).href;
const newUrl = new URL(fullPath, finalSiteUrl).href;

if (config.build.format === 'file') {
const urlPath = new URL(newUrl).pathname;
if (urlPath !== '/' && urlPath !== '' && !urlPath.endsWith('/')) {
return newUrl + '.html';
}
}
return newUrl;
});

const routeUrls = _routes.reduce<string[]>((urls, r) => {
Expand All @@ -146,7 +154,16 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => {

const newUrl = new URL(fullPath, finalSiteUrl).href;

if (config.trailingSlash === 'never') {
if (config.build.format === 'file') {
const urlPath = new URL(newUrl).pathname;
if (urlPath !== '/' && urlPath !== '' && !urlPath.endsWith('/')) {
// When build.format is 'file', output files have .html extension
// (e.g., /about → /about.html), so sitemap URLs should match
urls.push(newUrl + '.html');
} else {
urls.push(newUrl);
}
} else if (config.trailingSlash === 'never') {
urls.push(newUrl);
} else if (config.build.format === 'directory' && !newUrl.endsWith('/')) {
urls.push(newUrl + '/');
Expand Down
6 changes: 3 additions & 3 deletions packages/integrations/sitemap/test/trailing-slash.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ describe('Trailing slash', () => {
await fixture.build();
});

it('URLs do not end with trailing slash', async () => {
it('URLs have .html extension', async () => {
const data = await readXML(fixture.readFile('/sitemap-0.xml'));
const urls = data.urlset.url;

assert.equal(urls[0].loc[0], 'http://example.com');
assert.equal(urls[1].loc[0], 'http://example.com/one');
assert.equal(urls[2].loc[0], 'http://example.com/two');
assert.equal(urls[1].loc[0], 'http://example.com/one.html');
assert.equal(urls[2].loc[0], 'http://example.com/two.html');
});
});
});
Expand Down