From 7072e0f909dd60656122220905598ee3cc85d9ef Mon Sep 17 00:00:00 2001 From: Pablo Pettinari Date: Tue, 30 Sep 2025 10:34:21 +0200 Subject: [PATCH] feat(middleware): convert default-locale 307 redirects to 301 via composed next-intl middleware for SEO --- middleware.ts | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/middleware.ts b/middleware.ts index ccc22f188fd..97d6926076d 100644 --- a/middleware.ts +++ b/middleware.ts @@ -1,8 +1,28 @@ +import { NextRequest, NextResponse } from "next/server" import createMiddleware from "next-intl/middleware" import { routing } from "./src/i18n/routing" +import { DEFAULT_LOCALE } from "./src/lib/constants" -export default createMiddleware(routing) +const handleI18nRouting = createMiddleware(routing) + +export default function middleware(request: NextRequest) { + const response = handleI18nRouting(request) + + // Upgrade default-locale strip redirects from 307 to 301 for SEO + if (response.status === 307) { + const pathname = request.nextUrl.pathname + const defaultPrefix = `/${DEFAULT_LOCALE}` + if ( + pathname === defaultPrefix || + pathname.startsWith(`${defaultPrefix}/`) + ) { + return new NextResponse(null, { status: 301, headers: response.headers }) + } + } + + return response +} // Simplified matcher pattern export const config = {