import createMiddleware from "next-intl/middleware"; import { NextRequest, NextResponse } from "next/server"; import { routing } from "./i18n/routing"; const intlMiddleware = createMiddleware(routing); export default function middleware(request: NextRequest) { const { pathname } = request.nextUrl; // --- IGNORE STATIC ASSET --- const isStaticAsset = pathname.startsWith("/api") || pathname.startsWith("/_next") || pathname.startsWith("/favicon") || pathname.startsWith("/assets") || pathname.startsWith("/static") || pathname.match(/\.(png|jpg|jpeg|gif|webp|svg|ico|css|js)$/); if (isStaticAsset) return NextResponse.next(); // --- 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); } // Jika URL TIDAK ada locale → redirect ke /in/ 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).*)"], }; // 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*"], // };