Fix sitemap filter (#7263)

This commit is contained in:
André Alves 2023-06-02 05:07:44 -03:00 committed by GitHub
parent 408be72d1d
commit dff0d0dda2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 54 additions and 2 deletions

View file

@ -0,0 +1,5 @@
---
'@astrojs/sitemap': patch
---
Fix sitemap does not filter pages

View file

@ -118,6 +118,8 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => {
return urls; return urls;
}, []); }, []);
pageUrls = Array.from(new Set([...pageUrls, ...routeUrls, ...(customPages ?? [])]));
try { try {
if (filter) { if (filter) {
pageUrls = pageUrls.filter(filter); pageUrls = pageUrls.filter(filter);
@ -127,8 +129,6 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => {
return; return;
} }
pageUrls = Array.from(new Set([...pageUrls, ...routeUrls, ...(customPages ?? [])]));
if (pageUrls.length === 0) { if (pageUrls.length === 0) {
logger.warn(`No pages found!\n\`${OUTFILE}\` not created.`); logger.warn(`No pages found!\n\`${OUTFILE}\` not created.`);
return; return;

View file

@ -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);
});
});
});

View file

@ -0,0 +1 @@
export { default as sitemap } from '@astrojs/sitemap';