From 52f75369efe5a0a1b320478984c90b6727d52159 Mon Sep 17 00:00:00 2001 From: Oleksii Tymoshenko Date: Mon, 27 Jun 2022 21:02:00 +0300 Subject: [PATCH] =?UTF-8?q?feat:=20better=20behavior=20for=20'undefined'?= =?UTF-8?q?=20return=20values=20from=20'serialize=E2=80=A6=20(#3723)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: better behavior with 'undefined' return values after 'serialize' func * build: changeset added --- .changeset/tidy-dots-own.md | 5 +++++ packages/integrations/sitemap/README.md | 7 ++++++- packages/integrations/sitemap/src/index.ts | 10 ++++++++-- 3 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 .changeset/tidy-dots-own.md diff --git a/.changeset/tidy-dots-own.md b/.changeset/tidy-dots-own.md new file mode 100644 index 000000000..a9367dc2a --- /dev/null +++ b/.changeset/tidy-dots-own.md @@ -0,0 +1,5 @@ +--- +'@astrojs/sitemap': patch +--- + +fix: if `serialize` function returns `undefined` for the passed entry, such entry will be excluded from sitemap diff --git a/packages/integrations/sitemap/README.md b/packages/integrations/sitemap/README.md index a2e7b73d9..e37a1ed86 100644 --- a/packages/integrations/sitemap/README.md +++ b/packages/integrations/sitemap/README.md @@ -222,7 +222,9 @@ The `LinkItem` type has two required fields: `url` (the fully-qualified URL for The `serialize` function should return `SitemapItem`, touched or not. -The example below shows the ability to add the sitemap specific properties individually. +To exclude the passed entry from sitemap it should return `undefined`. + +The example below shows the ability to exclude certain entries and add the sitemap specific properties individually. __astro.config.mjs__ @@ -234,6 +236,9 @@ export default { integrations: [ sitemap({ serialize(item) { + if (/exclude-from-sitemap/.test(item.url)) { + return undefined; + } if (/your-special-page/.test(item.url)) { item.changefreq = 'daily'; item.lastmod = new Date(); diff --git a/packages/integrations/sitemap/src/index.ts b/packages/integrations/sitemap/src/index.ts index 2e1fa5a6e..67990b041 100644 --- a/packages/integrations/sitemap/src/index.ts +++ b/packages/integrations/sitemap/src/index.ts @@ -38,7 +38,7 @@ export type SitemapOptions = priority?: number; // called for each sitemap item just before to save them on disk, sync or async - serialize?(item: SitemapItem): SitemapItem | Promise; + serialize?(item: SitemapItem): SitemapItem | Promise | undefined; } | undefined; @@ -117,8 +117,14 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => { const serializedUrls: SitemapItem[] = []; for (const item of urlData) { const serialized = await Promise.resolve(serialize(item)); - serializedUrls.push(serialized); + if (serialized) { + serializedUrls.push(serialized); + } } + if (serializedUrls.length === 0) { + logger.warn('No pages found!'); + return; + } urlData = serializedUrls; } catch (err) { logger.error(`Error serializing pages\n${(err as any).toString()}`);