Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions docs/app/sitemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]> {
/**
* Recursively retrieves MDX/MD file paths with a shared accumulator to minimize memory allocations.
*/
async function getMdxFiles(
dir: string,
baseDir: string,
results: string[] = [],
): Promise<string[]> {
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"))
Expand All @@ -30,13 +37,12 @@ async function getMdxFiles(dir: string, baseDir: string): Promise<string[]> {
} else if (relativePath === "index") {
relativePath = "";
}
return [relativePath];
results.push(relativePath);
}
return [];
}),
);

return pathsArray.flat();
return results;
}

export const dynamic = "force-static";
Expand All @@ -47,11 +53,12 @@ export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
}

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,
}));
}