Skip to content

Proxy not work in production mode #85711

@rindev0901

Description

@rindev0901

Link to the code that reproduces this issue

https://github.com/rindev0901/learn-next-16

To Reproduce

  • Install dependencies (npm install or whatever).
  • Build and start project (npm run start or your equivalent).
  • Open the app in your browser
  • Check ternimal no any log in proxy

Current vs. Expected behavior

Current behavior

When i try access to / path in dev mode proxy work correct follow my below logic:

import { NextRequest, NextResponse } from "next/server";

export const locales = ["en", "nl"] as const;
export type Lang = (typeof locales)[number];

// Get the preferred locale, similar to the above or using a library
function getLocale(request: NextRequest) {
	console.log("Call proxy");
	const acceptLanguage = request.headers.get("accept-language");
	if (!acceptLanguage) return locales[0];

	const languages = acceptLanguage.split(",").map((lang) => lang.trim());
	const matchedLocale = languages.find((lang) =>
		locales.includes(lang as never)
	);
	return matchedLocale || locales[0];
}

export function proxy(request: NextRequest) {
	// Check if there is any supported locale in the pathname
	const { pathname } = request.nextUrl;
	const pathnameHasLocale = locales.some(
		(locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`
	);

	if (pathnameHasLocale) return;

	// Redirect if there is no locale
	const locale = getLocale(request);
	request.nextUrl.pathname = `/${locale}${pathname}`;
	// e.g. incoming request is /products
	// The new URL is now /en-US/products
	return NextResponse.redirect(request.nextUrl);
}

export const config = {
	matcher: [
		// Skip all internal paths (_next) and api routes
		"/((?!_next|api).*)",
		// Optional: only run on root (/) URL
		// '/'
	],
};

But after build and run in production mode the proxy is not running and I don't see any log lines displayed

Expected behavior

I want when the user accesses a path that does not contain locale, he will be redirected to the default locale.

Provide environment information

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 10 Pro
  Available memory (MB): 32596
  Available CPU cores: 12
Binaries:
  Node: 22.20.0
  npm: 10.9.2
  Yarn: 1.22.22
  pnpm: 10.13.1
Relevant Packages:
  next: 16.0.0 // There is a newer version (16.0.1) available, upgrade 
recommended!
  eslint-config-next: N/A
  react: 19.2.0
  react-dom: 19.2.0
  typescript: 5.9.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Middleware

Which stage(s) are affected? (Select all that apply)

next dev (local), next start (local)

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    MiddlewareRelated to Next.js Middleware.

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions