-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-sitemap.cjs
41 lines (32 loc) Β· 1.3 KB
/
generate-sitemap.cjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const { SitemapStream, streamToPromise } = require("sitemap");
const { createWriteStream } = require("fs");
const path = require("path");
const publicUrl = "https://gradegenie.site"; // Replace with your domain
// Define your routes
const routes = [
{ url: "/", changefreq: "daily", priority: 1.0 },
{ url: "/about", changefreq: "weekly", priority: 0.8 },
{ url: "/contact", changefreq: "monthly", priority: 0.7 },
{ url: "/gpa-calculator", changefreq: "weekly", priority: 0.9 },
{ url: "/cgpa-calculator", changefreq: "weekly", priority: 0.9 },
{ url: "/converter", changefreq: "weekly", priority: 0.9 },
];
// Generate the sitemap
const generateSitemap = async () => {
try {
const sitemapStream = new SitemapStream({ hostname: publicUrl });
// Add each route to the sitemap
routes.forEach((route) => {
sitemapStream.write(route);
});
sitemapStream.end();
// Convert the stream to a string and save it to the public folder
const sitemap = await streamToPromise(sitemapStream);
const outputPath = path.resolve(__dirname, "public", "sitemap.xml");
createWriteStream(outputPath).write(sitemap);
console.log("β
Sitemap generated at public/sitemap.xml");
} catch (error) {
console.error("β Failed to generate sitemap:", error);
}
};
generateSitemap();