Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Created sitemap #1267

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,7 @@ yarn-error.log*

# typescript
*.tsbuildinfo
next-env.d.ts
next-env.d.ts

# misc
/public/sitemap.xml
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
"build-member-files": "node scripts/loadMemberFiles.js",
"local-dev": "cross-env NODE_ENV=development pnpm netlify dev",
"build": "next build",
"postbuild": "pnpm generate-sitemap",
"start": "next start",
"prebuild": "pnpm build-member-files",
"watch": "npm-watch",
"dev": "pnpm build-member-files && concurrently \"pnpm watch\" \"pnpm local-dev\""
"dev": "pnpm build-member-files && concurrently \"pnpm watch\" \"pnpm local-dev\"",
"generate-sitemap": "node scripts/generate-sitemap.mjs"
},
"watch": {
"build-member-files": "src/content/members/**/*.ts"
Expand Down
112 changes: 112 additions & 0 deletions scripts/generate-sitemap.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { getEpisodes } from '../src/data/podcast';
import { getNewsletter } from '../src/data/newsletters';
import { url } from 'inspector';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

async function generateStaticParams() {
const newsletters = await getNewsletter();
const podcasts = await getEpisodes();

const newsletterUrls = newsletters.map((newsletter) => ({
href: newsletter.href,
lastModified: new Date(newsletter.date),
}));
const podcastUrls = podcasts.map((podcast) => ({
href: podcast.href,
lastModified: new Date(podcast.date),
}));

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,
},
{
url: 'https://store.virtualcoffee.io/pages/contact',
lastModified: new Date(),
changeFrequency: 'yearly',
priority: 0.8,
},
{
url: 'https://virtualcoffee.io/resources',
lastModified: new Date(),
changeFrequency: 'monthly',
priority: 0.8,
},
{
url: 'https://virtualcoffee.io/resources/virtual-coffee-handbook/join-virtual-coffee',
lastModified: new Date(),
changeFrequency: 'yearly',
priority: 0.6,
},
{
url: 'https://store.virtualcoffee.io/',
lastModified: new Date(),
changeFrequency: 'monthly',
priority: 0.8,
},
{
url: 'https://store.virtualcoffee.io/collections/all',
lastModified: new Date(),
changeFrequency: 'monthly',
priority: 0.8,
},
...newsletterUrls,
...podcastUrls,
];
}

(async () => {
console.log('Generating sitemap...');

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 outputPath = path.join(__dirname, '../public/sitemap.xml');
fs.writeFileSync(outputPath, sitemapXml, 'utf8');
console.log(`Sitemap generated successfully at ${outputPath}`);
})();
Loading