Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: better behavior for 'undefined' return values from 'serialize… #3723

Merged
merged 2 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/integrations/sitemap/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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__

Expand All @@ -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();
Expand Down
10 changes: 8 additions & 2 deletions packages/integrations/sitemap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SitemapItem>;
serialize?(item: SitemapItem): SitemapItem | Promise<SitemapItem | undefined> | undefined;
}
| undefined;

Expand Down Expand Up @@ -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()}`);
Expand Down