Skip to content

fix: Skip accept-language parsing when locale cookie is already present#2143

Merged
amannn merged 1 commit intoamannn:mainfrom
lxup:main
Dec 1, 2025
Merged

fix: Skip accept-language parsing when locale cookie is already present#2143
amannn merged 1 commit intoamannn:mainfrom
lxup:main

Conversation

@lxup
Copy link
Contributor

@lxup lxup commented Dec 1, 2025

Fixes #2116

Description

This PR addresses a severe performance bottleneck in the syncCookie middleware logic where getAcceptLanguageLocale (and consequently locale negotiation) was called unconditionally on every request.

As detailed in the linked issue, this caused linear performance degradation as the number of locales increased (~130ms overhead for ~140 locales).

Changes

I optimized the syncCookie function to verify the cookie state before attempting to derive the locale from headers.

  • Before: Header parsing & locale negotiation ran on every request.
  • After: Header parsing only runs if:
  1. No locale cookie exists.
  2. OR the cookie is outdated/invalid.

Benchmarks

Tested with ~140 locales in the reproduction repository provided in the issue.

Before:

[next-intl-timing] syncCookie: 130ms
[next-intl-timing] total: 132ms

After:

[next-intl-timing] syncCookie: 1ms
[next-intl-timing] total: 3ms

Checklist

  • I have verified that the issue is resolved locally.
  • I have verified that existing tests pass.

…arsing

Previously, getAcceptLanguageLocale was called unconditionally, causing linear performance degradation with a large number of locales. This change lazy-evaluates the header parsing only when needed.
@vercel
Copy link

vercel bot commented Dec 1, 2025

@lxup is attempting to deploy a commit to the next-intl Team on Vercel.

A member of the Team first needs to authorize it.

@vercel
Copy link

vercel bot commented Dec 1, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
next-intl-docs Ready Ready Preview Comment Dec 1, 2025 2:50pm
next-intl-example-app-router Ready Ready Preview Comment Dec 1, 2025 2:50pm
next-intl-example-app-router-without-i18n-routing Ready Ready Preview Comment Dec 1, 2025 2:50pm

@amannn amannn changed the title fix: optimize syncCookie performance by avoiding unnecessary header parsing fix: Short-cut accept-language parsing when locale cookie is already present Dec 1, 2025
@amannn amannn changed the title fix: Short-cut accept-language parsing when locale cookie is already present fix: Skip accept-language parsing when locale cookie is already present Dec 1, 2025
Copy link
Owner

@amannn amannn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Careful analysis + simple fix + a lot of impact = dream PR — thanks a bunch @lxup ❤️

@amannn amannn merged commit 0d1331b into amannn:main Dec 1, 2025
12 of 13 checks passed
@lxup
Copy link
Contributor Author

lxup commented Dec 2, 2025

@amannn thx! That was my first PR ever 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

next-intl middleware extremely slow with large number of locales (~140+)

2 participants