11import { MetadataRoute } from 'next' ;
2- import { getAllArticlePaths } from './services/articleService ' ;
3- import { getAllReferenceParams } from './services/referenceService ' ;
2+ import { getAllContent } from './services/contentService ' ;
3+ import { getBaseUrl } from './services/siteService ' ;
44import { execSync } from 'child_process' ;
55import path from 'path' ;
66
77// Required to evaluate at build time
88export const dynamic = 'force-static' ;
99
10- // Determine base URL
11- const getBaseUrl = ( ) : string => 'https://documentdb.io' ;
12-
1310// Get the last git commit date for a specific file
1411const getGitLastModified = ( filePath : string ) : Date => {
1512 try {
@@ -27,148 +24,47 @@ const getGitLastModified = (filePath: string): Date => {
2724 }
2825} ;
2926
30- // Get the most recent git commit date for a directory
31- const getDirectoryLastModified = ( dirPath : string ) : Date => {
32- try {
33- // Convert absolute path to relative path from repo root
34- const relativePath = path . relative ( process . cwd ( ) , dirPath ) . replace ( / \\ / g, '/' ) ;
35- const output = execSync (
36- `git log -1 --format=%cI -- "${ relativePath } "` ,
37- { encoding : 'utf-8' , cwd : process . cwd ( ) }
38- ) . trim ( ) ;
39-
40- return output ? new Date ( output ) : new Date ( ) ;
41- } catch ( error ) {
42- console . warn ( `Could not get git date for directory ${ dirPath } ` ) ;
43- return new Date ( ) ;
44- }
45- } ;
46-
4727export default function sitemap ( ) : MetadataRoute . Sitemap {
4828 const baseUrl = getBaseUrl ( ) ;
49- const currentDate = new Date ( ) ;
50-
51- const sitemapEntries : MetadataRoute . Sitemap = [ ] ;
52-
53- // 1. Homepage
54- sitemapEntries . push ( {
55- url : baseUrl ,
56- lastModified : currentDate ,
57- changeFrequency : 'yearly' ,
58- priority : 1.0 ,
59- } ) ;
60-
61- // 2. Main section pages
62- const mainSections = [
63- { path : '/docs' , dirPath : 'articles' } ,
64- { path : '/blogs' , dirPath : 'blogs' } ,
65- { path : '/packages' , dirPath : 'app/packages' } ,
66- { path : '/docs/reference' , dirPath : 'reference' } ,
67- ] ;
68-
69- mainSections . forEach ( section => {
70- const dirFullPath = path . join ( process . cwd ( ) , section . dirPath ) ;
71- const lastModified = getDirectoryLastModified ( dirFullPath ) ;
72-
73- sitemapEntries . push ( {
74- url : `${ baseUrl } ${ section . path } ` ,
75- lastModified,
76- changeFrequency : 'monthly' ,
77- priority : 0.70 ,
78- } ) ;
79- } ) ;
80-
81- // 3. Article/Documentation pages
82- try {
83- const articlePaths = getAllArticlePaths ( ) ;
84-
85- articlePaths . forEach ( ( { section, slug } ) => {
86- // Build the URL path
87- let urlPath = `/docs/${ section } ` ;
88- if ( slug . length > 0 ) {
89- urlPath += `/${ slug . join ( '/' ) } ` ;
29+ const entities = getAllContent ( ) ;
30+
31+ return entities
32+ . filter ( entity => ! entity . isExternal ) // Exclude external blog posts
33+ . map ( entity => {
34+ // Determine priority and change frequency based on page type and depth
35+ let priority = 0.00 ;
36+ let changeFrequency : 'yearly' | 'monthly' | 'weekly' = 'yearly' ;
37+
38+ if ( entity . type === 'home' ) {
39+ priority = 1.0 ;
40+ changeFrequency = 'yearly' ;
41+ } else if ( entity . type === 'docs' ) {
42+ priority = 0.85 ;
43+ changeFrequency = 'weekly' ;
44+ } else if ( entity . type === 'packages' ) {
45+ priority = 0.70 ;
46+ changeFrequency = 'weekly' ;
47+ } else if ( entity . type === 'reference' ) {
48+ priority = 0.55 ;
49+ changeFrequency = 'monthly' ;
50+ } else if ( entity . type === 'blog' ) {
51+ priority = 0.40 ;
52+ changeFrequency = 'monthly' ;
53+ } else if ( entity . type === 'landing' ) {
54+ priority = 0.25 ;
55+ changeFrequency = 'monthly' ;
9056 }
9157
92- // Determine the markdown file path
93- const mdFilePath = path . join ( process . cwd ( ) , 'articles' , section , ...slug , 'index.md' ) ;
94- const lastModified = getGitLastModified ( mdFilePath ) ;
95-
96- sitemapEntries . push ( {
97- url : `${ baseUrl } ${ urlPath } ` ,
98- lastModified,
99- changeFrequency : 'weekly' ,
100- priority : 0.70 ,
101- } ) ;
102- } ) ;
103- } catch ( error ) {
104- console . error ( 'Error generating article sitemap entries:' , error ) ;
105- }
106-
107- // 4. Blog posts
108- // Note: Blog posts are external URIs, so we don't include them in the sitemap
109- // as they redirect to external sites. The /blogs page itself is already included above.
110-
111- // 5. Reference documentation pages
112- try {
113- const referenceParams = getAllReferenceParams ( ) ;
114-
115- referenceParams . forEach ( ( { type, category, name } ) => {
116- const urlPath = `/docs/reference/${ type } /${ category } /${ name } ` ;
117-
118- // Determine the markdown file path
119- const mdFilePath = path . join ( process . cwd ( ) , 'reference' , type , category , `${ name } .md` ) ;
120- const lastModified = getGitLastModified ( mdFilePath ) ;
121-
122- sitemapEntries . push ( {
123- url : `${ baseUrl } ${ urlPath } ` ,
124- lastModified,
125- changeFrequency : 'weekly' ,
126- priority : 0.85 ,
127- } ) ;
128- } ) ;
129- } catch ( error ) {
130- console . error ( 'Error generating reference sitemap entries:' , error ) ;
131- }
132-
133- // 6. Reference type index pages (e.g., /docs/reference/commands, /docs/reference/operators)
134- try {
135- const referenceParams = getAllReferenceParams ( ) ;
136- const uniqueTypes = [ ...new Set ( referenceParams . map ( p => p . type ) ) ] ;
137-
138- uniqueTypes . forEach ( type => {
139- const dirPath = path . join ( process . cwd ( ) , 'reference' , type ) ;
140- const lastModified = getDirectoryLastModified ( dirPath ) ;
58+ // Get last modified date from git
59+ const lastModified = entity . filePath
60+ ? getGitLastModified ( entity . filePath )
61+ : new Date ( ) ;
14162
142- sitemapEntries . push ( {
143- url : `${ baseUrl } /docs/reference/ ${ type } ` ,
63+ return {
64+ url : `${ baseUrl } ${ entity . url } ` ,
14465 lastModified,
145- changeFrequency : 'monthly' ,
146- priority : 0.55 ,
147- } ) ;
66+ changeFrequency,
67+ priority,
68+ } ;
14869 } ) ;
149- } catch ( error ) {
150- console . error ( 'Error generating reference type index sitemap entries:' , error ) ;
151- }
152-
153- // 7. Reference category pages (e.g., /docs/reference/operators/miscellaneous-query)
154- try {
155- const referenceParams = getAllReferenceParams ( ) ;
156- const uniqueCategories = [ ...new Set ( referenceParams . map ( p => `${ p . type } /${ p . category } ` ) ) ] ;
157-
158- uniqueCategories . forEach ( typeCategory => {
159- const dirPath = path . join ( process . cwd ( ) , 'reference' , ...typeCategory . split ( '/' ) ) ;
160- const lastModified = getDirectoryLastModified ( dirPath ) ;
161-
162- sitemapEntries . push ( {
163- url : `${ baseUrl } /docs/reference/${ typeCategory } ` ,
164- lastModified,
165- changeFrequency : 'monthly' ,
166- priority : 0.55 ,
167- } ) ;
168- } ) ;
169- } catch ( error ) {
170- console . error ( 'Error generating reference category sitemap entries:' , error ) ;
171- }
172-
173- return sitemapEntries ;
17470}
0 commit comments