-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathsitemap.ts
More file actions
82 lines (72 loc) · 2.53 KB
/
sitemap.ts
File metadata and controls
82 lines (72 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { promises as fs } from "fs";
import path from "path";
import { getBlogPosts, getDocs } from "@/utils/blog";
import { seoPages } from "../lib/seo-pages";
async function getPagePaths(
dir: string
): Promise<{ path: string; lastModified: string }[]> {
const entries = await fs.readdir(dir, { withFileTypes: true });
const paths: { path: string; lastModified: string }[] = [];
for (const entry of entries) {
const fullPath = path.join(dir, entry.name);
if (
entry.isDirectory() &&
entry.name !== "dashboard" &&
entry.name !== "blog" &&
!entry.name.startsWith("[")
) {
const subPaths = await getPagePaths(fullPath);
paths.push(...subPaths);
} else if (
entry.isFile() &&
(entry.name === "page.tsx" || entry.name === "page.mdx")
) {
const relativePath = path.relative(process.cwd(), dir);
const routePath = "/" + relativePath.split(path.sep).slice(1).join("/");
if (!routePath.includes("/dashboard") && !routePath.includes("[")) {
const stats = await fs.stat(fullPath);
paths.push({
path: routePath === "/app" ? "/" : routePath,
lastModified: stats.mtime.toISOString(),
});
}
}
}
return paths;
}
export default async function sitemap() {
const appDirectory = path.join(process.cwd(), "app");
const pagePaths = await getPagePaths(appDirectory);
// Add blog post routes
const blogPosts = getBlogPosts();
const blogRoutes = blogPosts.map((post) => {
const publishDate = new Date(post.metadata.publishedAt);
publishDate.setHours(9, 0, 0, 0); // Set time to 9:00 AM
return {
path: `/blog/${post.slug}`,
lastModified: publishDate.toISOString(),
};
});
// Add docs routes
const docs = getDocs();
const docsRoutes = docs.map((doc) => ({
path: `/docs/${doc.slug}`,
lastModified: new Date().toISOString(), // You might want to add a publishedAt to doc metadata
}));
// Add SEO pages
const seoRoutes = Object.keys(seoPages).map((slug) => ({
path: `/${slug}`,
lastModified: new Date().toISOString(),
}));
// Combine routes and ensure '/' is first
const allRoutes = [...pagePaths, ...blogRoutes, ...docsRoutes, ...seoRoutes];
const homeRoute = allRoutes.find((route) => route.path === "/");
const otherRoutes = allRoutes.filter((route) => route.path !== "/");
const routes = [...(homeRoute ? [homeRoute] : []), ...otherRoutes].map(
(route) => ({
url: `https://cap.so${route.path}`,
lastModified: route.lastModified,
})
);
return routes;
}