Skip to content

Commit

Permalink
Shopify cookie fix again (#2143)
Browse files Browse the repository at this point in the history
  • Loading branch information
wizardlyhel committed May 22, 2024
1 parent 73716c8 commit 9bce950
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions packages/hydrogen-react/src/useShopifyCookies.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,24 @@ export function useShopifyCookies(options?: UseShopifyCookiesOptions): void {
useEffect(() => {
const cookies = getShopifyCookies(document.cookie);

/**
* Setting cookie with domain
*
* If no domain is provided, the cookie will be set for the current host.
* For Shopify, we need to ensure this domain is set with a leading dot.
*/

// Use override domain or current host
let currentDomain = domain || window.document.location.host;

// Reset domain if localhost
if (/^localhost/.test(currentDomain)) currentDomain = '';

// Shopify checkout only consumes cookies set with leading dot domain
const domainWithLeadingDot = domain
? /^\./.test(domain)
? domain
: `.${domain}`
const domainWithLeadingDot = currentDomain
? /^\./.test(currentDomain)
? currentDomain
: `.${currentDomain}`
: '';

/**
Expand Down

0 comments on commit 9bce950

Please sign in to comment.