Skip to content

Commit e2a8a67

Browse files
Add sitemap and robots files (#76)
1 parent 4e4cf80 commit e2a8a67

4 files changed

Lines changed: 89 additions & 0 deletions

File tree

app/robots.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { MetadataRoute } from "next";
2+
3+
export default function robots(): MetadataRoute.Robots {
4+
return {
5+
rules: {
6+
userAgent: "*",
7+
allow: "/",
8+
},
9+
sitemap: "https://typescript.cur8d.dev/sitemap.xml",
10+
};
11+
}

app/sitemap.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import type { MetadataRoute } from "next";
2+
3+
export default function sitemap(): MetadataRoute.Sitemap {
4+
return [
5+
{
6+
url: "https://typescript.cur8d.dev",
7+
lastModified: new Date(),
8+
changeFrequency: "monthly",
9+
priority: 1,
10+
},
11+
];
12+
}

docs/app/robots.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type { MetadataRoute } from "next";
2+
3+
export const dynamic = "force-static";
4+
5+
export default function robots(): MetadataRoute.Robots {
6+
return {
7+
rules: {
8+
userAgent: "*",
9+
allow: "/",
10+
},
11+
sitemap: "https://cur8d.dev/typescript/sitemap.xml",
12+
};
13+
}

docs/app/sitemap.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import type { MetadataRoute } from "next";
2+
import fs from "node:fs";
3+
import path from "node:path";
4+
5+
const BASE_URL = "https://cur8d.dev/typescript";
6+
// Next.js runs from the project root during build
7+
const CONTENT_DIR = path.join(process.cwd(), "content");
8+
9+
function getMdxFiles(dir: string, baseDir: string): string[] {
10+
if (!fs.existsSync(dir)) {
11+
return [];
12+
}
13+
14+
const files = fs.readdirSync(dir);
15+
let paths: string[] = [];
16+
17+
for (const file of files) {
18+
const fullPath = path.join(dir, file);
19+
const stat = fs.statSync(fullPath);
20+
21+
if (stat.isDirectory()) {
22+
paths = paths.concat(getMdxFiles(fullPath, baseDir));
23+
} else if (file.endsWith(".mdx") || file.endsWith(".md")) {
24+
let relativePath = path.relative(baseDir, fullPath);
25+
// Normalize path separators for URLs (handle Windows)
26+
relativePath = relativePath.split(path.sep).join("/");
27+
// Remove extension
28+
relativePath = relativePath.replace(/\.mdx?$/, "");
29+
// Handle index files
30+
if (relativePath.endsWith("/index")) {
31+
relativePath = relativePath.slice(0, -6);
32+
} else if (relativePath === "index") {
33+
relativePath = "";
34+
}
35+
paths.push(relativePath);
36+
}
37+
}
38+
39+
return paths;
40+
}
41+
42+
export const dynamic = "force-static";
43+
44+
export default function sitemap(): MetadataRoute.Sitemap {
45+
const paths = getMdxFiles(CONTENT_DIR, CONTENT_DIR);
46+
47+
return paths.map((p) => ({
48+
url: `${BASE_URL}/${p}${p ? "/" : ""}`,
49+
lastModified: new Date(),
50+
changeFrequency: "monthly",
51+
priority: p === "" ? 1 : 0.8,
52+
}));
53+
}

0 commit comments

Comments
 (0)