From 1f62ccc8a32f88935d1be0430d8833ccfaa0e190 Mon Sep 17 00:00:00 2001 From: Dayuan Jiang Date: Tue, 30 Dec 2025 20:39:15 +0900 Subject: [PATCH] fix: use proxy.ts instead of middleware.ts for Next.js 16 --- middleware.ts => proxy.ts | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) rename middleware.ts => proxy.ts (75%) diff --git a/middleware.ts b/proxy.ts similarity index 75% rename from middleware.ts rename to proxy.ts index 6611433..1db77c6 100644 --- a/middleware.ts +++ b/proxy.ts @@ -4,13 +4,7 @@ import type { NextRequest } from "next/server" import { NextResponse } from "next/server" import { i18n } from "./lib/i18n/config" -function getLocale(request: NextRequest): string { - // Check for saved locale in cookie first - const cookieLocale = request.cookies.get("NEXT_LOCALE")?.value - if (cookieLocale && i18n.locales.includes(cookieLocale as any)) { - return cookieLocale - } - +function getLocale(request: NextRequest): string | undefined { // Negotiator expects plain object so we need to transform headers const negotiatorHeaders: Record = {} request.headers.forEach((value, key) => { @@ -26,10 +20,11 @@ function getLocale(request: NextRequest): string { ) const locale = matchLocale(languages, locales, i18n.defaultLocale) + return locale } -export function middleware(request: NextRequest) { +export function proxy(request: NextRequest) { const pathname = request.nextUrl.pathname // Skip API routes, static files, and Next.js internals @@ -53,20 +48,12 @@ export function middleware(request: NextRequest) { const locale = getLocale(request) // Redirect to localized path - const response = NextResponse.redirect( + return NextResponse.redirect( new URL( `/${locale}${pathname.startsWith("/") ? "" : "/"}${pathname}`, request.url, ), ) - - // Set locale cookie for future visits - response.cookies.set("NEXT_LOCALE", locale, { - maxAge: 60 * 60 * 24 * 365, // 1 year - path: "/", - }) - - return response } }