Prevent sitemap URLs without pathname (#3553)

* fix(@astrojs/sitemap): handle base/pathname correctly

* chore: add changeset
This commit is contained in:
Caio Ferrarezi 2022-06-08 12:07:12 -03:00 committed by GitHub
parent 16cf649e0a
commit c601ce59b5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 3 deletions

View file

@ -0,0 +1,5 @@
---
'@astrojs/sitemap': patch
---
Prevent sitemap URLs with trimmed paths

View file

@ -61,14 +61,22 @@ export default function createPlugin({
config = _config;
},
'astro:build:done': async ({ pages, dir }) => {
const finalSiteUrl = canonicalURL || config.site;
if (!finalSiteUrl) {
let finalSiteUrl: URL;
if (canonicalURL) {
finalSiteUrl = new URL(canonicalURL);
finalSiteUrl.pathname += finalSiteUrl.pathname.endsWith('/') ? '' : '/'; // normalizes the final url since it's provided by user
} else if (config.site) {
finalSiteUrl = new URL(config.base, config.site);
} else {
console.warn(
'The Sitemap integration requires either the `site` astro.config option or `canonicalURL` integration option. Skipping.'
);
return;
}
let pageUrls = pages.map((p) => new URL(p.pathname, finalSiteUrl).href);
let pageUrls = pages.map((p) => {
const path = finalSiteUrl.pathname + p.pathname
return new URL(path, finalSiteUrl).href
});
if (filter) {
pageUrls = pageUrls.filter((page: string) => filter(page));
}