diff --git a/scripts/generate-sitemap.mjs b/scripts/generate-sitemap.mjs index 744ee296..a96aa871 100644 --- a/scripts/generate-sitemap.mjs +++ b/scripts/generate-sitemap.mjs @@ -1,65 +1,80 @@ import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; +import { + getEpisodes, +} from '../src/data/podcast'; +import { getNewsletter } from '../src/data/newsletters'; + const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); -function sitemap() { - return [ - { - url: 'https://virtualcoffee.io', - lastModified: new Date(), - changeFrequency: 'yearly', - priority: 1, - }, - { - url: 'https://virtualcoffee.io/about', - lastModified: new Date(), - changeFrequency: 'monthly', - priority: 0.8, - }, - { - url: 'https://virtualcoffee.io/events', - lastModified: new Date(), - changeFrequency: 'weekly', - priority: 0.6, - }, - { - url: 'https://virtualcoffee.io/podcast', - lastModified: new Date(), - changeFrequency: 'monthly', - priority: 0.7, - }, - { - url: 'https://virtualcoffee.io/contact', - lastModified: new Date(), - changeFrequency: 'yearly', - priority: 0.4, - }, - ]; +async function generateStaticParams() { + const newsletters = await getNewsletter(); + const podcasts = await getEpisodes(); + + + const newsletterUrls = newsletters.map(newsletter => ({ + href: newsletter.href, + lastModified: newsletter.lastModified, + })); + const podcastUrls = podcasts.map(podcast => ({ + href: podcast.href, + lastModified: podcast.lastModified, // Ensure you have this field in your data + })); + + return { + newsletters: newsletterUrls, + podcasts: podcastUrls, + }; +} + +async function sitemap() { + const newsletters = await generateStaticParams(); + + const newsletterUrls = newsletters.map(newsletter => ({ + url: `https://virtualcoffee.io/newsletter/issues/${newsletter.href.replace('/newsletter/issues/', '')}`, + lastModified: new Date(newsletter.lastModified), + changeFrequency: 'monthly', + priority: 0.7, + })); + + return [ + { + url: 'https://virtualcoffee.io', + lastModified: new Date(), + changeFrequency: 'yearly', + priority: 1, + }, + { + url: 'https://virtualcoffee.io/about', + lastModified: new Date(), + changeFrequency: 'monthly', + priority: 0.8, + }, + ...newsletterUrls, + ]; } -console.log('Generating sitemap...'); +(async () => { + console.log('Generating sitemap...'); -const sitemapContent = sitemap() - .map( - (entry) => ` - - ${entry.url} - ${entry.lastModified.toISOString()} - ${entry.changeFrequency} - ${entry.priority} - -`, - ) - .join(''); + const sitemapContent = (await sitemap()).map(entry => ` + + ${entry.url} + ${entry.lastModified.toISOString()} + ${entry.changeFrequency} + ${entry.priority} + + `).join(''); -const sitemapXml = ` - - ${sitemapContent} -`; + const sitemapXml = ` + + ${sitemapContent} + `; -const outputPath = path.join(__dirname, '../public/sitemap.xml'); -fs.writeFileSync(outputPath, sitemapXml, 'utf8'); -console.log(`Sitemap generated successfully at ${outputPath}`); + const outputPath = path.join(__dirname, '../public/sitemap.xml'); + fs.writeFileSync(outputPath, sitemapXml, 'utf8'); + console.log(`Sitemap generated successfully at ${outputPath}`); +})(); \ No newline at end of file