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) { export default function middleware(request: NextRequest) {
const { pathname } = request.nextUrl; const { pathname } = request.nextUrl;
// Abaikan semua static file dan asset // --- IGNORE STATIC ASSET ---
const isStaticAsset = const isStaticAsset =
pathname.startsWith("/api") || pathname.startsWith("/api") ||
pathname.startsWith("/_next") || pathname.startsWith("/_next") ||
pathname.startsWith("/favicon") || pathname.startsWith("/favicon") ||
pathname.startsWith("/assets") || pathname.startsWith("/assets") ||
pathname.startsWith("/static") || pathname.startsWith("/static") ||
pathname.startsWith("/images") || pathname.match(/\.(png|jpg|jpeg|gif|webp|svg|ico|css|js)$/);
pathname.startsWith("/icons") ||
pathname.match(/\.(png|jpg|jpeg|gif|webp|svg|ico)$/);
if (isStaticAsset) { if (isStaticAsset) return NextResponse.next();
return NextResponse.next();
}
// Jika sudah dalam /in jalankan intl middleware // --- LOCALES YANG VALID ---
if (pathname.startsWith("/in")) { 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); return intlMiddleware(request);
} }
// Redirect otomatis ke /in // Jika URL TIDAK ada locale → redirect ke /in/<path>
const url = request.nextUrl.clone(); const url = request.nextUrl.clone();
url.pathname = `/in${pathname}`; url.pathname = `/in${pathname}`;
return NextResponse.redirect(url); return NextResponse.redirect(url);
} }
// Matcher untuk semua route kecuali static files
export const config = { 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"; // import createMiddleware from "next-intl/middleware";