feat: better behavior for 'undefined' return values from 'serialize… (#3723)
* feat: better behavior with 'undefined' return values after 'serialize' func * build: changeset added
This commit is contained in:
parent
0ae1365533
commit
52f75369ef
3 changed files with 19 additions and 3 deletions
5
.changeset/tidy-dots-own.md
Normal file
5
.changeset/tidy-dots-own.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@astrojs/sitemap': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix: if `serialize` function returns `undefined` for the passed entry, such entry will be excluded from sitemap
|
|
@ -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 `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__
|
__astro.config.mjs__
|
||||||
|
|
||||||
|
@ -234,6 +236,9 @@ export default {
|
||||||
integrations: [
|
integrations: [
|
||||||
sitemap({
|
sitemap({
|
||||||
serialize(item) {
|
serialize(item) {
|
||||||
|
if (/exclude-from-sitemap/.test(item.url)) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
if (/your-special-page/.test(item.url)) {
|
if (/your-special-page/.test(item.url)) {
|
||||||
item.changefreq = 'daily';
|
item.changefreq = 'daily';
|
||||||
item.lastmod = new Date();
|
item.lastmod = new Date();
|
||||||
|
|
|
@ -38,7 +38,7 @@ export type SitemapOptions =
|
||||||
priority?: number;
|
priority?: number;
|
||||||
|
|
||||||
// called for each sitemap item just before to save them on disk, sync or async
|
// 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;
|
| undefined;
|
||||||
|
|
||||||
|
@ -117,8 +117,14 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => {
|
||||||
const serializedUrls: SitemapItem[] = [];
|
const serializedUrls: SitemapItem[] = [];
|
||||||
for (const item of urlData) {
|
for (const item of urlData) {
|
||||||
const serialized = await Promise.resolve(serialize(item));
|
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;
|
urlData = serializedUrls;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.error(`Error serializing pages\n${(err as any).toString()}`);
|
logger.error(`Error serializing pages\n${(err as any).toString()}`);
|
||||||
|
|
Loading…
Reference in a new issue