|
| 1 | +import { readdirSync, statSync } from "node:fs"; |
| 2 | +import { join } from "node:path"; |
| 3 | + |
| 4 | +/** |
| 5 | + * Discovers all page routes from the Next.js app directory. |
| 6 | + * |
| 7 | + * This scans the app directory for all page.tsx files and converts |
| 8 | + * them to URL paths, ensuring the sitemap is always exhaustive. |
| 9 | + * |
| 10 | + * Route conventions handled: |
| 11 | + * - (group) folders - stripped from URL path |
| 12 | + * - [[...slug]] - catch-all routes (handled by fumadocs) |
| 13 | + * - [...slug] - catch-all routes (handled by fumadocs) |
| 14 | + * - [param] - dynamic routes (skipped, handled by fumadocs) |
| 15 | + * - page.tsx at root - becomes "/" |
| 16 | + */ |
| 17 | + |
| 18 | +/** Routes that should be excluded from the sitemap */ |
| 19 | +const EXCLUDED_ROUTES = new Set([ |
| 20 | + "/confirm", // Thank you page, not meant for SEO indexing |
| 21 | +]); |
| 22 | + |
| 23 | +/** |
| 24 | + * Check if a route segment is a route group (parentheses) |
| 25 | + */ |
| 26 | +function isRouteGroup(segment: string): boolean { |
| 27 | + return segment.startsWith("(") && segment.endsWith(")"); |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Check if a route segment is dynamic (brackets) |
| 32 | + */ |
| 33 | +function isDynamicSegment(segment: string): boolean { |
| 34 | + return segment.startsWith("[") && segment.endsWith("]"); |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Recursively find all page.tsx files in a directory |
| 39 | + */ |
| 40 | +function findPageFiles(dir: string, basePath: string = ""): Array<string> { |
| 41 | + const pages: Array<string> = []; |
| 42 | + |
| 43 | + let entries: Array<string>; |
| 44 | + try { |
| 45 | + entries = readdirSync(dir); |
| 46 | + } catch { |
| 47 | + return pages; |
| 48 | + } |
| 49 | + |
| 50 | + for (const entry of entries) { |
| 51 | + const fullPath = join(dir, entry); |
| 52 | + |
| 53 | + let stat; |
| 54 | + try { |
| 55 | + stat = statSync(fullPath); |
| 56 | + } catch { |
| 57 | + continue; |
| 58 | + } |
| 59 | + |
| 60 | + if (stat.isDirectory()) { |
| 61 | + // Skip node_modules and hidden directories |
| 62 | + if (entry.startsWith(".") || entry === "node_modules") { |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + // Skip api routes - they're not pages |
| 67 | + if (entry === "api") { |
| 68 | + continue; |
| 69 | + } |
| 70 | + |
| 71 | + // Build the URL path segment |
| 72 | + let urlSegment: string; |
| 73 | + if (isRouteGroup(entry)) { |
| 74 | + // Route groups don't affect the URL |
| 75 | + urlSegment = ""; |
| 76 | + } else if (isDynamicSegment(entry)) { |
| 77 | + // Dynamic segments are handled by fumadocs loaders |
| 78 | + // We still recurse to find static pages within |
| 79 | + urlSegment = entry; |
| 80 | + } else { |
| 81 | + urlSegment = entry; |
| 82 | + } |
| 83 | + |
| 84 | + const newBasePath = urlSegment |
| 85 | + ? basePath |
| 86 | + ? `${basePath}/${urlSegment}` |
| 87 | + : urlSegment |
| 88 | + : basePath; |
| 89 | + |
| 90 | + pages.push(...findPageFiles(fullPath, newBasePath)); |
| 91 | + } else if (entry === "page.tsx" || entry === "page.ts") { |
| 92 | + // Found a page file |
| 93 | + // Skip if the path contains dynamic segments (handled by fumadocs) |
| 94 | + if (!basePath.includes("[")) { |
| 95 | + const urlPath = basePath ? `/${basePath}` : "/"; |
| 96 | + pages.push(urlPath); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + return pages; |
| 102 | +} |
| 103 | + |
| 104 | +/** |
| 105 | + * Convert app directory path to URL path |
| 106 | + */ |
| 107 | +export function discoverStaticRoutes(appDir: string): Array<string> { |
| 108 | + const routes = findPageFiles(appDir); |
| 109 | + |
| 110 | + // Filter out excluded routes and deduplicate |
| 111 | + const uniqueRoutes = [...new Set(routes)].filter( |
| 112 | + (route) => !EXCLUDED_ROUTES.has(route) |
| 113 | + ); |
| 114 | + |
| 115 | + return uniqueRoutes.sort(); |
| 116 | +} |
| 117 | + |
| 118 | +/** |
| 119 | + * Get the app directory path relative to the current working directory |
| 120 | + */ |
| 121 | +export function getAppDirectory(): string { |
| 122 | + // In Next.js, the app directory is at the root of the project |
| 123 | + // This function returns the path that should work both in development |
| 124 | + // and when the code is run from the project root |
| 125 | + return join(process.cwd(), "app"); |
| 126 | +} |
0 commit comments