This repository has been archived by the owner on Sep 18, 2024. It is now read-only.
forked from HHS/simpler-grants-gov
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
62 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { MetadataRoute } from "next"; | ||
import { getNextRoutes } from "../utils/getRoutes"; | ||
|
||
export default function sitemap(): MetadataRoute.Sitemap { | ||
const routes = getNextRoutes("./src/app"); | ||
|
||
const baseUrl = process.env.NEXT_PUBLIC_BASE_URL || "http://localhost:3000"; | ||
const sitemap: MetadataRoute.Sitemap = routes.map((route) => ({ | ||
url: `${baseUrl}${route || ""}`, | ||
lastModified: new Date().toISOString(), | ||
changeFrequency: "weekly", | ||
priority: 0.5, | ||
})); | ||
|
||
return sitemap; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import fs from "node:fs"; | ||
import path from "node:path"; | ||
|
||
// Helper function to list all paths recursively | ||
function listPaths(dir: string): string[] { | ||
let fileList: string[] = []; | ||
const files = fs.readdirSync(dir); | ||
files.forEach((file) => { | ||
const filePath = path.join(dir, file); | ||
if (fs.statSync(filePath).isDirectory()) { | ||
fileList = fileList.concat(listPaths(filePath)); | ||
} else { | ||
fileList.push(filePath); | ||
} | ||
}); | ||
return fileList; | ||
} | ||
|
||
// Function to get the Next.js routes | ||
export function getNextRoutes(src: string): string[] { | ||
|
||
// Get all paths from the `app` directory | ||
const appPaths = listPaths(src).filter((file) => file.endsWith("page.tsx")); | ||
|
||
// Extract the route name for each `page.tsx` file | ||
// Basically anything between [locale] and /page.tsx is removed, | ||
// which lets us get nested routes such as /newsletter/unsubscribe | ||
const appRoutes = appPaths.map((filePath) => { | ||
const relativePath = path.relative(src, filePath); | ||
const route = relativePath | ||
? "/" + | ||
relativePath | ||
.replace("/page.tsx", "") | ||
.replace(/\[locale\]/g, "") | ||
.replace(/\\/g, "/") | ||
: "/"; | ||
return route.replace(/\/\//g, "/"); | ||
}); | ||
console.log("appRoutes => ", appRoutes); | ||
return appRoutes; | ||
} |