11import type { MetadataRoute } from 'next' ;
2- import { blogSource } from '@/lib/blog-source' ;
2+ import { SITE_URL } from '@/app/util/constants' ;
3+ import { getPosts } from '@/lib/blog-query' ;
34import { source } from '@/lib/source' ;
45
56// Regex pattern for matching integration pages
@@ -18,6 +19,9 @@ const priorityMap: Record<string, number> = {
1819 '/docs/security' : 0.8 ,
1920 '/docs/Integrations' : 0.8 ,
2021 '/docs/api' : 0.7 ,
22+ '/blog' : 0.8 ,
23+ '/api' : 0.7 ,
24+ '/contributors' : 0.8 ,
2125 '/privacy' : 0.5 ,
2226 '/demo' : 0.6 ,
2327 '/llms.txt' : 0.4 ,
@@ -36,6 +40,9 @@ const changeFrequencyMap: Record<string, 'weekly' | 'monthly' | 'yearly'> = {
3640 '/docs/security' : 'monthly' ,
3741 '/docs/Integrations' : 'weekly' ,
3842 '/docs/api' : 'monthly' ,
43+ '/blog' : 'monthly' ,
44+ '/api' : 'monthly' ,
45+ '/contributors' : 'monthly' ,
3946 '/privacy' : 'yearly' ,
4047 '/demo' : 'monthly' ,
4148 '/llms.txt' : 'weekly' ,
@@ -86,16 +93,6 @@ function processSourcePages(
8693 ) ;
8794}
8895
89- function processBlogPages (
90- pages : Array < { url : string } > ,
91- baseUrl : string ,
92- lastModified : Date
93- ) : MetadataRoute . Sitemap {
94- return pages . map ( ( page ) =>
95- createSitemapEntry ( page . url , baseUrl , lastModified , 0.5 , 'weekly' )
96- ) ;
97- }
98-
9996function processNonDocPages (
10097 pages : string [ ] ,
10198 baseUrl : string ,
@@ -106,6 +103,26 @@ function processNonDocPages(
106103 ) ;
107104}
108105
106+ function processBlogPages ( baseUrl : string ) : Promise < MetadataRoute . Sitemap > {
107+ return getPosts ( )
108+ . then ( ( data ) => {
109+ if ( ! data ?. posts ) {
110+ return [ ] ;
111+ }
112+
113+ return data . posts . map ( ( post ) => ( {
114+ url : `${ baseUrl } /blog/${ post . slug } ` ,
115+ lastModified : new Date ( post . publishedAt ) ,
116+ changeFrequency : 'monthly' as const ,
117+ priority : 0.8 ,
118+ } ) ) ;
119+ } )
120+ . catch ( ( error ) => {
121+ console . warn ( 'Failed to fetch blog posts for sitemap:' , error ) ;
122+ return [ ] ;
123+ } ) ;
124+ }
125+
109126function getFallbackEntries ( ) : Array < {
110127 url : string ;
111128 priority : number ;
@@ -177,23 +194,52 @@ function processFallbackEntries(
177194 ) ;
178195}
179196
180- export function generateSitemapEntries ( ) : MetadataRoute . Sitemap {
181- const baseUrl = 'https://www.databuddy.cc' ;
197+ export async function generateSitemapEntries ( ) : Promise < MetadataRoute . Sitemap > {
182198 const lastModified = new Date ( ) ;
183199 const entries : MetadataRoute . Sitemap = [ ] ;
184200
185201 try {
186202 const pages = source . getPages ( ) ;
187- const blogPages = blogSource . getPages ( ) ;
188- const nonDocPages = [ '/privacy' , '/demo' , '/llms.txt' ] ;
203+ const nonDocPages = [
204+ '/privacy' ,
205+ '/demo' ,
206+ '/llms.txt' ,
207+ '/contributors' ,
208+ '/api' ,
209+ ] ;
210+
211+ entries . push ( ...processSourcePages ( pages , SITE_URL , lastModified ) ) ;
212+ entries . push ( ...processNonDocPages ( nonDocPages , SITE_URL , lastModified ) ) ;
213+
214+ const blogEntries = await processBlogPages ( SITE_URL ) ;
215+ entries . push ( ...blogEntries ) ;
216+
217+ // Add the blog index page with lastModified set to latest blog post
218+ const latestBlogModified = blogEntries . length
219+ ? new Date (
220+ Math . max (
221+ ...blogEntries
222+ . map ( ( e ) =>
223+ e . lastModified ? new Date ( e . lastModified ) . getTime ( ) : 0
224+ )
225+ . filter ( ( t ) => Number . isFinite ( t ) && t > 0 )
226+ )
227+ )
228+ : lastModified ;
229+ entries . push (
230+ createSitemapEntry (
231+ '/blog' ,
232+ SITE_URL ,
233+ latestBlogModified ,
234+ priorityMap [ '/blog' ] ,
235+ changeFrequencyMap [ '/blog' ]
236+ )
237+ ) ;
189238
190- entries . push ( ...processSourcePages ( pages , baseUrl , lastModified ) ) ;
191- entries . push ( ...processBlogPages ( blogPages , baseUrl , lastModified ) ) ;
192- entries . push ( ...processNonDocPages ( nonDocPages , baseUrl , lastModified ) ) ;
193239 processIntegrationPages ( entries ) ;
194240 } catch ( error ) {
195241 console . warn ( 'Failed to generate dynamic sitemap, using fallback:' , error ) ;
196- entries . push ( ...processFallbackEntries ( baseUrl , lastModified ) ) ;
242+ entries . push ( ...processFallbackEntries ( SITE_URL , lastModified ) ) ;
197243 }
198244
199245 return entries ;
@@ -204,13 +250,5 @@ export function getSitemapMetadata() {
204250 title : 'Databuddy Documentation Sitemap' ,
205251 description :
206252 'Dynamically generated sitemap of Databuddy documentation including all guides, integrations, and API references.' ,
207- keywords : [
208- 'databuddy' ,
209- 'analytics' ,
210- 'documentation' ,
211- 'sitemap' ,
212- 'privacy-first' ,
213- 'web analytics' ,
214- ] ,
215253 } ;
216254}
0 commit comments