105 lines
2.4 KiB
TypeScript
105 lines
2.4 KiB
TypeScript
import { readdirSync } from "node:fs";
|
|
import { join } from "node:path";
|
|
|
|
export const dynamic = "force-static";
|
|
export const runtime = "nodejs";
|
|
|
|
type StaticPage = {
|
|
path: string;
|
|
changefreq: "daily" | "weekly" | "monthly";
|
|
priority: number;
|
|
};
|
|
|
|
const EXCLUDED_SEGMENTS = new Set([
|
|
"account",
|
|
"admin",
|
|
"api",
|
|
"auth",
|
|
"cart",
|
|
"checkout",
|
|
"login",
|
|
"meshkee",
|
|
"register",
|
|
]);
|
|
|
|
function collectStaticPagePaths(
|
|
directory: string,
|
|
routeSegments: string[] = [],
|
|
): string[] {
|
|
const entries = readdirSync(directory, { withFileTypes: true });
|
|
const paths: string[] = [];
|
|
|
|
if (entries.some((entry) => entry.isFile() && /^page\.[jt]sx?$/.test(entry.name))) {
|
|
const path = routeSegments.length ? `/${routeSegments.join("/")}` : "/";
|
|
paths.push(path);
|
|
}
|
|
|
|
for (const entry of entries) {
|
|
if (!entry.isDirectory()) continue;
|
|
|
|
const isRouteGroup = entry.name.startsWith("(") && entry.name.endsWith(")");
|
|
const isPrivateFolder = entry.name.startsWith("_");
|
|
const isParallelRoute = entry.name.startsWith("@");
|
|
const isDynamicRoute = entry.name.includes("[");
|
|
|
|
if (
|
|
isPrivateFolder ||
|
|
isParallelRoute ||
|
|
isDynamicRoute ||
|
|
EXCLUDED_SEGMENTS.has(entry.name)
|
|
) {
|
|
continue;
|
|
}
|
|
|
|
paths.push(
|
|
...collectStaticPagePaths(
|
|
join(directory, entry.name),
|
|
isRouteGroup ? routeSegments : [...routeSegments, entry.name],
|
|
),
|
|
);
|
|
}
|
|
|
|
return paths;
|
|
}
|
|
|
|
function toStaticPage(path: string): StaticPage {
|
|
if (path === "/") {
|
|
return { path, changefreq: "daily", priority: 1 };
|
|
}
|
|
|
|
if (path === "/about-us") {
|
|
return { path, changefreq: "monthly", priority: 0.6 };
|
|
}
|
|
|
|
if (path === "/contact-us") {
|
|
return { path, changefreq: "monthly", priority: 0.5 };
|
|
}
|
|
|
|
return { path, changefreq: "weekly", priority: 0.5 };
|
|
}
|
|
|
|
export function GET() {
|
|
const baseUrl = (
|
|
process.env.NEXT_PUBLIC_SITE_URL ||
|
|
process.env.SITE_URL ||
|
|
"https://samanandish.com"
|
|
).replace(/\/$/, "");
|
|
|
|
const staticPages = collectStaticPagePaths(join(process.cwd(), "app"))
|
|
.sort((first, second) => {
|
|
if (first === "/") return -1;
|
|
if (second === "/") return 1;
|
|
return first.localeCompare(second);
|
|
})
|
|
.map(toStaticPage);
|
|
|
|
return Response.json(
|
|
{ baseUrl, staticPages },
|
|
{
|
|
headers: {
|
|
"Cache-Control": "public, max-age=300, stale-while-revalidate=3600",
|
|
},
|
|
},
|
|
);
|
|
}
|