Skip to content
This repository has been archived by the owner on Sep 18, 2024. It is now read-only.

Commit

Permalink
working dynamic sitemap
Browse files Browse the repository at this point in the history
  • Loading branch information
rylew1 committed Jun 14, 2024
1 parent 2db4d35 commit 6cd4e58
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
16 changes: 16 additions & 0 deletions frontend/src/app/sitemap.ts
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;
}
10 changes: 5 additions & 5 deletions frontend/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { NextRequest, NextResponse } from "next/server";
import { defaultLocale, locales } from "./i18n/config";

import { FeatureFlagsManager } from "./services/FeatureFlagManager";
/**
* @file Middleware allows you to run code before a request is completed. Then, based on
* the incoming request, you can modify the response by rewriting, redirecting,
* modifying the request or response headers, or responding directly.
* @see https://nextjs.org/docs/app/building-your-application/routing/middleware
*/
import createIntlMiddleware from "next-intl/middleware";
import { NextRequest, NextResponse } from "next/server";
import { defaultLocale, locales } from "./i18n/config";

import { FeatureFlagsManager } from "./services/FeatureFlagManager";

export const config = {
matcher: [
Expand All @@ -19,7 +19,7 @@ export const config = {
* - _next/image (image optimization files)
* - images (static files in public/images/ directory)
*/
"/((?!api|_next/static|_next/image|public|img|uswds|images|robots.txt|site.webmanifest).*)",
"/((?!api|_next/static|_next/image|sitemap|public|img|uswds|images|robots.txt|site.webmanifest).*)",
/**
* Fix issue where the pattern above was causing middleware
* to not run on the homepage:
Expand Down
41 changes: 41 additions & 0 deletions frontend/src/utils/getRoutes.ts
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;
}

0 comments on commit 6cd4e58

Please sign in to comment.