Skip to content

Commit

Permalink
not able to locate file [currently working on it]
Browse files Browse the repository at this point in the history
  • Loading branch information
manya706 committed Oct 12, 2024
1 parent 25fb8f2 commit 0604083
Showing 1 changed file with 68 additions and 53 deletions.
121 changes: 68 additions & 53 deletions scripts/generate-sitemap.mjs
Original file line number Diff line number Diff line change
@@ -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) => `
<url>
<loc>${entry.url}</loc>
<lastmod>${entry.lastModified.toISOString()}</lastmod>
<changefreq>${entry.changeFrequency}</changefreq>
<priority>${entry.priority}</priority>
</url>
`,
)
.join('');
const sitemapContent = (await sitemap()).map(entry => `
<url>
<loc>${entry.url}</loc>
<lastmod>${entry.lastModified.toISOString()}</lastmod>
<changefreq>${entry.changeFrequency}</changefreq>
<priority>${entry.priority}</priority>
</url>
`).join('');

const sitemapXml = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${sitemapContent}
</urlset>`;
const sitemapXml = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${sitemapContent}
</urlset>`;

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}`);
})();

0 comments on commit 0604083

Please sign in to comment.