Skip to content

Commit

Permalink
fix(@astrojs/sitemap): handle base/pathname correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
caioferrarezi committed Jun 7, 2022
1 parent 68caa71 commit 62dfd15
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions packages/integrations/sitemap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down

0 comments on commit 62dfd15

Please sign in to comment.