Fix sitemap.xml page urls (#2335)

* For pages, use the expected pathname, not the file name

* changeset
This commit is contained in:
Jonathan Neal 2022-01-08 08:25:37 -05:00 committed by GitHub
parent 0bbc015a67
commit f008a19c9d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 8 additions and 4 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Preserve pathnames for sitemap.xml

View file

@ -6,7 +6,7 @@ export function generateSitemap(pages: string[]): string {
// copy just in case original copy is needed
// make sure that 404 page is excluded
const urls = [...pages].filter((url) => !url.endsWith('/404/index.html'));
const urls = [...pages].filter((url) => !/404\/?$/.test(url));
urls.sort((a, b) => a.localeCompare(b, 'en', { numeric: true })); // sort alphabetically so sitemap is same each time
let sitemap = `<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">`;
for (const url of urls) {

View file

@ -80,8 +80,7 @@ export function rollupPluginAstroBuildHTML(options: PluginOptions): VitePlugin {
}
for (const pathname of pageData.paths) {
const pathrepl = astroConfig.buildOptions.pageUrlFormat === 'directory' ? '/index.html' : pathname === '/' ? 'index.html' : '.html';
pageNames.push(pathname.replace(/\/?$/, pathrepl).replace(/^\//, ''));
pageNames.push(pathname.replace(/\/?$/, '/').replace(/^\//, ''));
const id = ASTRO_PAGE_PREFIX + pathname;
const html = await ssrRender(renderers, mod, {
astroConfig,

View file

@ -29,7 +29,7 @@ describe('Sitemaps', () => {
it('Generates Sitemap correctly', async () => {
let sitemap = await fixture.readFile('/sitemap.xml');
expect(sitemap).to.equal(
`<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>https://astro.build/episode/fazers/index.html</loc></url><url><loc>https://astro.build/episode/rap-snitch-knishes/index.html</loc></url><url><loc>https://astro.build/episode/rhymes-like-dimes/index.html</loc></url><url><loc>https://astro.build/episodes/index.html</loc></url></urlset>\n`
`<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>https://astro.build/episode/fazers/</loc></url><url><loc>https://astro.build/episode/rap-snitch-knishes/</loc></url><url><loc>https://astro.build/episode/rhymes-like-dimes/</loc></url><url><loc>https://astro.build/episodes/</loc></url></urlset>\n`
);
});
});