Fix sitemap filter (#7263)
This commit is contained in:
parent
408be72d1d
commit
dff0d0dda2
4 changed files with 54 additions and 2 deletions
5
.changeset/eight-planes-drop.md
Normal file
5
.changeset/eight-planes-drop.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@astrojs/sitemap': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fix sitemap does not filter pages
|
|
@ -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;
|
||||||
|
|
46
packages/integrations/sitemap/test/filter.test.js
Normal file
46
packages/integrations/sitemap/test/filter.test.js
Normal 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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
1
packages/integrations/sitemap/test/fixtures/static/deps.mjs
vendored
Normal file
1
packages/integrations/sitemap/test/fixtures/static/deps.mjs
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
export { default as sitemap } from '@astrojs/sitemap';
|
Loading…
Reference in a new issue