From dff0d0dda2f20c02901594739a654834d3451c8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Alves?= <71379045+andremralves@users.noreply.github.com> Date: Fri, 2 Jun 2023 05:07:44 -0300 Subject: [PATCH] Fix sitemap filter (#7263) --- .changeset/eight-planes-drop.md | 5 ++ packages/integrations/sitemap/src/index.ts | 4 +- .../integrations/sitemap/test/filter.test.js | 46 +++++++++++++++++++ .../sitemap/test/fixtures/static/deps.mjs | 1 + 4 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 .changeset/eight-planes-drop.md create mode 100644 packages/integrations/sitemap/test/filter.test.js create mode 100644 packages/integrations/sitemap/test/fixtures/static/deps.mjs diff --git a/.changeset/eight-planes-drop.md b/.changeset/eight-planes-drop.md new file mode 100644 index 000000000..8ebef4be5 --- /dev/null +++ b/.changeset/eight-planes-drop.md @@ -0,0 +1,5 @@ +--- +'@astrojs/sitemap': patch +--- + +Fix sitemap does not filter pages diff --git a/packages/integrations/sitemap/src/index.ts b/packages/integrations/sitemap/src/index.ts index 46ff694fd..13df4d1ae 100644 --- a/packages/integrations/sitemap/src/index.ts +++ b/packages/integrations/sitemap/src/index.ts @@ -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); @@ -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; diff --git a/packages/integrations/sitemap/test/filter.test.js b/packages/integrations/sitemap/test/filter.test.js new file mode 100644 index 000000000..65a418e87 --- /dev/null +++ b/packages/integrations/sitemap/test/filter.test.js @@ -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); + }); + }); + +}); + diff --git a/packages/integrations/sitemap/test/fixtures/static/deps.mjs b/packages/integrations/sitemap/test/fixtures/static/deps.mjs new file mode 100644 index 000000000..b24f26189 --- /dev/null +++ b/packages/integrations/sitemap/test/fixtures/static/deps.mjs @@ -0,0 +1 @@ +export { default as sitemap } from '@astrojs/sitemap';