Skip to content

Commit 8836c21

Browse files
IgboPharaohEmmanuel-Develops
authored andcommitted
feat: add middleware setup (#36)
1 parent e84173a commit 8836c21

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
/public/categories.json
1919
/public/types-data.json
2020
/public/tag-data.json
21+
/public/aliases.json
2122

2223
# misc
2324
.DS_Store

contentlayer.config.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,22 @@ function createTagCount(allTranscripts: ContentTranscriptType[]): { tagCounts: R
4646
return { tagCounts };
4747
}
4848

49+
const getTranscriptAliases = (allTranscripts: ContentTranscriptType[]) => {
50+
const aliases: Record<string, string> = {};
51+
52+
for (const transcript of allTranscripts) {
53+
if (!transcript.aliases) continue;
54+
55+
if (transcript.aliases) {
56+
transcript.aliases.forEach((alias) => {
57+
aliases[alias] = transcript.url;
58+
});
59+
}
60+
}
61+
62+
writeFileSync("./public/aliases.json", JSON.stringify(aliases));
63+
};
64+
4965
const getCategories = () => {
5066
const filePath = path.join(process.cwd(), "public", "categories.json");
5167
const fileContents = fs.readFileSync(filePath, "utf8");
@@ -205,5 +221,6 @@ export default makeSource({
205221
const { allDocuments } = await importData();
206222
organizeTags(allDocuments);
207223
createTypesCount(allDocuments);
224+
getTranscriptAliases(allDocuments);
208225
},
209226
});

src/middleware.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { NextResponse } from "next/server";
2+
import type { NextRequest } from "next/server";
3+
import aliasesJson from "../public/aliases.json";
4+
5+
const aliases = aliasesJson as { [key: string]: string };
6+
7+
export function middleware(req: NextRequest) {
8+
const { pathname } = req.nextUrl;
9+
const alias = aliases[pathname];
10+
11+
if (alias) {
12+
return NextResponse.redirect(new URL(alias, req.url));
13+
}
14+
return NextResponse.next();
15+
}
16+
17+
export const config = {
18+
matcher: [
19+
"/:path/:subpath*", // Matches any path with at least one subdirectory
20+
"/:path*/:file((?!.*\\.).*)", // Exclude paths with file extensions
21+
"/((?!api|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt|plugins).*)", // Exclude specific Next.js paths and API routes
22+
],
23+
};

0 commit comments

Comments
 (0)