77 lines
2.5 KiB
JavaScript
77 lines
2.5 KiB
JavaScript
import { mkdir, readdir, writeFile } from "node:fs/promises";
|
|
import path from "node:path";
|
|
|
|
const projectRoot = process.cwd();
|
|
const appRoot = path.join(projectRoot, "src", "app");
|
|
const outputDir = path.join(projectRoot, "public", "meshkee");
|
|
const outputFile = path.join(outputDir, "sitemap-config.json");
|
|
const baseUrl = `https://${(process.env.MESHKEE_WEBSITE_DOMAIN || "hediehmezon.ir")
|
|
.trim()
|
|
.replace(/^https?:\/\//, "")
|
|
.replace(/^www\./, "")
|
|
.split("/")[0]}`;
|
|
|
|
const excludedSegments = new Set([
|
|
"account",
|
|
"admin",
|
|
"auth",
|
|
"business",
|
|
"cart",
|
|
"checkout",
|
|
"customer",
|
|
"dashboard",
|
|
"login",
|
|
"register",
|
|
"signin",
|
|
"signup",
|
|
]);
|
|
|
|
async function collectPages(directory, segments = []) {
|
|
const entries = await readdir(directory, { withFileTypes: true });
|
|
const routes = [];
|
|
|
|
if (entries.some((entry) => entry.isFile() && /^page\.(?:js|jsx|ts|tsx)$/.test(entry.name))) {
|
|
const publicSegments = segments.filter(
|
|
(segment) => !segment.startsWith("(") && !segment.startsWith("@"),
|
|
);
|
|
const isDynamic = publicSegments.some((segment) => segment.includes("[") || segment.includes("]"));
|
|
const isExcluded = publicSegments.some((segment) => excludedSegments.has(segment.toLowerCase()));
|
|
|
|
if (!isDynamic && !isExcluded) {
|
|
routes.push(publicSegments.length === 0 ? "/" : `/${publicSegments.join("/")}`);
|
|
}
|
|
}
|
|
|
|
for (const entry of entries) {
|
|
if (!entry.isDirectory() || entry.name.startsWith("_") || entry.name === "api") continue;
|
|
routes.push(...(await collectPages(path.join(directory, entry.name), [...segments, entry.name])));
|
|
}
|
|
|
|
return routes;
|
|
}
|
|
|
|
function sitemapEntry(route) {
|
|
if (route === "/") return { path: route, changefreq: "daily", priority: 1 };
|
|
if (route === "/portfolios" || route === "/blog") {
|
|
return { path: route, changefreq: "weekly", priority: 0.8 };
|
|
}
|
|
if (route === "/about") return { path: route, changefreq: "monthly", priority: 0.6 };
|
|
if (route === "/contact") return { path: route, changefreq: "yearly", priority: 0.5 };
|
|
return { path: route, changefreq: "monthly", priority: 0.6 };
|
|
}
|
|
|
|
const routes = [...new Set(await collectPages(appRoot))].sort((a, b) => {
|
|
if (a === "/") return -1;
|
|
if (b === "/") return 1;
|
|
return a.localeCompare(b, "en");
|
|
});
|
|
|
|
const config = {
|
|
baseUrl,
|
|
staticPages: routes.map(sitemapEntry),
|
|
};
|
|
|
|
await mkdir(outputDir, { recursive: true });
|
|
await writeFile(outputFile, `${JSON.stringify(config, null, 2)}\n`, "utf8");
|
|
console.log(`Generated ${path.relative(projectRoot, outputFile)} with ${routes.length} static routes.`);
|