fix: sync IS locale with URL and selectedFacets on navigation#3405
Conversation
WalkthroughLocale selection now prefers stored VTEX session data in search requests and uses the current router locale for product facet variables when localization is enabled. ChangesLocale resolution updates
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
|
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. |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/core/src/sdk/product/useLocalizedVariables.ts (1)
34-45: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueOptional: flatten the memo with an early return.
Logic is correct, but a guard-style early return reads cleaner than the nested
if/try.♻️ Flatter variant
const locale = useMemo(() => { - if (storeConfig.localization?.enabled && typeof window !== 'undefined') { - try { - return getSettings({ - url: new URL(router.asPath, window.location.origin), - }).locale - } catch { - return sessionLocale - } - } - return sessionLocale + if (!storeConfig.localization?.enabled || typeof window === 'undefined') { + return sessionLocale + } + try { + return getSettings({ + url: new URL(router.asPath, window.location.origin), + }).locale + } catch { + return sessionLocale + } }, [router.asPath, sessionLocale])As per path instructions: "Prefer flat conditionals over nested ifs."
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/core/src/sdk/product/useLocalizedVariables.ts` around lines 34 - 45, The locale computation in useLocalizedVariables is correct but uses a nested if/try structure inside the useMemo callback. Refactor the useMemo logic to use guard-style early returns instead of nesting, keeping the same fallback behavior to sessionLocale when localization is disabled or when getSettings throws; use the existing identifiers useMemo, getSettings, router.asPath, and sessionLocale to preserve behavior while flattening the control flow.Source: Path instructions
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@packages/core/src/sdk/product/useLocalizedVariables.ts`:
- Around line 34-45: The locale computation in useLocalizedVariables is correct
but uses a nested if/try structure inside the useMemo callback. Refactor the
useMemo logic to use guard-style early returns instead of nesting, keeping the
same fallback behavior to sessionLocale when localization is disabled or when
getSettings throws; use the existing identifiers useMemo, getSettings,
router.asPath, and sessionLocale to preserve behavior while flattening the
control flow.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 46c92912-22a9-4648-8643-08b988ae98ef
📒 Files selected for processing (3)
packages/api/src/platforms/vtex/clients/search/index.tspackages/api/test/unit/platforms/vtex/clients/search.test.tspackages/core/src/sdk/product/useLocalizedVariables.ts
When localization is enabled, client-side IS queries could use a stale locale after hard navigation because session.locale and vtex_segment lag behind the URL. Derive the locale facet from router.locale and prefer ctx.storage.locale over the segment cookie when calling Intelligent Search. Co-authored-by: Cursor <cursoragent@cursor.com>
7d13777 to
f3163a7
Compare
@faststore/api
@faststore/cli
@faststore/components
@faststore/core
@faststore/diagnostics
@faststore/lighthouse
@faststore/sdk
@faststore/ui
commit: |
|

0 New Issues
0 Fixed Issues
0 Accepted Issues
Summary
@faststore/core: whenlocalization.enabled, derive the locale facet for client-side IS queries fromrouter.locale(viagetSettings) instead ofsession.locale, which can lag after a hard locale switch or back/forward navigation.@faststore/api: preferctx.storage.locale(set from trustedselectedFacets) overvtex_segmentcookiecultureInfowhen building Intelligent Search requests — the cookie can remain stale while the URL and facets already reflect the new locale.These two changes work together: core sends the correct locale in the GraphQL request; API ensures IS does not override it with a stale segment cookie.
Context
Introduced while working on localized PLP pages (#3401). The bug also affects stores with localization enabled on
devtoday (locale-prefixed PLPs, search, shelves) even without localized collection slug resolution.Does not include PLP-specific work (
by-linkid,StoreCollection.otherLocales, etc.) — that remains in #3401.Test plan
cd packages/api && pnpm vitest run test/unit/platforms/vtex/clients/search.test.tslocalization.enabled: true, open a locale-prefixed PLP (e.g./pt-BR/apparel)localization.enabled: false, verify no behavior change on PLP/search pagesPR: https://vtex-dev.atlassian.net/jira/software/c/projects/SFS/boards/1051?selectedIssue=SFS-3242
Made with Cursor
Summary by CodeRabbit
Summary by CodeRabbit
New Features
Bug Fixes
Tests