import type { NextConfig } from "next";
import { locales, defaultLocale } from "@/lib/i18n";
const nextConfig: NextConfig = {
/* config options here */
i18n: {
locales: [...locales],
defaultLocale,
// @ts-ignore
localeDetection: true,
},
skipMiddlewareUrlNormalize: true,
};
export default nextConfig;
< /code>
i18n.ts:
export const locales = ["en", "ja"] as const;
export const defaultLocale = "en";
< /code>
middleware.ts:
import { NextRequest, NextResponse } from "next/server";
import { locales, defaultLocale } from "@/lib/i18n";
// Pattern to exclude static files and Next.js internals
const PUBLIC_FILE = /\.(.*)$/;
export async function middleware(req: NextRequest) {
console.log("");
console.log("");
console.log("");
console.log("");
console.log("");
const url = req.nextUrl.clone();
const { pathname } = url;
// DEBUG: Log what we're working with
console.log("🔍 Middleware Debug:");
console.log(" pathname:", pathname);
console.log(" locales:", locales);
console.log(" defaultLocale:", defaultLocale);
// Skip API root, routes, _next, and public files
// also Skips 404s, so they can be handled with
if (
pathname === "/" || // allows root to pass through
locales.some(loc => pathname.startsWith(`/${loc}/`)) ||
PUBLIC_FILE.test(pathname) ||
pathname.startsWith("/api") ||
pathname.startsWith("/_next") ||
//404/error skips to not-found.tsx
pathname === "/404" ||
pathname === "/_error" ||
pathname === `/${defaultLocale}/404`
) {
console.log("1");
return NextResponse.next();
}
// filters into array, gets the first word in path
const segments = pathname.split("/").filter(Boolean);
const [first, ...rest] = segments;
console.log("firstFIRSTFIRSTFIRST", first);
console.log("URL URL URL URL:", url.pathname);
// handles perfectly inputted urls
if (first && locales.includes(first as (typeof locales)[number])) {
console.log("2");
return NextResponse.next();
}
// creates the correct locale for the user
const rawLang = req.headers.get("accept-language")?.split(",")[0].split("-")[0] || defaultLocale;
const finalLocale = locales.includes(rawLang as (typeof locales)[number]) ? rawLang : defaultLocale;
console.log("FINAL LOCALE FINAL LOCALE:", finalLocale);
// handles directory without locale
if (segments.length === 1) {
const redirectUrl = new URL(`/ja/catalogue`, req.url);
url.pathname = `/${finalLocale}/${segments[0]}`;
console.log("3");
console.log("3 pathname sending...:", redirectUrl.pathname);
return NextResponse.redirect(redirectUrl);
}
// handles bogus locales but solid directiory
if (first && rest.length > 0 && !locales.includes(first as (typeof locales)[number])) {
url.pathname = `/${finalLocale}/${rest.join("/")}`;
console.log("4");
console.log(url);
console.log(url.pathname);
return NextResponse.redirect(url);
}
console.log("5");
return NextResponse.next();
}
// checks to even bother running the middleware if these are in the url
export const config = {
matcher: ["/((?!api|_next|.*\\..*).*)"],
};
Я попытался добавить Skipmiddlewareurlnormalize: true, в my next.config.ts. Таким образом, бесконечный цикл промежуточного программного обеспечения продолжается. < /p>
Вот журналы: < /p>
Вот весь мой код. Он пронизан комментариями, но, кроме того, я не понимаю, как он бесконечно зацикливается < /p> next.config.ts: [code]import type { NextConfig } from "next"; import { locales, defaultLocale } from "@/lib/i18n";
// DEBUG: Log what we're working with console.log("🔍 Middleware Debug:"); console.log(" pathname:", pathname); console.log(" locales:", locales); console.log(" defaultLocale:", defaultLocale);
// Skip API root, routes, _next, and public files // also Skips 404s, so they can be handled with if ( pathname === "/" || // allows root to pass through locales.some(loc => pathname.startsWith(`/${loc}/`)) || PUBLIC_FILE.test(pathname) || pathname.startsWith("/api") || pathname.startsWith("/_next") ||
// filters into array, gets the first word in path const segments = pathname.split("/").filter(Boolean); const [first, ...rest] = segments; console.log("firstFIRSTFIRSTFIRST", first); console.log("URL URL URL URL:", url.pathname);
// handles perfectly inputted urls if (first && locales.includes(first as (typeof locales)[number])) { console.log("2"); return NextResponse.next(); }
// creates the correct locale for the user const rawLang = req.headers.get("accept-language")?.split(",")[0].split("-")[0] || defaultLocale; const finalLocale = locales.includes(rawLang as (typeof locales)[number]) ? rawLang : defaultLocale; console.log("FINAL LOCALE FINAL LOCALE:", finalLocale);
// handles directory without locale if (segments.length === 1) { const redirectUrl = new URL(`/ja/catalogue`, req.url); url.pathname = `/${finalLocale}/${segments[0]}`; console.log("3"); console.log("3 pathname sending...:", redirectUrl.pathname);
return NextResponse.redirect(redirectUrl); }
// handles bogus locales but solid directiory if (first && rest.length > 0 && !locales.includes(first as (typeof locales)[number])) { url.pathname = `/${finalLocale}/${rest.join("/")}`; console.log("4"); console.log(url); console.log(url.pathname); return NextResponse.redirect(url); } console.log("5");
return NextResponse.next(); }
// checks to even bother running the middleware if these are in the url export const config = { matcher: ["/((?!api|_next|.*\\..*).*)"], }; [/code] Я попытался добавить Skipmiddlewareurlnormalize: true, в my next.config.ts. Таким образом, бесконечный цикл промежуточного программного обеспечения продолжается. < /p> Вот журналы: < /p> [code]Middleware Debug: pathname: /wrx/catalogue locales: [ 'en', 'ja' ] defaultLocale: en firstFIRSTFIRSTFIRST wrx URL URL URL URL: /wrx/catalogue FINAL LOCALE FINAL LOCALE: en 4 { href: 'http://localhost:3000/en/catalogue', origin: 'http://localhost:3000', protocol: 'http:', username: '', password: '', host: 'localhost:3000', hostname: 'localhost', port: '3000', pathname: '/en/catalogue', search: '', searchParams: URLSearchParams { }, hash: '' } /en/catalogue
🔍 Middleware Debug: pathname: /catalogue locales: [ 'en', 'ja' ] defaultLocale: en firstFIRSTFIRSTFIRST catalogue URL URL URL URL: /catalogue FINAL LOCALE FINAL LOCALE: en 3 3 pathname sending...: /ja/catalogue
🔍 Middleware Debug: pathname: /catalogue locales: [ 'en', 'ja' ] defaultLocale: en firstFIRSTFIRSTFIRST catalogue URL URL URL URL: /catalogue FINAL LOCALE FINAL LOCALE: en 3 3 pathname sending...: /ja/catalogue
🔍 Middleware Debug: pathname: /catalogue locales: [ 'en', 'ja' ] defaultLocale: en firstFIRSTFIRSTFIRST catalogue URL URL URL URL: /catalogue FINAL LOCALE FINAL LOCALE: en 3 3 pathname sending...: /ja/catalogue [/code] он просто продолжает идти, потому что он зацикливается в блоке "3".