Skip to content

Commit

Permalink
Fix shopify cookie with leading dot domain (#2142)
Browse files Browse the repository at this point in the history
  • Loading branch information
wizardlyhel committed May 22, 2024
1 parent 32d4c33 commit 73716c8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/gorgeous-ghosts-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/hydrogen-react': patch
---

Fix shopify cookie domain setting
32 changes: 31 additions & 1 deletion packages/hydrogen-react/src/useShopifyCookies.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ describe(`useShopifyCookies`, () => {
expect(cookieJar['_shopify_y'].maxage).toBe(31104000);
});

it('sets domain when provided', () => {
it('sets domain with leading period when provided without a leading period', () => {
const cookieJar: MockCookieJar = mockCookie();
const domain = 'myshop.com';

Expand All @@ -170,6 +170,36 @@ describe(`useShopifyCookies`, () => {
expect(cookies['_shopify_s']).not.toBe('');
expect(cookies['_shopify_y']).not.toBe('');

expect(cookieJar['_shopify_s'].value).not.toBe(
cookieJar['_shopify_y'].value,
);
expect(cookieJar['_shopify_s']).toMatchObject({
domain: `.${domain}`,
maxage: 1800,
});
expect(cookieJar['_shopify_y']).toMatchObject({
domain: `.${domain}`,
maxage: 31104000,
});
});

it('sets domain as is when provided with a leading period', () => {
const cookieJar: MockCookieJar = mockCookie();
const domain = '.myshop.com';

renderHook(() => useShopifyCookies({hasUserConsent: true, domain}));

const cookies = getShopifyCookies(document.cookie);

expect(cookies).toEqual({
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
_shopify_s: expect.any(String),
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
_shopify_y: expect.any(String),
});
expect(cookies['_shopify_s']).not.toBe('');
expect(cookies['_shopify_y']).not.toBe('');

expect(cookieJar['_shopify_s'].value).not.toBe(
cookieJar['_shopify_y'].value,
);
Expand Down
15 changes: 11 additions & 4 deletions packages/hydrogen-react/src/useShopifyCookies.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ export function useShopifyCookies(options?: UseShopifyCookiesOptions): void {
useEffect(() => {
const cookies = getShopifyCookies(document.cookie);

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

/**
* Set user and session cookies and refresh the expiry time
*/
Expand All @@ -32,17 +39,17 @@ export function useShopifyCookies(options?: UseShopifyCookiesOptions): void {
SHOPIFY_Y,
cookies[SHOPIFY_Y] || buildUUID(),
longTermLength,
domain,
domainWithLeadingDot,
);
setCookie(
SHOPIFY_S,
cookies[SHOPIFY_S] || buildUUID(),
shortTermLength,
domain,
domainWithLeadingDot,
);
} else {
setCookie(SHOPIFY_Y, '', 0, domain);
setCookie(SHOPIFY_S, '', 0, domain);
setCookie(SHOPIFY_Y, '', 0, domainWithLeadingDot);
setCookie(SHOPIFY_S, '', 0, domainWithLeadingDot);
}
}, [options, hasUserConsent, domain]);
}
Expand Down

0 comments on commit 73716c8

Please sign in to comment.