63 lines
1.7 KiB
TypeScript
63 lines
1.7 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import type { NextRequest } from "next/server";
|
|
|
|
// This function can be marked `async` if using `await` inside
|
|
export async function middleware(request: NextRequest) {
|
|
// const locales = await fetch(CMS_API, {
|
|
// locales: true
|
|
// })
|
|
const locales = ["en", "sv", "no", "fi", "da", "de"];
|
|
|
|
const locale = locales.find(
|
|
(locale) =>
|
|
request.nextUrl.pathname.startsWith(`/${locale}/`) ||
|
|
request.nextUrl.pathname === `/${locale}`
|
|
);
|
|
|
|
if (!locale) {
|
|
//return <LocalePicker />
|
|
return Response.json("Not found!!!", { status: 404 });
|
|
}
|
|
|
|
// const data = await fetch(CMS_API, {
|
|
// uri: request.nextUrl.pathname,
|
|
// locale
|
|
// }).json()
|
|
|
|
//const contentType = data.response.meta.contentType;
|
|
const contentType = "currentContentPage";
|
|
|
|
const pathNameWithoutLocale = request.nextUrl.pathname.replace(
|
|
`/${locale}`,
|
|
""
|
|
);
|
|
|
|
const searchParams = new URLSearchParams(request.nextUrl.searchParams)
|
|
searchParams.set("uri", pathNameWithoutLocale)
|
|
|
|
switch (contentType) {
|
|
case "currentContentPage":
|
|
return NextResponse.rewrite(
|
|
new URL(
|
|
`/${locale}/current-content-page?${searchParams.toString()}`,
|
|
request.url
|
|
)
|
|
);
|
|
}
|
|
return NextResponse.redirect(new URL("/home", request.url));
|
|
}
|
|
|
|
// See "Matching Paths" below to learn more
|
|
export const config = {
|
|
matcher: [
|
|
/*
|
|
* Match all request paths except for the ones starting with:
|
|
* - api (API routes)
|
|
* - _next/static (static files)
|
|
* - _next/image (image optimization files)
|
|
* - favicon.ico (favicon file)
|
|
*/
|
|
"/((?!api|_next/static|_next/image|Static|imageVault|contentassets|favicon.ico).*)",
|
|
],
|
|
};
|