mediahub-fe/middleware.ts

66 lines
1.9 KiB
TypeScript
Raw Normal View History

import createMiddleware from "next-intl/middleware";
import { NextRequest, NextResponse } from "next/server";
2025-05-22 09:53:54 +00:00
import { routing } from "./i18n/routing";
2024-11-26 03:09:48 +00:00
2025-11-13 10:46:23 +00:00
const intlMiddleware = createMiddleware(routing);
2025-11-13 10:46:23 +00:00
export default function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
2024-11-26 03:09:48 +00:00
2025-11-13 10:46:23 +00:00
// Abaikan asset & API
if (
pathname.startsWith("/api") ||
pathname.startsWith("/_next") ||
pathname.startsWith("/favicon") ||
pathname.startsWith("/assets")
) {
return NextResponse.next();
}
2024-11-26 03:09:48 +00:00
2025-11-13 10:46:23 +00:00
// Jika sudah mengandung /in → pakai next-intl
if (pathname.startsWith("/in")) {
return intlMiddleware(request);
}
2025-05-22 09:53:54 +00:00
2025-11-13 10:46:23 +00:00
// Jika TIDAK mengandung /in → selalu tambahkan /in
// Contoh:
// /image/detail/... → /in/image/detail/...
const url = request.nextUrl.clone();
url.pathname = `/in${pathname}`;
return NextResponse.redirect(url);
}
2024-11-26 03:09:48 +00:00
export const config = {
2025-11-13 10:46:23 +00:00
matcher: ["/((?!_next|api|favicon.ico|assets).*)"],
};
2025-11-13 10:46:23 +00:00
// import createMiddleware from "next-intl/middleware";
// import { NextRequest, NextResponse } from "next/server";
// import { locales } from "@/config";
// import { routing } from "./i18n/routing";
// // export default async function middleware(request: NextRequest) {
// // // Step 1: Use the incoming request (example)
// // const defaultLocale = "in";
// // // const defaultLocale = request.headers.get("dashcode-locale") || "in";
// // // Step 2: Create and call the next-intl middleware (example)
// // const handleI18nRouting = createMiddleware({
// // locales: ["in", "en"],
// // defaultLocale: "in",
// // });
// // const response = handleI18nRouting(request);
// // // Step 3: Alter the response (example)
// // response.headers.set("dashcode-locale", defaultLocale);
// // return response;
// // }
// export default createMiddleware(routing);
// export const config = {
// // Match only internationalized pathnames
// matcher: ["/", "/(in|en)/:path*"],
// };