Skip to content

fix: localization selector redirect path - #3237

Merged
lariciamota merged 2 commits into
canary-v2from
fix/locale-selector-redirect-path
Mar 9, 2026
Merged

lariciamota merged 2 commits into
canary-v2from
fix/locale-selector-redirect-path

Conversation

@lariciamota

@lariciamota lariciamota commented Mar 5, 2026 •

Copy link
Copy Markdown
Contributor

What's the purpose of this pull request?

When users switch locales via the Localization Selector, they are always redirected to the home page of the new locale instead of staying on the current page. For example, switching from pt-BR to en-US while on /pt-BR/products/item?color=red redirects to /en-US instead of /en-US/products/item?color=red.

How it works?

The save callback in useBindingSelector previously redirected directly to binding.url (the binding's base URL), discarding the current page path and query string.
The new function buildRedirectUrl combines the new binding URL with the extracted page path, preserving query strings and hash fragments. The save callback now calls buildRedirectUrl with the binding URL and the full current URL, so users stay on the same page after switching locales.

File rename: customPaths.ts was renamed to bindingPaths.ts since the file already contained general-purpose binding path utilities (matchesBindingPath) beyond just custom paths, and the new functions further broaden its scope. All 4 import sites and the test file were updated accordingly.

How to test it?

  • Navigate to a page different from the home
  • Open the Localization Selector and switch to a different locale
  • Verify the redirect goes to the current path with the new locale instead of just the new locale
  • Repeat with a custom-path binding
  • Verify query strings are preserved
Screen.Recording.2026-03-05.at.15.37.30.mov

References

Summary by CodeRabbit

  • Bug Fixes

    • URL redirects between locales now preserve query parameters and hash fragments, maintaining user context when switching language versions.
  • Improvements

    • Enhanced multi-locale URL path resolution for more reliable navigation across localized content.

@lariciamota lariciamota self-assigned this Mar 5, 2026
@lariciamota lariciamota added the bug Something isn't working label Mar 5, 2026
@coderabbitai

coderabbitai Bot commented Mar 5, 2026 •

Copy link
Copy Markdown
Contributor

Walkthrough

This PR refactors localization path utilities by consolidating functions into a new bindingPaths module, replaces import paths across multiple files, and introduces three new utilities (collectAllBindingPaths, getPagePath, buildRedirectUrl) to improve redirect behavior by preserving the current page path and query context.

Changes

Cohort / File(s) Summary
Import path consolidation
packages/core/src/sdk/localization/match-url.ts, packages/core/src/sdk/ui/useLink.ts, packages/core/src/proxy.ts
Updated imports to reference the new bindingPaths module instead of customPaths; no logic changes.
Redirect behavior enhancement
packages/core/src/sdk/localization/useBindingSelector.ts
Added buildRedirectUrl import and refactored redirect logic to preserve current page path, query string, and hash when navigating via binding URLs.
New localization utilities
packages/core/src/utils/localization/bindingPaths.ts
Added three exported functions: collectAllBindingPaths() to gather unique binding paths sorted by length; getPagePath() to extract the page path after stripping binding prefix; buildRedirectUrl() to construct full redirect URLs while preserving query/hash context.
Comprehensive test coverage
packages/core/test/utils/localization/bindingPaths.test.ts
Added test suites for new utilities covering deduplication, prefix matching, exact/partial matches, trailing slashes, query/hash preservation, and edge cases (root paths, unknown paths).

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Possibly related PRs

  • fix: session update for custom path #3204: Both PRs modify path-matching logic for URL bindings and update match-url.ts to use matchesBindingPath; this PR extends that helper in the new bindingPaths module alongside new redirect utilities.
  • feat: localization middleware #3185: This PR's refactor of localization helpers (moving from customPaths to bindingPaths) directly extends the utilities introduced in that PR and updates all call sites accordingly.
  • fix: Adjust links and search path with locale #3214: Both PRs modify useLink and localization path helper imports, specifically addCustomPathPrefix and extractCustomPathPrefix usage across the codebase.

Suggested reviewers

  • lemagnetic
  • hellofanny
  • emersonlaurentino

Poem

🧭 Paths reorganized, bindings now bound,
Redirects preserve what was once lost and found,
With queries and hashes kept safe in the fold,
New utilities shine—a refactor of gold! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title 'fix: localization selector redirect path' directly addresses the main change: fixing the redirect behavior in the Localization Selector to preserve the current page path instead of redirecting to the binding's home page.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/locale-selector-redirect-path

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codesandbox-ci

codesandbox-ci Bot commented Mar 5, 2026 •

Copy link
Copy Markdown

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@lariciamota
lariciamota marked this pull request as ready for review March 5, 2026 18:29
@lariciamota
lariciamota requested a review from a team as a code owner March 5, 2026 18:29
@lariciamota
lariciamota requested review from hellofanny and renatomaurovtex and removed request for a team March 5, 2026 18:29

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/core/src/sdk/localization/useBindingSelector.ts`:
- Around line 163-167: The redirect currently drops the URL hash causing anchor
state loss; update the redirect construction in useBindingSelector (the block
calling buildRedirectUrl with binding.url and
`${window.location.pathname}${window.location.search}`) to also append
window.location.hash so the full fragment is preserved when calling
buildRedirectUrl; ensure the second argument becomes the full path+search+hash
string before assigning window.location.href.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: d057fcb7-e1b5-4d77-a2bc-798796a2d0f0

📥 Commits

Reviewing files that changed from the base of the PR and between b64597f and 341d19a.

📒 Files selected for processing (6)
  • packages/core/src/middleware.ts
  • packages/core/src/sdk/localization/match-url.ts
  • packages/core/src/sdk/localization/useBindingSelector.ts
  • packages/core/src/sdk/ui/useLink.ts
  • packages/core/src/utils/localization/bindingPaths.ts
  • packages/core/test/utils/localization/bindingPaths.test.ts

@lariciamota
lariciamota force-pushed the fix/locale-selector-redirect-path branch from 77bf40e to 87c4ef2 Compare March 6, 2026 17:43

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/core/src/utils/localization/bindingPaths.ts`:
- Around line 259-269: buildRedirectUrl treats currentUrl as a raw pathname but
callers pass a full URL; update buildRedirectUrl to first parse currentUrl into
a URL (using new URL(currentUrl, windowOrigin fallback) or try/catch to support
relative URLs) and derive a pathWithQueryHash = url.pathname + url.search +
url.hash, then call getPathOnly(pathWithQueryHash) and getPagePath(pathOnly) so
the binding prefix is removed correctly before composing the final redirect with
bindingUrl, pagePath and suffix; reference functions buildRedirectUrl,
getPathOnly, getPagePath and variables currentUrl, bindingUrl, suffix in your
change.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 5b1cba42-cc5d-4b0f-ba70-e7c8480403ac

📥 Commits

Reviewing files that changed from the base of the PR and between 77bf40e and 87c4ef2.

📒 Files selected for processing (6)
  • packages/core/src/proxy.ts
  • packages/core/src/sdk/localization/match-url.ts
  • packages/core/src/sdk/localization/useBindingSelector.ts
  • packages/core/src/sdk/ui/useLink.ts
  • packages/core/src/utils/localization/bindingPaths.ts
  • packages/core/test/utils/localization/bindingPaths.test.ts
🚧 Files skipped from review as they are similar to previous changes (3)
  • packages/core/src/sdk/localization/useBindingSelector.ts
  • packages/core/src/sdk/localization/match-url.ts
  • packages/core/src/sdk/ui/useLink.ts

@lariciamota
lariciamota merged commit c3cbe92 into canary-v2 Mar 9, 2026
4 of 5 checks passed
@lariciamota
lariciamota deleted the fix/locale-selector-redirect-path branch March 9, 2026 12:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants