Hi,
I have a problem with the rewrite() method of the NextResponse function inside middleware.
It works perfectly, but on the index page, it actually redirects to /en for some reason. The website is - https://preview.edgeless.systems
Here is the middleware.ts content:
import { i18n } from "@/i18n.config";
import { match as matchLocale } from "@formatjs/intl-localematcher";
import Negotiator from "negotiator";
import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
function getLocale(request: NextRequest): string | undefined {
const negotiatorHeaders: Record<string, string> = {};
request.headers.forEach((value, key) => (negotiatorHeaders[key] = value));
const locales = i18n.locales;
const languages = new Negotiator({ headers: negotiatorHeaders }).languages();
const locale = matchLocale(languages, locales, i18n.defaultLocale);
return locale;
}
export function middleware(request: NextRequest) {
const pathname = request.nextUrl.pathname;
const pathnameIsMissingLocale = i18n.locales.every(
(locale) =>
!pathname.startsWith(`/${locale}/`) && pathname !== `/${locale}`,
);
console.log("===================================");
console.log("Pathname:", pathname);
// Redirect if there is no locale
if (pathnameIsMissingLocale) {
const locale = getLocale(request);
if (locale === i18n.defaultLocale) {
const url = new URL(
`/${locale}${pathname.startsWith("/") ? "" : "/"}${pathname}`,
request.url,
);
console.log("URL: " + url.toString());
return NextResponse.rewrite(url);
}
}
}
export const config = {
matcher: [`/((?!_next|svg|meta|api|slice-simulator|sitemap.xml|icon.svg).*)`],
};
Edge Functions Logs:
Apr 21, 02:28:50 PM: 01HW0A2V info [___netlify-edge-handler-src-middleware] ===================================
Apr 21, 02:28:50 PM: 01HW0A2V info [___netlify-edge-handler-src-middleware] Pathname: /
Apr 21, 02:28:50 PM: 01HW0A2V info [___netlify-edge-handler-src-middleware] Locale: en
Apr 21, 02:28:50 PM: 01HW0A2V info [___netlify-edge-handler-src-middleware] Languages: [ "uk", "en" ]
Apr 21, 02:28:50 PM: 01HW0A2V info [___netlify-edge-handler-src-middleware] Locales: [ "en", "de" ]
Apr 21, 02:28:50 PM: 01HW0A2V info [___netlify-edge-handler-src-middleware] Default Locale: en
Apr 21, 02:28:50 PM: 01HW0A2V info [___netlify-edge-handler-src-middleware] Is defaultLocale
Apr 21, 02:28:50 PM: 01HW0A2V info [___netlify-edge-handler-src-middleware] URL: https://preview.edgeless.systems/en/
Apr 21, 02:28:50 PM: 01HW0A2V info [___netlify-edge-handler-src-middleware] ===================================
Apr 21, 02:28:50 PM: 01HW0A2V info [___netlify-edge-handler-src-middleware] Pathname: /en <- REDIRECTED
Apr 21, 02:28:51 PM: 01HW0A2W info [___netlify-edge-handler-src-middleware] ===================================
Please note, it happens with the index page only. There are no redirects anywhere in the project.
No problem locally and on Vercel. It happens only on Netlify.
Stack used:
Next.js 14.1
Next.js Runtime - v5.1.2
Content of autogenerated netlify.toml:
[[redirects]]
from = "/_next/image"
to = "/.netlify/images?url=:url&w=:width&q=:quality"
status = 200
[redirects.query]
url = ":url"
w = ":width"
q = ":quality"
[[redirects]]
from = "/_ipx/*"
to = "/.netlify/images?url=:url&w=:width&q=:quality"
status = 200
[redirects.query]
url = ":url"
w = ":width"
q = ":quality"
[context]
[context.production]
[context.main]