Skip to content

fix(web): persist locale cookie on canonical docs pages#15938

Open
zerone0x wants to merge 1 commit intoanomalyco:devfrom
zerone0x:fix/docs-locale-switcher-cookie
Open

fix(web): persist locale cookie on canonical docs pages#15938
zerone0x wants to merge 1 commit intoanomalyco:devfrom
zerone0x:fix/docs-locale-switcher-cookie

Conversation

@zerone0x
Copy link
Contributor

@zerone0x zerone0x commented Mar 4, 2026

Issue for this PR

Closes #15845

Type of change

  • Bug fix

What does this PR do?

The language switcher on opencode.ai/docs doesn't stick when switching to English (or any locale via the Starlight switcher).

Root cause: the oc_locale cookie was only written in one place — inside docsAlias(), which triggers on alias URLs like /docs/en/page and redirects to the canonical path. The Starlight language switcher generates canonical URLs directly (e.g. /docs/page for English, /docs/tr/page for Turkish), so the cookie was never written when switching language this way. On the next visit to /docs/, the middleware re-read Accept-Language from the browser and redirected back to the original locale.

The fix detects the locale from the URL path on every docs page request and appends the oc_locale cookie to the response when the current cookie value differs. This makes the language switcher choice sticky without requiring any Starlight or build-time changes.

How did you verify your code works?

Traced the middleware logic manually:

  • /docs/tr/page → sets oc_locale=tr if cookie differs
  • /docs/page → infers root/English (no locale prefix → exactLocale returns null → "root") → sets oc_locale=en
  • Subsequent /docs/ → reads cookie → no redirect ✓
  • Already-matching cookie → no Set-Cookie header (no overhead per request) ✓

Screenshots / recordings

N/A — middleware logic change, no visual output.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

The oc_locale cookie was only written when the middleware triggered a
redirect through docsAlias() — i.e., when a user visited an alias URL
like /docs/en/page.  Canonical locale paths (e.g. /docs/tr/page) and
the English root path (e.g. /docs/page) never wrote the cookie.

This meant that switching language via the Starlight language switcher
had no lasting effect: clicking English navigated to /docs/page, but
no cookie was set.  The next visit to /docs/ re-read Accept-Language
from the browser and redirected back to the non-English locale.

Fix: detect the locale from the URL path on every docs page request
and write the oc_locale cookie whenever the current cookie value
differs.  This makes the language switcher choice sticky across
navigations without requiring an alias redirect.

Closes anomalyco#15845

Co-Authored-By: Claude <noreply@anthropic.com>
@github-actions
Copy link
Contributor

github-actions bot commented Mar 4, 2026

The following comment was made by an LLM, it may be inaccurate:

Potential duplicate found:

Why it might be related:
PR #13756 appears to address a similar issue with persisting locale on docs routes. Both PRs deal with fixing language/locale persistence on documentation pages. You should check if #13756 was already merged or if it addressed the same underlying problem differently.

IgorTavcar added a commit to IgorTavcar/opencode that referenced this pull request Mar 4, 2026
)

Cherry-picked from upstream PR anomalyco#15938.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Documentation] Language Switcher Does Not Override Browser Locale

1 participant