summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.changeset/eight-planes-drop.md5
-rw-r--r--packages/integrations/sitemap/src/index.ts4
-rw-r--r--packages/integrations/sitemap/test/filter.test.js46
-rw-r--r--packages/integrations/sitemap/test/fixtures/static/deps.mjs1
4 files changed, 54 insertions, 2 deletions
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';