Skip to content

Commit 2320e78

Browse files
authored
Fix Sitemap (#647)
* Fix Sitemap * cache sitemap * sanitize better
1 parent 79a8401 commit 2320e78

6 files changed

Lines changed: 151 additions & 94 deletions

File tree

app/sitemap.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import fs from "node:fs/promises";
2+
import path from "node:path";
3+
import type { MetadataRoute } from "next";
4+
5+
const SITE_URL = process.env.SITE_URL ?? "https://docs.arcade.dev";
6+
const NORMALIZED_SITE_URL = SITE_URL.replace(/\/+$/, "");
7+
const APP_DIR = path.join(process.cwd(), "app");
8+
const SKIP_DIRS = new Set(["_meta", "_api", "_redirects", "api"]);
9+
const INDEX_SUFFIX_REGEX = /\/index$/;
10+
let cachedRoutes: Promise<MetadataRoute.Sitemap> | null = null;
11+
12+
async function collectRoutes(dir: string): Promise<MetadataRoute.Sitemap> {
13+
const dirs = await fs.readdir(dir, { withFileTypes: true });
14+
const entries: MetadataRoute.Sitemap = [];
15+
16+
for (const entry of dirs) {
17+
if (entry.isDirectory()) {
18+
if (SKIP_DIRS.has(entry.name) || entry.name.includes("[")) {
19+
continue;
20+
}
21+
const childRoutes = await collectRoutes(path.join(dir, entry.name));
22+
entries.push(...childRoutes);
23+
continue;
24+
}
25+
26+
if (entry.isFile() && entry.name === "page.mdx") {
27+
const filePath = path.join(dir, entry.name);
28+
const stats = await fs.stat(filePath);
29+
30+
const relativeDir = path
31+
.relative(APP_DIR, dir)
32+
.replace(/\\/g, "/")
33+
.replace(INDEX_SUFFIX_REGEX, "");
34+
35+
const routePath = relativeDir ? `/${relativeDir}` : "/";
36+
entries.push({
37+
url: `${NORMALIZED_SITE_URL}${routePath}`,
38+
lastModified: stats.mtime,
39+
});
40+
}
41+
}
42+
43+
return entries;
44+
}
45+
46+
export default function sitemap(): Promise<MetadataRoute.Sitemap> {
47+
if (!cachedRoutes) {
48+
cachedRoutes = collectRoutes(APP_DIR).then((routes) => {
49+
routes.sort((a, b) => a.url.localeCompare(b.url));
50+
return routes;
51+
});
52+
}
53+
54+
return cachedRoutes;
55+
}

next-env.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// <reference types="next" />
22
/// <reference types="next/image-types/global" />
3-
import "./.next/types/routes.d.ts";
3+
import "./.next/dev/types/routes.d.ts";
44

55
// NOTE: This file should not be edited
66
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.

next-sitemap.config.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"lint": "pnpm dlx ultracite check",
1111
"format": "pnpm dlx ultracite fix",
1212
"prepare": "husky install",
13-
"postbuild": "pnpm run custompagefind && next-sitemap",
13+
"postbuild": "pnpm run custompagefind",
1414
"translate": "pnpm dlx tsx scripts/i18n-sync/index.ts && pnpm format",
1515
"sync:metas": "pnpm dlx tsx scripts/sync-metas.ts app/en",
1616
"llmstxt": "pnpm dlx tsx scripts/generate-llmstxt.ts",
@@ -40,8 +40,7 @@
4040
"lucide-react": "0.548.0",
4141
"mdast-util-to-string": "4.0.0",
4242
"motion": "12.23.24",
43-
"next": "16.0.10",
44-
"next-sitemap": "4.2.3",
43+
"next": "16.1.1",
4544
"nextra": "4.6.0",
4645
"nextra-theme-docs": "4.6.0",
4746
"posthog-js": "1.298.0",

0 commit comments

Comments
 (0)