-
Notifications
You must be signed in to change notification settings - Fork 1
/
middleware.ts
34 lines (28 loc) · 1.01 KB
/
middleware.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { type NextFetchEvent, NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { getRewritesFromBackend } from "./getRewritesFromBackend";
// This function can be marked `async` if using `await` inside
export async function middleware(request: NextRequest, event: NextFetchEvent) {
const { pathname } = request.nextUrl;
// Forward these standard requests
if (
pathname.startsWith("/_next") ||
pathname.startsWith("/favicon.ico") ||
pathname.startsWith("/api")
) {
return NextResponse.next();
}
// Simulate getting the rewrites from a backend (like a CMS)
const rewrites = await getRewritesFromBackend();
// Check if the pathname is in the rewrites object
if (pathname in rewrites) {
const { rewriteUrl, locale } = rewrites[pathname as keyof typeof rewrites];
// Choose which page template to use
return NextResponse.rewrite(
new URL(`/${locale}${rewriteUrl}`, request.url)
);
}
}
export const config = {
matcher: ["/:path*"],
};