92 lines
2.5 KiB
JavaScript
92 lines
2.5 KiB
JavaScript
const fs = require("fs");
|
|
const path = require("path");
|
|
const { SitemapStream, streamToPromise } = require("sitemap");
|
|
|
|
// ✅ My Dosa Place — Production domain
|
|
const hostname = "https://mydosaplace.ca";
|
|
|
|
// ✅ Next.js is configured with trailingSlash: true (see next.config.ts)
|
|
const addTrailingSlash = true;
|
|
|
|
// 🔧 Utility: Add trailing slash only for "directory-like" URLs
|
|
const shouldAddSlash = (url) => {
|
|
// Keep "/" as-is
|
|
if (url === "/") return false;
|
|
// Don't touch file-like URLs (has extension, e.g. .xml, .json)
|
|
if (/\.[a-z0-9]{2,6}(\?.*)?$/i.test(url)) return false;
|
|
return true;
|
|
};
|
|
|
|
const formatUrl = (url) => {
|
|
// Normalize to leading slash
|
|
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 = [
|
|
// Home — highest priority, changes daily
|
|
{ url: "/", changefreq: "daily", priority: 1.0 },
|
|
|
|
// Core pages
|
|
{ url: "/menu/", changefreq: "weekly", priority: 0.9 },
|
|
{ url: "/about/", changefreq: "monthly", priority: 0.7 },
|
|
{ url: "/contact/", changefreq: "monthly", priority: 0.7 },
|
|
{ url: "/reservation/",changefreq: "weekly", priority: 0.8 },
|
|
|
|
// Blog listing
|
|
{ url: "/blog/", changefreq: "weekly", priority: 0.7 },
|
|
];
|
|
|
|
const blogPosts = [
|
|
{ slug: "dosa-in-waterloo-guide" },
|
|
{ slug: "south-indian-restaurant-waterloo" },
|
|
{ slug: "vegan-friendly-south-indian-food" },
|
|
];
|
|
|
|
const blogLinks = blogPosts.map((post) => ({
|
|
url: `/blog/${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) => {
|
|
console.log(" -", hostname + link.url);
|
|
sitemap.write(link);
|
|
});
|
|
|
|
sitemap.end();
|
|
await streamToPromise(sitemap);
|
|
|
|
console.log("\n✅ sitemap.xml created successfully!");
|
|
console.log(` Total URLs: ${allLinks.length}`);
|
|
console.log(` Output: public/sitemap.xml`);
|
|
} catch (error) {
|
|
console.error("❌ Error creating sitemap.xml:", error);
|
|
}
|
|
}
|
|
|
|
generateSitemap();
|