Skip to content

Commit

Permalink
Fix sitemap filter (#7263)
Browse files Browse the repository at this point in the history
  • Loading branch information
andremralves authored Jun 2, 2023
1 parent 408be72 commit dff0d0d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/eight-planes-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/sitemap': patch
---

Fix sitemap does not filter pages
4 changes: 2 additions & 2 deletions packages/integrations/sitemap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => {
return urls;
}, []);

pageUrls = Array.from(new Set([...pageUrls, ...routeUrls, ...(customPages ?? [])]));

try {
if (filter) {
pageUrls = pageUrls.filter(filter);
Expand All @@ -127,8 +129,6 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => {
return;
}

pageUrls = Array.from(new Set([...pageUrls, ...routeUrls, ...(customPages ?? [])]));

if (pageUrls.length === 0) {
logger.warn(`No pages found!\n\`${OUTFILE}\` not created.`);
return;
Expand Down
46 changes: 46 additions & 0 deletions packages/integrations/sitemap/test/filter.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { loadFixture, readXML } from './test-utils.js';
import { expect } from 'chai';
import { sitemap } from './fixtures/static/deps.mjs';

describe('Filter support', () => {
/** @type {import('./test-utils.js').Fixture} */
let fixture;

describe('Static', () => {
before(async () => {
fixture = await loadFixture({
root: './fixtures/static/',
integrations: [sitemap({
filter: (page) => page !== 'http://example.com/two/'
})],
});
await fixture.build();
});

it('Just one page is added', async () => {
const data = await readXML(fixture.readFile('/sitemap-0.xml'));
const urls = data.urlset.url;
expect(urls.length).to.equal(1);
});
});

describe('SSR', () => {
before(async () => {
fixture = await loadFixture({
root: './fixtures/ssr/',
integrations: [sitemap({
filter: (page) => page !== 'http://example.com/two/'
})],
});
await fixture.build();
});

it('Just one page is added', async () => {
const data = await readXML(fixture.readFile('/client/sitemap-0.xml'));
const urls = data.urlset.url;
expect(urls.length).to.equal(1);
});
});

});

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as sitemap } from '@astrojs/sitemap';

0 comments on commit dff0d0d

Please sign in to comment.