@@ -10,6 +10,20 @@ const __dirname = path.dirname(__filename);
1010// Define your site URL
1111const SITE_URL = 'https://yourselftoscience.org' ;
1212
13+ // Helper function to get the last modified date of a file from Git
14+ function getLastModified ( filePath ) {
15+ try {
16+ const fullPath = path . resolve ( __dirname , '..' , filePath ) ;
17+ const date = execFileSync ( 'git' , [ 'log' , '-1' , '--format=%as' , fullPath ] ) . toString ( ) . trim ( ) ;
18+ if ( date ) {
19+ return date ;
20+ }
21+ } catch ( error ) {
22+ // Fallback for files not in git or other errors
23+ }
24+ return new Date ( ) . toISOString ( ) . split ( 'T' ) [ 0 ] ;
25+ }
26+
1327// Generate the sitemap XML
1428async function generateSitemap ( ) {
1529 console . log ( 'Generating sitemap.xml...' ) ;
@@ -28,8 +42,7 @@ async function generateSitemap() {
2842
2943 console.log(JSON.stringify({
3044 resources: resources.map(r => ({
31- slug: r.slug,
32- lastModified: new Date().toISOString().split('T')[0]
45+ slug: r.slug
3346 })),
3447 clinicalTrialCountries: clinicalTrialCountries,
3548 bodyDonationCountries: bodyDonationCountries
@@ -43,21 +56,21 @@ async function generateSitemap() {
4356 // Delete the temporary script
4457 fs . unlinkSync ( tempScriptPath ) ;
4558
46- const today = new Date ( ) . toISOString ( ) . split ( 'T' ) [ 0 ] ;
47-
4859 const staticPages = [
49- { url : `${ SITE_URL } /` , priority : '1.0' , changefreq : 'weekly' } ,
50- { url : `${ SITE_URL } /stats` , priority : '0.9' , changefreq : 'weekly' } ,
51- { url : `${ SITE_URL } /get-involved` , priority : '0.9' , changefreq : 'weekly' } ,
52- { url : `${ SITE_URL } /mission` , priority : '0.9' , changefreq : 'monthly' } ,
53- { url : `${ SITE_URL } /clinical-trials` , priority : '0.8' , changefreq : 'weekly' } ,
54- { url : `${ SITE_URL } /organ-body-tissue-donation` , priority : '0.8' , changefreq : 'weekly' } ,
55- { url : `${ SITE_URL } /resources` , priority : '0.5' , changefreq : 'monthly' } , // SEO-only page
56- { url : `${ SITE_URL } /license/content` , priority : '0.3' , changefreq : 'yearly' } ,
57- { url : `${ SITE_URL } /license/code` , priority : '0.3' , changefreq : 'yearly' } ,
58- { url : `${ SITE_URL } /yourselftoscience.pdf` , priority : '0.7' , changefreq : 'weekly' }
60+ { url : `${ SITE_URL } /` , priority : '1.0' , changefreq : 'weekly' , file : 'src/app/page.js' , isDataDriven : true } ,
61+ { url : `${ SITE_URL } /stats` , priority : '0.9' , changefreq : 'weekly' , file : 'src/app/stats/page.js' , isDataDriven : true } ,
62+ { url : `${ SITE_URL } /get-involved` , priority : '0.9' , changefreq : 'weekly' , file : 'src/app/get-involved/page.js' } ,
63+ { url : `${ SITE_URL } /mission` , priority : '0.9' , changefreq : 'monthly' , file : 'src/app/mission/page.js' } ,
64+ { url : `${ SITE_URL } /clinical-trials` , priority : '0.8' , changefreq : 'weekly' , file : 'src/app/clinical-trials/page.js' , isDataDriven : true } ,
65+ { url : `${ SITE_URL } /organ-body-tissue-donation` , priority : '0.8' , changefreq : 'weekly' , file : 'src/app/organ-body-tissue-donation/page.js' , isDataDriven : true } ,
66+ { url : `${ SITE_URL } /resources` , priority : '0.5' , changefreq : 'monthly' , file : 'src/app/resources/page.js' , isDataDriven : true } ,
67+ { url : `${ SITE_URL } /license/content` , priority : '0.3' , changefreq : 'yearly' , file : 'LICENSE-CONTENT' } ,
68+ { url : `${ SITE_URL } /license/code` , priority : '0.3' , changefreq : 'yearly' , file : 'LICENSE-CODE' } ,
69+ { url : `${ SITE_URL } /yourselftoscience.pdf` , priority : '0.7' , changefreq : 'weekly' , file : 'public/yourselftoscience.pdf' }
5970 ] ;
6071
72+ const dataDrivenLastMod = getLastModified ( 'src/data/resources.js' ) ;
73+
6174 const markdownPages = [
6275 { url : `${ SITE_URL } /index.html.md` , priority : '0.8' , changefreq : 'weekly' } ,
6376 { url : `${ SITE_URL } /stats.md` , priority : '0.7' , changefreq : 'weekly' } ,
@@ -70,10 +83,15 @@ async function generateSitemap() {
7083<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">` ;
7184
7285 staticPages . forEach ( page => {
86+ let lastmod = getLastModified ( page . file ) ;
87+ if ( page . isDataDriven ) {
88+ // Use the more recent date between the page's own file and the data file
89+ lastmod = dataDrivenLastMod > lastmod ? dataDrivenLastMod : lastmod ;
90+ }
7391 sitemap += `
7492 <url>
7593 <loc>${ page . url } </loc>
76- <lastmod>${ today } </lastmod>
94+ <lastmod>${ lastmod } </lastmod>
7795 <changefreq>${ page . changefreq } </changefreq>
7896 <priority>${ page . priority } </priority>
7997 </url>` ;
@@ -83,7 +101,7 @@ async function generateSitemap() {
83101 sitemap += `
84102 <url>
85103 <loc>${ page . url } </loc>
86- <lastmod>${ today } </lastmod>
104+ <lastmod>${ dataDrivenLastMod } </lastmod>
87105 <changefreq>${ page . changefreq } </changefreq>
88106 <priority>${ page . priority } </priority>
89107 </url>` ;
@@ -94,7 +112,7 @@ async function generateSitemap() {
94112 sitemap += `
95113 <url>
96114 <loc>${ SITE_URL } /clinical-trials?countries=${ encodeURIComponent ( country ) } </loc>
97- <lastmod>${ today } </lastmod>
115+ <lastmod>${ dataDrivenLastMod } </lastmod>
98116 <changefreq>weekly</changefreq>
99117 <priority>0.7</priority>
100118 </url>` ;
@@ -105,17 +123,20 @@ async function generateSitemap() {
105123 sitemap += `
106124 <url>
107125 <loc>${ SITE_URL } /organ-body-tissue-donation?countries=${ encodeURIComponent ( country ) } </loc>
108- <lastmod>${ today } </lastmod>
126+ <lastmod>${ dataDrivenLastMod } </lastmod>
109127 <changefreq>weekly</changefreq>
110128 <priority>0.7</priority>
111129 </url>` ;
112130 } ) ;
113131
132+ const resourceTemplateLastMod = getLastModified ( 'src/app/resource/[slug]/page.js' ) ;
133+ const lastModForResources = dataDrivenLastMod > resourceTemplateLastMod ? dataDrivenLastMod : resourceTemplateLastMod ;
134+
114135 resourcesData . forEach ( resource => {
115136 sitemap += `
116137 <url>
117138 <loc>${ SITE_URL } /resource/${ resource . slug } </loc>
118- <lastmod>${ resource . lastModified } </lastmod>
139+ <lastmod>${ lastModForResources } </lastmod>
119140 <changefreq>monthly</changefreq>
120141 <priority>0.5</priority>
121142 </url>` ;
0 commit comments