diff --git a/docs/app/sitemap.ts b/docs/app/sitemap.ts index edacb07..68fe4bb 100644 --- a/docs/app/sitemap.ts +++ b/docs/app/sitemap.ts @@ -6,15 +6,22 @@ const BASE_URL = "https://cur8d.dev/typescript"; // Next.js runs from the project root during build const CONTENT_DIR = path.join(process.cwd(), "content"); -async function getMdxFiles(dir: string, baseDir: string): Promise { +/** + * Recursively retrieves MDX/MD file paths with a shared accumulator to minimize memory allocations. + */ +async function getMdxFiles( + dir: string, + baseDir: string, + results: string[] = [], +): Promise { const entries = await fs.promises.readdir(dir, { withFileTypes: true }); - const pathsArray = await Promise.all( + await Promise.all( entries.map(async (entry) => { const fullPath = path.join(dir, entry.name); if (entry.isDirectory()) { - return getMdxFiles(fullPath, baseDir); + await getMdxFiles(fullPath, baseDir, results); } else if ( entry.isFile() && (entry.name.endsWith(".mdx") || entry.name.endsWith(".md")) @@ -30,13 +37,12 @@ async function getMdxFiles(dir: string, baseDir: string): Promise { } else if (relativePath === "index") { relativePath = ""; } - return [relativePath]; + results.push(relativePath); } - return []; }), ); - return pathsArray.flat(); + return results; } export const dynamic = "force-static"; @@ -47,11 +53,12 @@ export default async function sitemap(): Promise { } const paths = await getMdxFiles(CONTENT_DIR, CONTENT_DIR); + const now = new Date(); return paths.map((p) => ({ url: `${BASE_URL}/${p}${p ? "/" : ""}`, - lastModified: new Date(), - changeFrequency: "monthly", + lastModified: now, + changeFrequency: "monthly" as const, priority: p === "" ? 1 : 0.8, })); }