80 lines
2.2 KiB
JavaScript
80 lines
2.2 KiB
JavaScript
const fs = require("fs");
|
|
const path = require("path");
|
|
const { SitemapStream, streamToPromise } = require("sitemap");
|
|
|
|
const hostname = "https://shivasakthi.ca";
|
|
const addTrailingSlash = true; // Set true if your Next.js has trailingSlash: true
|
|
|
|
// ✅ Utility to control trailing slashes
|
|
const shouldAddSlash = (url) => {
|
|
if (url === "/") return false;
|
|
if (/\.[a-z0-9]{2,6}(\?.*)?$/i.test(url)) return false;
|
|
return true;
|
|
};
|
|
|
|
const formatUrl = (url) => {
|
|
if (!url.startsWith("/")) url = "/" + url;
|
|
|
|
if (addTrailingSlash && shouldAddSlash(url) && !url.endsWith("/")) {
|
|
return url + "/";
|
|
}
|
|
if (!addTrailingSlash && url.endsWith("/") && url !== "/") {
|
|
return url.slice(0, -1);
|
|
}
|
|
return url;
|
|
};
|
|
|
|
const staticLinks = [
|
|
{ url: "/", changefreq: "daily", priority: 1.0 },
|
|
{ url: "/about", changefreq: "weekly", priority: 0.7 },
|
|
{ url: "/menu", changefreq: "weekly", priority: 0.7 },
|
|
{ url: "/blog", changefreq: "weekly", priority: 0.7 },
|
|
{ url: "/contact-south-indian-restaurant", changefreq: "monthly", priority: 0.5 },
|
|
];
|
|
|
|
// ✅ Dynamic blog posts (example)
|
|
const blogPosts = [
|
|
{ slug: "/blog/the-flavors-of-north-india-more-than-just-curries/" },
|
|
{ slug: "/blog/the-secret-to-perfect-north-indian-curries/" },
|
|
{ slug: "/blog/the-rich-history-of-south-indian-cuisine/" },
|
|
];
|
|
|
|
// // Convert blog slugs to sitemap entries
|
|
const blogLinks = blogPosts.map((post) => ({
|
|
url: formatUrl(post.slug),
|
|
changefreq: "weekly",
|
|
priority: 0.6,
|
|
}));
|
|
|
|
const allLinks = [...staticLinks, ...blogLinks].map((link) => ({
|
|
...link,
|
|
url: formatUrl(link.url),
|
|
}));
|
|
|
|
async function generateSitemap() {
|
|
try {
|
|
const sitemap = new SitemapStream({ hostname });
|
|
const writeStream = fs.createWriteStream(
|
|
path.resolve(__dirname, "../public/sitemap.xml")
|
|
);
|
|
|
|
sitemap.pipe(writeStream);
|
|
|
|
console.log("📦 Writing URLs to sitemap:");
|
|
allLinks.forEach((link) => {
|
|
const fullUrl = `${hostname}${link.url}`;
|
|
console.log(" -", fullUrl);
|
|
sitemap.write(link);
|
|
});
|
|
|
|
sitemap.end();
|
|
await streamToPromise(sitemap);
|
|
|
|
console.log("✅ sitemap.xml created successfully!");
|
|
} catch (error) {
|
|
console.error("❌ Error creating sitemap.xml:", error);
|
|
}
|
|
}
|
|
|
|
generateSitemap();
|