File tree Expand file tree Collapse file tree
packages/integrations/sitemap Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ ---
2+ ' @astrojs/sitemap ' : patch
3+ ---
4+
5+ fix: if ` serialize ` function returns ` undefined ` for the passed entry, such entry will be excluded from sitemap
Original file line number Diff line number Diff line change @@ -222,7 +222,9 @@ The `LinkItem` type has two required fields: `url` (the fully-qualified URL for
222222
223223The ` serialize ` function should return ` SitemapItem ` , touched or not.
224224
225- The example below shows the ability to add the sitemap specific properties individually.
225+ To exclude the passed entry from sitemap it should return ` undefined ` .
226+
227+ The example below shows the ability to exclude certain entries and add the sitemap specific properties individually.
226228
227229__ astro.config.mjs__
228230
@@ -234,6 +236,9 @@ export default {
234236 integrations: [
235237 sitemap ({
236238 serialize (item ) {
239+ if (/ exclude-from-sitemap/ .test (item .url )) {
240+ return undefined ;
241+ }
237242 if (/ your-special-page/ .test (item .url )) {
238243 item .changefreq = ' daily' ;
239244 item .lastmod = new Date ();
Original file line number Diff line number Diff line change @@ -38,7 +38,7 @@ export type SitemapOptions =
3838 priority ?: number ;
3939
4040 // called for each sitemap item just before to save them on disk, sync or async
41- serialize ?( item : SitemapItem ) : SitemapItem | Promise < SitemapItem > ;
41+ serialize ?( item : SitemapItem ) : SitemapItem | Promise < SitemapItem | undefined > | undefined ;
4242 }
4343 | undefined ;
4444
@@ -117,8 +117,14 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => {
117117 const serializedUrls : SitemapItem [ ] = [ ] ;
118118 for ( const item of urlData ) {
119119 const serialized = await Promise . resolve ( serialize ( item ) ) ;
120- serializedUrls . push ( serialized ) ;
120+ if ( serialized ) {
121+ serializedUrls . push ( serialized ) ;
122+ }
121123 }
124+ if ( serializedUrls . length === 0 ) {
125+ logger . warn ( 'No pages found!' ) ;
126+ return ;
127+ }
122128 urlData = serializedUrls ;
123129 } catch ( err ) {
124130 logger . error ( `Error serializing pages\n${ ( err as any ) . toString ( ) } ` ) ;
You can’t perform that action at this time.
0 commit comments