fix: redirect /in and /en

This commit is contained in:
Sabda Yagra 2025-11-14 09:26:50 +07:00
parent 0c5a7f86bb
commit 3b0bf64e39
1 changed files with 16 additions and 11 deletions

View File

@ -7,34 +7,39 @@ const intlMiddleware = createMiddleware(routing);
export default function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
// Abaikan semua static file dan asset
// --- IGNORE STATIC ASSET ---
const isStaticAsset =
pathname.startsWith("/api") ||
pathname.startsWith("/_next") ||
pathname.startsWith("/favicon") ||
pathname.startsWith("/assets") ||
pathname.startsWith("/static") ||
pathname.startsWith("/images") ||
pathname.startsWith("/icons") ||
pathname.match(/\.(png|jpg|jpeg|gif|webp|svg|ico)$/);
pathname.match(/\.(png|jpg|jpeg|gif|webp|svg|ico|css|js)$/);
if (isStaticAsset) {
return NextResponse.next();
}
if (isStaticAsset) return NextResponse.next();
// Jika sudah dalam /in jalankan intl middleware
if (pathname.startsWith("/in")) {
// --- LOCALES YANG VALID ---
const locales = ["in", "en"];
// Ambil locale utama dari URL
const firstSegment = pathname.split("/")[1];
const isLocaleURL = locales.includes(firstSegment);
// Jika URL sudah mengandung locale → JALANKAN next-intl
if (isLocaleURL) {
return intlMiddleware(request);
}
// Redirect otomatis ke /in
// Jika URL TIDAK ada locale → redirect ke /in/<path>
const url = request.nextUrl.clone();
url.pathname = `/in${pathname}`;
return NextResponse.redirect(url);
}
// Matcher untuk semua route kecuali static files
export const config = {
matcher: ["/((?!_next|api|favicon.ico|assets|static|images|icons).*)"],
matcher: ["/((?!_next|api|favicon.ico|assets|static).*)"],
};
// import createMiddleware from "next-intl/middleware";