Skip to content

Commit

Permalink
fix: cookie domain
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyagabriel committed Oct 22, 2024
1 parent f765b42 commit c277ac5
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
9 changes: 7 additions & 2 deletions app/components/Layout/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,20 @@ import {
} from '~/hooks';

export function Layout({children}: {children: ReactNode}) {
const {consent, shop} = useRootLoaderData();
const {consent, cookieDomain, shop} = useRootLoaderData();
const {mainPaddingTopClass} = usePromobar();
const cartForAnalytics = useCartForAnalytics();
useCartAddDiscountUrl();
usePreviewModeCustomerInit();
useSetViewportHeightCssVar();

return (
<Analytics.Provider shop={shop} cart={cartForAnalytics} consent={consent}>
<Analytics.Provider
shop={shop}
cart={cartForAnalytics}
consent={consent}
cookieDomain={cookieDomain}
>
<>
<PackAnalytics />

Expand Down
13 changes: 13 additions & 0 deletions app/lib/utils/document.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,16 @@ export const getAspectRatioFromPercentage = (
const number = parseInt(match[0], 10);
return `1/${number / 100}` as AspectRatio;
};

export const getCookieDomain = (url: string) => {
try {
const {hostname} = new URL(url);
const domainParts = hostname.split('.');
return `.${
domainParts.length > 2 ? domainParts.slice(-2).join('.') : hostname
}`;
} catch (error) {
console.error(`getCookieDomain:error:`, error);
return '';
}
};
4 changes: 3 additions & 1 deletion app/lib/utils/pack.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ const SESSION_COOKIE = 'pack_session';
const DEFAULT_EXPIRES = 365;

export const setPackCookie = async ({
cookieDomain,
headers,
request,
}: {
cookieDomain: string;
headers: Headers;
request: Request;
}) => {
Expand All @@ -32,7 +34,7 @@ export const setPackCookie = async ({

headers.append(
'Set-Cookie',
`${SESSION_COOKIE}=${sessionCookie}; Path=/; Expires=${expiresAt}; SameSite=Strict; Secure;`,
`${SESSION_COOKIE}=${sessionCookie}; Path=/; Domain=${cookieDomain}; Expires=${expiresAt}; Secure;`,
);
}

Expand Down
4 changes: 4 additions & 0 deletions app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
import {ApplicationError, Document, NotFound, ServerError} from '~/components';
import {customerGetAction, validateCustomerAccessToken} from '~/lib/customer';
import {
getCookieDomain,
getPublicEnvs,
getProductGroupings,
getShop,
Expand Down Expand Up @@ -100,7 +101,9 @@ export async function loader({context, request}: LoaderFunctionArgs) {
customer = customerData.customer;
}
}
const cookieDomain = getCookieDomain(request.url);
const {headers: headersWithPackCookie} = await setPackCookie({
cookieDomain,
headers: headersWithAccessToken,
request,
});
Expand Down Expand Up @@ -130,6 +133,7 @@ export async function loader({context, request}: LoaderFunctionArgs) {
{
analytics,
consent,
cookieDomain,
customer,
customerAccessToken,
customizerMeta: pack.session.get('customizerMeta'),
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "hydrogen-starter",
"private": true,
"sideEffects": false,
"version": "1.9.4",
"version": "1.9.5",
"scripts": {
"dev": "shopify hydrogen dev --port 8080",
"build": "shopify hydrogen build",
Expand Down

0 comments on commit c277ac5

Please sign in to comment.