Skip to content

fix: session update for custom path - #3204

Merged
lariciamota merged 2 commits into
feat/multilanguagefrom
fix/cookies-binding-mismatch
Feb 13, 2026
Merged

lariciamota merged 2 commits into
feat/multilanguagefrom
fix/cookies-binding-mismatch

Conversation

@lariciamota

@lariciamota lariciamota commented Feb 9, 2026 •

Copy link
Copy Markdown
Contributor

What's the purpose of this pull request?

Session and segment cookies (vtex-session and vtex-segment) were not being updated correctly for custom path bindings like /europe/it. The sales channel and locale remained at default values instead of updating to the correct binding configuration.

How it works?

The matchURLBinding function in packages/core/src/sdk/localization/match-url.ts only matched URLs using a locale code regex pattern /\/(\w{2}-\w{2})\/?/i. This worked for locale paths like /pt-BR but failed for custom paths like /europe/it.

Updated matchURLBinding to:

  1. Compare full pathnames directly instead of only extracting locale code patterns
  2. Support path prefix matching for URLs with additional segments (e.g., /europe/it/products matches binding /europe/it)
  3. Handle trailing slashes properly
  4. Support all binding types: root domains, locale paths, custom paths, and subdomains
  5. Refactored to use shared matchesBindingPath utility (cleaner, more maintainable)
  6. Properly implement development mode for local testing (allows port/protocol/hostname mismatch)

How to test it?

Test different binding types, mainly custom path, which is the one that had the bug.
After configuring /etc/hosts to point those bindings to localhost, go to the URL configured and check if the session/segment cookies are correct.

Binding Type Example
Root domain https://brandless.myvtex.com
Locale path /pt-BR, /it-IT, /en-US
Custom path /europe/it, /america/fr, /emea-eur
With segments /europe/it/products, /pt-BR/office
Subdomain https://pt.brandless.fast.store

Starters Deploy Preview

References

Summary by CodeRabbit

  • Bug Fixes

    • Improved URL binding so localization detection is more accurate across hostnames, environments (localhost vs production), and protocols; path comparisons now handle exact and prefix matches robustly.
  • Tests

    • Expanded test coverage for domain, locale-path, and custom-path binding scenarios, including trailing slashes, extra segments, and query strings.

@coderabbitai

coderabbitai Bot commented Feb 9, 2026 •

Copy link
Copy Markdown
Contributor

Walkthrough

The PR refactors URL binding: introduces hostnameMatch (with localhost permissiveness in development), enforces protocol only in production, replaces origin-based path checks with a new matchesBindingPath utility, and updates tests to cover many matching scenarios.

Changes

Cohort / File(s) Summary
URL Matching Logic
packages/core/src/sdk/localization/match-url.ts
Rewrote binding checks to require hostname match (dev allows localhost/127.0.0.1), apply protocol check only in production, and delegate pathname comparison to matchesBindingPath; removed origin/langPath computation and added early rejection on hostname mismatch.
Path Matching Utility
packages/core/src/utils/localization/customPaths.ts
Added exported matchesBindingPath(incomingPath: string, bindingPath: string): boolean which normalizes paths and supports exact/root and prefix matching; refactored extractCustomPathPrefix to use this helper.
Tests
packages/core/test/utils/localization/customPaths.test.ts, packages/core/test/utils/match-url.test.ts
Expanded unit tests: comprehensive matchesBindingPath cases (root, trailing slashes, prefix, non-matches) and many matchURLBinding scenarios (root domains, locale paths, custom paths, path normalization, query strings, multiple locales/currencies).

Sequence Diagram(s)

sequenceDiagram
  rect rgba(63,81,181,0.5)
    participant Client
  end
  rect rgba(3,169,244,0.5)
    participant Matcher as matchURLBinding
  end
  rect rgba(76,175,80,0.5)
    participant Config as BindingConfig
  end
  rect rgba(255,193,7,0.5)
    participant PathUtil as matchesBindingPath
  end

  Client->>Matcher: Send request URL
  Matcher->>Config: Retrieve candidate binding(s)
  Matcher->>Config: Compare hostname -> if mismatch reject
  alt hostname matches
    Matcher->>PathUtil: Compare pathname (incoming vs binding)
    PathUtil-->>Matcher: boolean (match / no match)
    alt path matches
      Matcher->>Config: Check protocol (production only)
      alt protocol matches or dev
        Matcher-->>Client: Binding accepted
      else protocol mismatch (prod)
        Matcher-->>Client: Binding rejected
      end
    else path mismatch
      Matcher-->>Client: Binding rejected
    end
  end
Loading

Estimated Code Review Effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly Related PRs

Suggested Reviewers

  • lemagnetic
  • renatomaurovtex
  • hellofanny

Poem

🌍 Hostnames align, paths fall in place,
Dev lets localhost run its race,
Production tightens protocol's hold,
New path matcher checks each road,
Tests sing confirmation, bright and bold. ✨

🚥 Pre-merge checks | ✅ 1 | ❌ 2
❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Title check ⚠️ Warning The PR title 'fix: session update for custom path' addresses a specific aspect of the changes but omits the core issue (cookies binding mismatch). While it references custom paths, the main objective centers on fixing incorrect cookie updates for custom path bindings, making the title incomplete and somewhat vague about the actual problem being solved. Update title to 'fix: cookies binding mismatch for custom paths' or similar to better reflect the root cause and primary objective of the changes.
Docstring Coverage ⚠️ Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (1 passed)
Check name Status Explanation
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
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/cookies-binding-mismatch

No actionable comments were generated in the recent review. 🎉

🧹 Recent nitpick comments
packages/core/src/sdk/localization/match-url.ts (1)

6-48: Hoist new URL(href) outside the loop to avoid redundant parsing.

hostURLObject is re-created for every binding of every locale. Since href never changes, parse it once before the iteration.

♻️ Proposed refactor
 export function matchURLBinding(href: string) {
   let binding
+  const hostURLObject = new URL(href)
   const matchedConfig = Object.entries(config.localization.locales).find(
     ([_, langConfig]: [string, any]) => {
-      const hostURLObject = new URL(href)
-
       binding = langConfig.bindings.find(({ url }: { url: string }) => {
         const configURLObject = new URL(url)
packages/core/test/utils/match-url.test.ts (2)

192-387: Good coverage of root, locale, custom path, and normalization scenarios.

The test structure is clean and the mock data exercises the key matching scenarios well, including prefix matching with additional segments and trailing slashes.

Consider adding a negative test — a URL that matches no binding — to verify the function returns undefined config/binding gracefully. Minor gap, not blocking.

💡 Example negative test
+  describe('Non-matching URLs', () => {
+    it('should return undefined config and binding for unknown path', () => {
+      const result = matchURLBinding('https://unknown.example.com/random')
+      expect(result.config).toBeUndefined()
+      expect(result.binding).toBeUndefined()
+    })
+  })

252-273: Potentially confusing: en-CA locale binding on /pt-BR path.

The expected match maps https://brandless.fast.store/pt-BR/products → en-CA config (not pt-BR). This is correct per the mock data (line 76-80: en-CA has a /pt-BR binding), but the variable name enCa for a /pt-BR URL match could trip up future readers. A brief inline comment explaining why the en-CA config is expected here would help.

packages/core/test/utils/localization/customPaths.test.ts (1)

316-316: Nit: matchesBindingPath suite is outside the customPaths describe block.

The matchesBindingPath describe (line 316) sits at the top level while the other custom path tests are nested under describe('customPaths', ...). Consider nesting it inside for organizational consistency.

Tip

Issue Planner is now in beta. Read the docs and try it out! 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 Feb 9, 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 self-assigned this Feb 10, 2026
@lariciamota lariciamota added the bug Something isn't working label Feb 10, 2026
@lariciamota
lariciamota marked this pull request as ready for review February 10, 2026 12:49
@lariciamota
lariciamota requested a review from a team as a code owner February 10, 2026 12:49
@lariciamota
lariciamota requested review from emersonlaurentino and renatomaurovtex and removed request for a team February 10, 2026 12:49
@lariciamota lariciamota changed the title fix: cookies binding mismatch fix: session update for custom path Feb 10, 2026
@lariciamota
lariciamota requested review from hellofanny and lemagnetic and removed request for emersonlaurentino and renatomaurovtex February 10, 2026 17:03

@lemagnetic lemagnetic 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.

Tested on localhost, cookies are updating as expected. LGTM

@lariciamota
lariciamota force-pushed the fix/cookies-binding-mismatch branch from 0977663 to 74db7d6 Compare February 11, 2026 13:54
}

// Sending the locale to the session, the store-session app will update cultureInfo
params.set('locale', oldSession.locale)

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.

nice!

@hellofanny hellofanny 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.

Niceee!
Re-tested after the fix (sending the locale to the session). It's working now ~ session locales and cookies match up as expected 🚀

subdomain-match-cookie.mov

@lariciamota
lariciamota merged commit f95820a into feat/multilanguage Feb 13, 2026
2 checks passed
@lariciamota
lariciamota deleted the fix/cookies-binding-mismatch branch February 13, 2026 12:04
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.

3 participants