@shopify/hydrogen-react 2024.4.3 → 2024.4.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser-dev/useShopifyCookies.mjs +17 -2
- package/dist/browser-dev/useShopifyCookies.mjs.map +1 -1
- package/dist/browser-prod/useShopifyCookies.mjs +17 -2
- package/dist/browser-prod/useShopifyCookies.mjs.map +1 -1
- package/dist/node-dev/useShopifyCookies.js +17 -2
- package/dist/node-dev/useShopifyCookies.js.map +1 -1
- package/dist/node-dev/useShopifyCookies.mjs +17 -2
- package/dist/node-dev/useShopifyCookies.mjs.map +1 -1
- package/dist/node-prod/useShopifyCookies.js +17 -2
- package/dist/node-prod/useShopifyCookies.js.map +1 -1
- package/dist/node-prod/useShopifyCookies.mjs +17 -2
- package/dist/node-prod/useShopifyCookies.mjs.map +1 -1
- package/dist/types/useShopifyCookies.d.ts +4 -0
- package/dist/umd/hydrogen-react.dev.js +17 -2
- package/dist/umd/hydrogen-react.dev.js.map +1 -1
- package/dist/umd/hydrogen-react.prod.js +6 -6
- package/dist/umd/hydrogen-react.prod.js.map +1 -1
- package/package.json +1 -1
|
@@ -5,10 +5,25 @@ import { getShopifyCookies, buildUUID } from "./cookies-utils.mjs";
|
|
|
5
5
|
const longTermLength = 60 * 60 * 24 * 360 * 1;
|
|
6
6
|
const shortTermLength = 60 * 30;
|
|
7
7
|
function useShopifyCookies(options) {
|
|
8
|
-
const {
|
|
8
|
+
const {
|
|
9
|
+
hasUserConsent = false,
|
|
10
|
+
domain = "",
|
|
11
|
+
checkoutDomain = ""
|
|
12
|
+
} = options || {};
|
|
9
13
|
useEffect(() => {
|
|
10
14
|
const cookies = getShopifyCookies(document.cookie);
|
|
11
15
|
let currentDomain = domain || window.document.location.host;
|
|
16
|
+
if (checkoutDomain) {
|
|
17
|
+
const checkoutDomainParts = checkoutDomain.split(".").reverse();
|
|
18
|
+
const currentDomainParts = currentDomain.split(".").reverse();
|
|
19
|
+
const sameDomainParts = [];
|
|
20
|
+
checkoutDomainParts.forEach((part, index) => {
|
|
21
|
+
if (part === currentDomainParts[index]) {
|
|
22
|
+
sameDomainParts.push(part);
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
currentDomain = sameDomainParts.reverse().join(".");
|
|
26
|
+
}
|
|
12
27
|
if (/^localhost/.test(currentDomain))
|
|
13
28
|
currentDomain = "";
|
|
14
29
|
const domainWithLeadingDot = currentDomain ? /^\./.test(currentDomain) ? currentDomain : `.${currentDomain}` : "";
|
|
@@ -29,7 +44,7 @@ function useShopifyCookies(options) {
|
|
|
29
44
|
setCookie(SHOPIFY_Y, "", 0, domainWithLeadingDot);
|
|
30
45
|
setCookie(SHOPIFY_S, "", 0, domainWithLeadingDot);
|
|
31
46
|
}
|
|
32
|
-
}, [options, hasUserConsent, domain]);
|
|
47
|
+
}, [options, hasUserConsent, domain, checkoutDomain]);
|
|
33
48
|
}
|
|
34
49
|
function setCookie(name, value, maxage, domain) {
|
|
35
50
|
document.cookie = stringify(name, value, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useShopifyCookies.mjs","sources":["../../src/useShopifyCookies.tsx"],"sourcesContent":["import {useEffect} from 'react';\nimport {stringify} from 'worktop/cookie';\nimport {SHOPIFY_Y, SHOPIFY_S} from './cart-constants.js';\nimport {buildUUID, getShopifyCookies} from './cookies-utils.js';\n\nconst longTermLength = 60 * 60 * 24 * 360 * 1; // ~1 year expiry\nconst shortTermLength = 60 * 30; // 30 mins\n\ntype UseShopifyCookiesOptions = {\n /**\n * If set to `false`, Shopify cookies will be removed.\n * If set to `true`, Shopify unique user token cookie will have cookie expiry of 1 year.\n * Defaults to false.\n **/\n hasUserConsent?: boolean;\n /**\n * The domain scope of the cookie. Defaults to empty string.\n **/\n domain?: string;\n};\n\nexport function useShopifyCookies(options?: UseShopifyCookiesOptions): void {\n const {hasUserConsent = false
|
|
1
|
+
{"version":3,"file":"useShopifyCookies.mjs","sources":["../../src/useShopifyCookies.tsx"],"sourcesContent":["import {useEffect} from 'react';\nimport {stringify} from 'worktop/cookie';\nimport {SHOPIFY_Y, SHOPIFY_S} from './cart-constants.js';\nimport {buildUUID, getShopifyCookies} from './cookies-utils.js';\n\nconst longTermLength = 60 * 60 * 24 * 360 * 1; // ~1 year expiry\nconst shortTermLength = 60 * 30; // 30 mins\n\ntype UseShopifyCookiesOptions = {\n /**\n * If set to `false`, Shopify cookies will be removed.\n * If set to `true`, Shopify unique user token cookie will have cookie expiry of 1 year.\n * Defaults to false.\n **/\n hasUserConsent?: boolean;\n /**\n * The domain scope of the cookie. Defaults to empty string.\n **/\n domain?: string;\n /**\n * The checkout domain of the shop. Defaults to empty string. If set, the cookie domain will check if it can be set with the checkout domain.\n */\n checkoutDomain?: string;\n};\n\nexport function useShopifyCookies(options?: UseShopifyCookiesOptions): void {\n const {\n hasUserConsent = false,\n domain = '',\n checkoutDomain = '',\n } = options || {};\n useEffect(() => {\n const cookies = getShopifyCookies(document.cookie);\n\n /**\n * Setting cookie with domain\n *\n * If no domain is provided, the cookie will be set for the current host.\n * For Shopify, we need to ensure this domain is set with a leading dot.\n */\n\n // Use override domain or current host\n let currentDomain = domain || window.document.location.host;\n\n if (checkoutDomain) {\n const checkoutDomainParts = checkoutDomain.split('.').reverse();\n const currentDomainParts = currentDomain.split('.').reverse();\n const sameDomainParts: Array<string> = [];\n checkoutDomainParts.forEach((part, index) => {\n if (part === currentDomainParts[index]) {\n sameDomainParts.push(part);\n }\n });\n\n currentDomain = sameDomainParts.reverse().join('.');\n }\n\n // Reset domain if localhost\n if (/^localhost/.test(currentDomain)) currentDomain = '';\n\n // Shopify checkout only consumes cookies set with leading dot domain\n const domainWithLeadingDot = currentDomain\n ? /^\\./.test(currentDomain)\n ? currentDomain\n : `.${currentDomain}`\n : '';\n\n /**\n * Set user and session cookies and refresh the expiry time\n */\n if (hasUserConsent) {\n setCookie(\n SHOPIFY_Y,\n cookies[SHOPIFY_Y] || buildUUID(),\n longTermLength,\n domainWithLeadingDot,\n );\n setCookie(\n SHOPIFY_S,\n cookies[SHOPIFY_S] || buildUUID(),\n shortTermLength,\n domainWithLeadingDot,\n );\n } else {\n setCookie(SHOPIFY_Y, '', 0, domainWithLeadingDot);\n setCookie(SHOPIFY_S, '', 0, domainWithLeadingDot);\n }\n }, [options, hasUserConsent, domain, checkoutDomain]);\n}\n\nfunction setCookie(\n name: string,\n value: string,\n maxage: number,\n domain: string,\n): void {\n document.cookie = stringify(name, value, {\n maxage,\n domain,\n samesite: 'Lax',\n path: '/',\n });\n}\n"],"names":[],"mappings":";;;;AAKA,MAAM,iBAAiB,KAAK,KAAK,KAAK,MAAM;AAC5C,MAAM,kBAAkB,KAAK;AAmBtB,SAAS,kBAAkB,SAA0C;AACpE,QAAA;AAAA,IACJ,iBAAiB;AAAA,IACjB,SAAS;AAAA,IACT,iBAAiB;AAAA,EAAA,IACf,WAAW,CAAA;AACf,YAAU,MAAM;AACR,UAAA,UAAU,kBAAkB,SAAS,MAAM;AAUjD,QAAI,gBAAgB,UAAU,OAAO,SAAS,SAAS;AAEvD,QAAI,gBAAgB;AAClB,YAAM,sBAAsB,eAAe,MAAM,GAAG,EAAE,QAAQ;AAC9D,YAAM,qBAAqB,cAAc,MAAM,GAAG,EAAE,QAAQ;AAC5D,YAAM,kBAAiC,CAAA;AACnB,0BAAA,QAAQ,CAAC,MAAM,UAAU;AACvC,YAAA,SAAS,mBAAmB,KAAK,GAAG;AACtC,0BAAgB,KAAK,IAAI;AAAA,QAC3B;AAAA,MAAA,CACD;AAED,sBAAgB,gBAAgB,QAAU,EAAA,KAAK,GAAG;AAAA,IACpD;AAGI,QAAA,aAAa,KAAK,aAAa;AAAmB,sBAAA;AAGhD,UAAA,uBAAuB,gBACzB,MAAM,KAAK,aAAa,IACtB,gBACA,IAAI,aAAa,KACnB;AAKJ,QAAI,gBAAgB;AAClB;AAAA,QACE;AAAA,QACA,QAAQ,SAAS,KAAK,UAAU;AAAA,QAChC;AAAA,QACA;AAAA,MAAA;AAEF;AAAA,QACE;AAAA,QACA,QAAQ,SAAS,KAAK,UAAU;AAAA,QAChC;AAAA,QACA;AAAA,MAAA;AAAA,IACF,OACK;AACK,gBAAA,WAAW,IAAI,GAAG,oBAAoB;AACtC,gBAAA,WAAW,IAAI,GAAG,oBAAoB;AAAA,IAClD;AAAA,KACC,CAAC,SAAS,gBAAgB,QAAQ,cAAc,CAAC;AACtD;AAEA,SAAS,UACP,MACA,OACA,QACA,QACM;AACG,WAAA,SAAS,UAAU,MAAM,OAAO;AAAA,IACvC;AAAA,IACA;AAAA,IACA,UAAU;AAAA,IACV,MAAM;AAAA,EAAA,CACP;AACH;"}
|
|
@@ -5,10 +5,25 @@ import { getShopifyCookies, buildUUID } from "./cookies-utils.mjs";
|
|
|
5
5
|
const longTermLength = 60 * 60 * 24 * 360 * 1;
|
|
6
6
|
const shortTermLength = 60 * 30;
|
|
7
7
|
function useShopifyCookies(options) {
|
|
8
|
-
const {
|
|
8
|
+
const {
|
|
9
|
+
hasUserConsent = false,
|
|
10
|
+
domain = "",
|
|
11
|
+
checkoutDomain = ""
|
|
12
|
+
} = options || {};
|
|
9
13
|
useEffect(() => {
|
|
10
14
|
const cookies = getShopifyCookies(document.cookie);
|
|
11
15
|
let currentDomain = domain || window.document.location.host;
|
|
16
|
+
if (checkoutDomain) {
|
|
17
|
+
const checkoutDomainParts = checkoutDomain.split(".").reverse();
|
|
18
|
+
const currentDomainParts = currentDomain.split(".").reverse();
|
|
19
|
+
const sameDomainParts = [];
|
|
20
|
+
checkoutDomainParts.forEach((part, index) => {
|
|
21
|
+
if (part === currentDomainParts[index]) {
|
|
22
|
+
sameDomainParts.push(part);
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
currentDomain = sameDomainParts.reverse().join(".");
|
|
26
|
+
}
|
|
12
27
|
if (/^localhost/.test(currentDomain))
|
|
13
28
|
currentDomain = "";
|
|
14
29
|
const domainWithLeadingDot = currentDomain ? /^\./.test(currentDomain) ? currentDomain : `.${currentDomain}` : "";
|
|
@@ -29,7 +44,7 @@ function useShopifyCookies(options) {
|
|
|
29
44
|
setCookie(SHOPIFY_Y, "", 0, domainWithLeadingDot);
|
|
30
45
|
setCookie(SHOPIFY_S, "", 0, domainWithLeadingDot);
|
|
31
46
|
}
|
|
32
|
-
}, [options, hasUserConsent, domain]);
|
|
47
|
+
}, [options, hasUserConsent, domain, checkoutDomain]);
|
|
33
48
|
}
|
|
34
49
|
function setCookie(name, value, maxage, domain) {
|
|
35
50
|
document.cookie = stringify(name, value, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useShopifyCookies.mjs","sources":["../../src/useShopifyCookies.tsx"],"sourcesContent":["import {useEffect} from 'react';\nimport {stringify} from 'worktop/cookie';\nimport {SHOPIFY_Y, SHOPIFY_S} from './cart-constants.js';\nimport {buildUUID, getShopifyCookies} from './cookies-utils.js';\n\nconst longTermLength = 60 * 60 * 24 * 360 * 1; // ~1 year expiry\nconst shortTermLength = 60 * 30; // 30 mins\n\ntype UseShopifyCookiesOptions = {\n /**\n * If set to `false`, Shopify cookies will be removed.\n * If set to `true`, Shopify unique user token cookie will have cookie expiry of 1 year.\n * Defaults to false.\n **/\n hasUserConsent?: boolean;\n /**\n * The domain scope of the cookie. Defaults to empty string.\n **/\n domain?: string;\n};\n\nexport function useShopifyCookies(options?: UseShopifyCookiesOptions): void {\n const {hasUserConsent = false
|
|
1
|
+
{"version":3,"file":"useShopifyCookies.mjs","sources":["../../src/useShopifyCookies.tsx"],"sourcesContent":["import {useEffect} from 'react';\nimport {stringify} from 'worktop/cookie';\nimport {SHOPIFY_Y, SHOPIFY_S} from './cart-constants.js';\nimport {buildUUID, getShopifyCookies} from './cookies-utils.js';\n\nconst longTermLength = 60 * 60 * 24 * 360 * 1; // ~1 year expiry\nconst shortTermLength = 60 * 30; // 30 mins\n\ntype UseShopifyCookiesOptions = {\n /**\n * If set to `false`, Shopify cookies will be removed.\n * If set to `true`, Shopify unique user token cookie will have cookie expiry of 1 year.\n * Defaults to false.\n **/\n hasUserConsent?: boolean;\n /**\n * The domain scope of the cookie. Defaults to empty string.\n **/\n domain?: string;\n /**\n * The checkout domain of the shop. Defaults to empty string. If set, the cookie domain will check if it can be set with the checkout domain.\n */\n checkoutDomain?: string;\n};\n\nexport function useShopifyCookies(options?: UseShopifyCookiesOptions): void {\n const {\n hasUserConsent = false,\n domain = '',\n checkoutDomain = '',\n } = options || {};\n useEffect(() => {\n const cookies = getShopifyCookies(document.cookie);\n\n /**\n * Setting cookie with domain\n *\n * If no domain is provided, the cookie will be set for the current host.\n * For Shopify, we need to ensure this domain is set with a leading dot.\n */\n\n // Use override domain or current host\n let currentDomain = domain || window.document.location.host;\n\n if (checkoutDomain) {\n const checkoutDomainParts = checkoutDomain.split('.').reverse();\n const currentDomainParts = currentDomain.split('.').reverse();\n const sameDomainParts: Array<string> = [];\n checkoutDomainParts.forEach((part, index) => {\n if (part === currentDomainParts[index]) {\n sameDomainParts.push(part);\n }\n });\n\n currentDomain = sameDomainParts.reverse().join('.');\n }\n\n // Reset domain if localhost\n if (/^localhost/.test(currentDomain)) currentDomain = '';\n\n // Shopify checkout only consumes cookies set with leading dot domain\n const domainWithLeadingDot = currentDomain\n ? /^\\./.test(currentDomain)\n ? currentDomain\n : `.${currentDomain}`\n : '';\n\n /**\n * Set user and session cookies and refresh the expiry time\n */\n if (hasUserConsent) {\n setCookie(\n SHOPIFY_Y,\n cookies[SHOPIFY_Y] || buildUUID(),\n longTermLength,\n domainWithLeadingDot,\n );\n setCookie(\n SHOPIFY_S,\n cookies[SHOPIFY_S] || buildUUID(),\n shortTermLength,\n domainWithLeadingDot,\n );\n } else {\n setCookie(SHOPIFY_Y, '', 0, domainWithLeadingDot);\n setCookie(SHOPIFY_S, '', 0, domainWithLeadingDot);\n }\n }, [options, hasUserConsent, domain, checkoutDomain]);\n}\n\nfunction setCookie(\n name: string,\n value: string,\n maxage: number,\n domain: string,\n): void {\n document.cookie = stringify(name, value, {\n maxage,\n domain,\n samesite: 'Lax',\n path: '/',\n });\n}\n"],"names":[],"mappings":";;;;AAKA,MAAM,iBAAiB,KAAK,KAAK,KAAK,MAAM;AAC5C,MAAM,kBAAkB,KAAK;AAmBtB,SAAS,kBAAkB,SAA0C;AACpE,QAAA;AAAA,IACJ,iBAAiB;AAAA,IACjB,SAAS;AAAA,IACT,iBAAiB;AAAA,EAAA,IACf,WAAW,CAAA;AACf,YAAU,MAAM;AACR,UAAA,UAAU,kBAAkB,SAAS,MAAM;AAUjD,QAAI,gBAAgB,UAAU,OAAO,SAAS,SAAS;AAEvD,QAAI,gBAAgB;AAClB,YAAM,sBAAsB,eAAe,MAAM,GAAG,EAAE,QAAQ;AAC9D,YAAM,qBAAqB,cAAc,MAAM,GAAG,EAAE,QAAQ;AAC5D,YAAM,kBAAiC,CAAA;AACnB,0BAAA,QAAQ,CAAC,MAAM,UAAU;AACvC,YAAA,SAAS,mBAAmB,KAAK,GAAG;AACtC,0BAAgB,KAAK,IAAI;AAAA,QAC3B;AAAA,MAAA,CACD;AAED,sBAAgB,gBAAgB,QAAU,EAAA,KAAK,GAAG;AAAA,IACpD;AAGI,QAAA,aAAa,KAAK,aAAa;AAAmB,sBAAA;AAGhD,UAAA,uBAAuB,gBACzB,MAAM,KAAK,aAAa,IACtB,gBACA,IAAI,aAAa,KACnB;AAKJ,QAAI,gBAAgB;AAClB;AAAA,QACE;AAAA,QACA,QAAQ,SAAS,KAAK,UAAU;AAAA,QAChC;AAAA,QACA;AAAA,MAAA;AAEF;AAAA,QACE;AAAA,QACA,QAAQ,SAAS,KAAK,UAAU;AAAA,QAChC;AAAA,QACA;AAAA,MAAA;AAAA,IACF,OACK;AACK,gBAAA,WAAW,IAAI,GAAG,oBAAoB;AACtC,gBAAA,WAAW,IAAI,GAAG,oBAAoB;AAAA,IAClD;AAAA,KACC,CAAC,SAAS,gBAAgB,QAAQ,cAAc,CAAC;AACtD;AAEA,SAAS,UACP,MACA,OACA,QACA,QACM;AACG,WAAA,SAAS,UAAU,MAAM,OAAO;AAAA,IACvC;AAAA,IACA;AAAA,IACA,UAAU;AAAA,IACV,MAAM;AAAA,EAAA,CACP;AACH;"}
|
|
@@ -7,10 +7,25 @@ const cookiesUtils = require("./cookies-utils.js");
|
|
|
7
7
|
const longTermLength = 60 * 60 * 24 * 360 * 1;
|
|
8
8
|
const shortTermLength = 60 * 30;
|
|
9
9
|
function useShopifyCookies(options) {
|
|
10
|
-
const {
|
|
10
|
+
const {
|
|
11
|
+
hasUserConsent = false,
|
|
12
|
+
domain = "",
|
|
13
|
+
checkoutDomain = ""
|
|
14
|
+
} = options || {};
|
|
11
15
|
React.useEffect(() => {
|
|
12
16
|
const cookies = cookiesUtils.getShopifyCookies(document.cookie);
|
|
13
17
|
let currentDomain = domain || window.document.location.host;
|
|
18
|
+
if (checkoutDomain) {
|
|
19
|
+
const checkoutDomainParts = checkoutDomain.split(".").reverse();
|
|
20
|
+
const currentDomainParts = currentDomain.split(".").reverse();
|
|
21
|
+
const sameDomainParts = [];
|
|
22
|
+
checkoutDomainParts.forEach((part, index) => {
|
|
23
|
+
if (part === currentDomainParts[index]) {
|
|
24
|
+
sameDomainParts.push(part);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
currentDomain = sameDomainParts.reverse().join(".");
|
|
28
|
+
}
|
|
14
29
|
if (/^localhost/.test(currentDomain))
|
|
15
30
|
currentDomain = "";
|
|
16
31
|
const domainWithLeadingDot = currentDomain ? /^\./.test(currentDomain) ? currentDomain : `.${currentDomain}` : "";
|
|
@@ -31,7 +46,7 @@ function useShopifyCookies(options) {
|
|
|
31
46
|
setCookie(cartConstants.SHOPIFY_Y, "", 0, domainWithLeadingDot);
|
|
32
47
|
setCookie(cartConstants.SHOPIFY_S, "", 0, domainWithLeadingDot);
|
|
33
48
|
}
|
|
34
|
-
}, [options, hasUserConsent, domain]);
|
|
49
|
+
}, [options, hasUserConsent, domain, checkoutDomain]);
|
|
35
50
|
}
|
|
36
51
|
function setCookie(name, value, maxage, domain) {
|
|
37
52
|
document.cookie = cookie.stringify(name, value, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useShopifyCookies.js","sources":["../../src/useShopifyCookies.tsx"],"sourcesContent":["import {useEffect} from 'react';\nimport {stringify} from 'worktop/cookie';\nimport {SHOPIFY_Y, SHOPIFY_S} from './cart-constants.js';\nimport {buildUUID, getShopifyCookies} from './cookies-utils.js';\n\nconst longTermLength = 60 * 60 * 24 * 360 * 1; // ~1 year expiry\nconst shortTermLength = 60 * 30; // 30 mins\n\ntype UseShopifyCookiesOptions = {\n /**\n * If set to `false`, Shopify cookies will be removed.\n * If set to `true`, Shopify unique user token cookie will have cookie expiry of 1 year.\n * Defaults to false.\n **/\n hasUserConsent?: boolean;\n /**\n * The domain scope of the cookie. Defaults to empty string.\n **/\n domain?: string;\n};\n\nexport function useShopifyCookies(options?: UseShopifyCookiesOptions): void {\n const {hasUserConsent = false
|
|
1
|
+
{"version":3,"file":"useShopifyCookies.js","sources":["../../src/useShopifyCookies.tsx"],"sourcesContent":["import {useEffect} from 'react';\nimport {stringify} from 'worktop/cookie';\nimport {SHOPIFY_Y, SHOPIFY_S} from './cart-constants.js';\nimport {buildUUID, getShopifyCookies} from './cookies-utils.js';\n\nconst longTermLength = 60 * 60 * 24 * 360 * 1; // ~1 year expiry\nconst shortTermLength = 60 * 30; // 30 mins\n\ntype UseShopifyCookiesOptions = {\n /**\n * If set to `false`, Shopify cookies will be removed.\n * If set to `true`, Shopify unique user token cookie will have cookie expiry of 1 year.\n * Defaults to false.\n **/\n hasUserConsent?: boolean;\n /**\n * The domain scope of the cookie. Defaults to empty string.\n **/\n domain?: string;\n /**\n * The checkout domain of the shop. Defaults to empty string. If set, the cookie domain will check if it can be set with the checkout domain.\n */\n checkoutDomain?: string;\n};\n\nexport function useShopifyCookies(options?: UseShopifyCookiesOptions): void {\n const {\n hasUserConsent = false,\n domain = '',\n checkoutDomain = '',\n } = options || {};\n useEffect(() => {\n const cookies = getShopifyCookies(document.cookie);\n\n /**\n * Setting cookie with domain\n *\n * If no domain is provided, the cookie will be set for the current host.\n * For Shopify, we need to ensure this domain is set with a leading dot.\n */\n\n // Use override domain or current host\n let currentDomain = domain || window.document.location.host;\n\n if (checkoutDomain) {\n const checkoutDomainParts = checkoutDomain.split('.').reverse();\n const currentDomainParts = currentDomain.split('.').reverse();\n const sameDomainParts: Array<string> = [];\n checkoutDomainParts.forEach((part, index) => {\n if (part === currentDomainParts[index]) {\n sameDomainParts.push(part);\n }\n });\n\n currentDomain = sameDomainParts.reverse().join('.');\n }\n\n // Reset domain if localhost\n if (/^localhost/.test(currentDomain)) currentDomain = '';\n\n // Shopify checkout only consumes cookies set with leading dot domain\n const domainWithLeadingDot = currentDomain\n ? /^\\./.test(currentDomain)\n ? currentDomain\n : `.${currentDomain}`\n : '';\n\n /**\n * Set user and session cookies and refresh the expiry time\n */\n if (hasUserConsent) {\n setCookie(\n SHOPIFY_Y,\n cookies[SHOPIFY_Y] || buildUUID(),\n longTermLength,\n domainWithLeadingDot,\n );\n setCookie(\n SHOPIFY_S,\n cookies[SHOPIFY_S] || buildUUID(),\n shortTermLength,\n domainWithLeadingDot,\n );\n } else {\n setCookie(SHOPIFY_Y, '', 0, domainWithLeadingDot);\n setCookie(SHOPIFY_S, '', 0, domainWithLeadingDot);\n }\n }, [options, hasUserConsent, domain, checkoutDomain]);\n}\n\nfunction setCookie(\n name: string,\n value: string,\n maxage: number,\n domain: string,\n): void {\n document.cookie = stringify(name, value, {\n maxage,\n domain,\n samesite: 'Lax',\n path: '/',\n });\n}\n"],"names":["useEffect","getShopifyCookies","SHOPIFY_Y","buildUUID","SHOPIFY_S","stringify"],"mappings":";;;;;;AAKA,MAAM,iBAAiB,KAAK,KAAK,KAAK,MAAM;AAC5C,MAAM,kBAAkB,KAAK;AAmBtB,SAAS,kBAAkB,SAA0C;AACpE,QAAA;AAAA,IACJ,iBAAiB;AAAA,IACjB,SAAS;AAAA,IACT,iBAAiB;AAAA,EAAA,IACf,WAAW,CAAA;AACfA,QAAAA,UAAU,MAAM;AACR,UAAA,UAAUC,aAAAA,kBAAkB,SAAS,MAAM;AAUjD,QAAI,gBAAgB,UAAU,OAAO,SAAS,SAAS;AAEvD,QAAI,gBAAgB;AAClB,YAAM,sBAAsB,eAAe,MAAM,GAAG,EAAE,QAAQ;AAC9D,YAAM,qBAAqB,cAAc,MAAM,GAAG,EAAE,QAAQ;AAC5D,YAAM,kBAAiC,CAAA;AACnB,0BAAA,QAAQ,CAAC,MAAM,UAAU;AACvC,YAAA,SAAS,mBAAmB,KAAK,GAAG;AACtC,0BAAgB,KAAK,IAAI;AAAA,QAC3B;AAAA,MAAA,CACD;AAED,sBAAgB,gBAAgB,QAAU,EAAA,KAAK,GAAG;AAAA,IACpD;AAGI,QAAA,aAAa,KAAK,aAAa;AAAmB,sBAAA;AAGhD,UAAA,uBAAuB,gBACzB,MAAM,KAAK,aAAa,IACtB,gBACA,IAAI,aAAa,KACnB;AAKJ,QAAI,gBAAgB;AAClB;AAAA,QACEC,cAAA;AAAA,QACA,QAAQA,cAAAA,SAAS,KAAKC,uBAAU;AAAA,QAChC;AAAA,QACA;AAAA,MAAA;AAEF;AAAA,QACEC,cAAA;AAAA,QACA,QAAQA,cAAAA,SAAS,KAAKD,uBAAU;AAAA,QAChC;AAAA,QACA;AAAA,MAAA;AAAA,IACF,OACK;AACK,gBAAAD,cAAAA,WAAW,IAAI,GAAG,oBAAoB;AACtC,gBAAAE,cAAAA,WAAW,IAAI,GAAG,oBAAoB;AAAA,IAClD;AAAA,KACC,CAAC,SAAS,gBAAgB,QAAQ,cAAc,CAAC;AACtD;AAEA,SAAS,UACP,MACA,OACA,QACA,QACM;AACG,WAAA,SAASC,iBAAU,MAAM,OAAO;AAAA,IACvC;AAAA,IACA;AAAA,IACA,UAAU;AAAA,IACV,MAAM;AAAA,EAAA,CACP;AACH;;"}
|
|
@@ -5,10 +5,25 @@ import { getShopifyCookies, buildUUID } from "./cookies-utils.mjs";
|
|
|
5
5
|
const longTermLength = 60 * 60 * 24 * 360 * 1;
|
|
6
6
|
const shortTermLength = 60 * 30;
|
|
7
7
|
function useShopifyCookies(options) {
|
|
8
|
-
const {
|
|
8
|
+
const {
|
|
9
|
+
hasUserConsent = false,
|
|
10
|
+
domain = "",
|
|
11
|
+
checkoutDomain = ""
|
|
12
|
+
} = options || {};
|
|
9
13
|
useEffect(() => {
|
|
10
14
|
const cookies = getShopifyCookies(document.cookie);
|
|
11
15
|
let currentDomain = domain || window.document.location.host;
|
|
16
|
+
if (checkoutDomain) {
|
|
17
|
+
const checkoutDomainParts = checkoutDomain.split(".").reverse();
|
|
18
|
+
const currentDomainParts = currentDomain.split(".").reverse();
|
|
19
|
+
const sameDomainParts = [];
|
|
20
|
+
checkoutDomainParts.forEach((part, index) => {
|
|
21
|
+
if (part === currentDomainParts[index]) {
|
|
22
|
+
sameDomainParts.push(part);
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
currentDomain = sameDomainParts.reverse().join(".");
|
|
26
|
+
}
|
|
12
27
|
if (/^localhost/.test(currentDomain))
|
|
13
28
|
currentDomain = "";
|
|
14
29
|
const domainWithLeadingDot = currentDomain ? /^\./.test(currentDomain) ? currentDomain : `.${currentDomain}` : "";
|
|
@@ -29,7 +44,7 @@ function useShopifyCookies(options) {
|
|
|
29
44
|
setCookie(SHOPIFY_Y, "", 0, domainWithLeadingDot);
|
|
30
45
|
setCookie(SHOPIFY_S, "", 0, domainWithLeadingDot);
|
|
31
46
|
}
|
|
32
|
-
}, [options, hasUserConsent, domain]);
|
|
47
|
+
}, [options, hasUserConsent, domain, checkoutDomain]);
|
|
33
48
|
}
|
|
34
49
|
function setCookie(name, value, maxage, domain) {
|
|
35
50
|
document.cookie = stringify(name, value, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useShopifyCookies.mjs","sources":["../../src/useShopifyCookies.tsx"],"sourcesContent":["import {useEffect} from 'react';\nimport {stringify} from 'worktop/cookie';\nimport {SHOPIFY_Y, SHOPIFY_S} from './cart-constants.js';\nimport {buildUUID, getShopifyCookies} from './cookies-utils.js';\n\nconst longTermLength = 60 * 60 * 24 * 360 * 1; // ~1 year expiry\nconst shortTermLength = 60 * 30; // 30 mins\n\ntype UseShopifyCookiesOptions = {\n /**\n * If set to `false`, Shopify cookies will be removed.\n * If set to `true`, Shopify unique user token cookie will have cookie expiry of 1 year.\n * Defaults to false.\n **/\n hasUserConsent?: boolean;\n /**\n * The domain scope of the cookie. Defaults to empty string.\n **/\n domain?: string;\n};\n\nexport function useShopifyCookies(options?: UseShopifyCookiesOptions): void {\n const {hasUserConsent = false
|
|
1
|
+
{"version":3,"file":"useShopifyCookies.mjs","sources":["../../src/useShopifyCookies.tsx"],"sourcesContent":["import {useEffect} from 'react';\nimport {stringify} from 'worktop/cookie';\nimport {SHOPIFY_Y, SHOPIFY_S} from './cart-constants.js';\nimport {buildUUID, getShopifyCookies} from './cookies-utils.js';\n\nconst longTermLength = 60 * 60 * 24 * 360 * 1; // ~1 year expiry\nconst shortTermLength = 60 * 30; // 30 mins\n\ntype UseShopifyCookiesOptions = {\n /**\n * If set to `false`, Shopify cookies will be removed.\n * If set to `true`, Shopify unique user token cookie will have cookie expiry of 1 year.\n * Defaults to false.\n **/\n hasUserConsent?: boolean;\n /**\n * The domain scope of the cookie. Defaults to empty string.\n **/\n domain?: string;\n /**\n * The checkout domain of the shop. Defaults to empty string. If set, the cookie domain will check if it can be set with the checkout domain.\n */\n checkoutDomain?: string;\n};\n\nexport function useShopifyCookies(options?: UseShopifyCookiesOptions): void {\n const {\n hasUserConsent = false,\n domain = '',\n checkoutDomain = '',\n } = options || {};\n useEffect(() => {\n const cookies = getShopifyCookies(document.cookie);\n\n /**\n * Setting cookie with domain\n *\n * If no domain is provided, the cookie will be set for the current host.\n * For Shopify, we need to ensure this domain is set with a leading dot.\n */\n\n // Use override domain or current host\n let currentDomain = domain || window.document.location.host;\n\n if (checkoutDomain) {\n const checkoutDomainParts = checkoutDomain.split('.').reverse();\n const currentDomainParts = currentDomain.split('.').reverse();\n const sameDomainParts: Array<string> = [];\n checkoutDomainParts.forEach((part, index) => {\n if (part === currentDomainParts[index]) {\n sameDomainParts.push(part);\n }\n });\n\n currentDomain = sameDomainParts.reverse().join('.');\n }\n\n // Reset domain if localhost\n if (/^localhost/.test(currentDomain)) currentDomain = '';\n\n // Shopify checkout only consumes cookies set with leading dot domain\n const domainWithLeadingDot = currentDomain\n ? /^\\./.test(currentDomain)\n ? currentDomain\n : `.${currentDomain}`\n : '';\n\n /**\n * Set user and session cookies and refresh the expiry time\n */\n if (hasUserConsent) {\n setCookie(\n SHOPIFY_Y,\n cookies[SHOPIFY_Y] || buildUUID(),\n longTermLength,\n domainWithLeadingDot,\n );\n setCookie(\n SHOPIFY_S,\n cookies[SHOPIFY_S] || buildUUID(),\n shortTermLength,\n domainWithLeadingDot,\n );\n } else {\n setCookie(SHOPIFY_Y, '', 0, domainWithLeadingDot);\n setCookie(SHOPIFY_S, '', 0, domainWithLeadingDot);\n }\n }, [options, hasUserConsent, domain, checkoutDomain]);\n}\n\nfunction setCookie(\n name: string,\n value: string,\n maxage: number,\n domain: string,\n): void {\n document.cookie = stringify(name, value, {\n maxage,\n domain,\n samesite: 'Lax',\n path: '/',\n });\n}\n"],"names":[],"mappings":";;;;AAKA,MAAM,iBAAiB,KAAK,KAAK,KAAK,MAAM;AAC5C,MAAM,kBAAkB,KAAK;AAmBtB,SAAS,kBAAkB,SAA0C;AACpE,QAAA;AAAA,IACJ,iBAAiB;AAAA,IACjB,SAAS;AAAA,IACT,iBAAiB;AAAA,EAAA,IACf,WAAW,CAAA;AACf,YAAU,MAAM;AACR,UAAA,UAAU,kBAAkB,SAAS,MAAM;AAUjD,QAAI,gBAAgB,UAAU,OAAO,SAAS,SAAS;AAEvD,QAAI,gBAAgB;AAClB,YAAM,sBAAsB,eAAe,MAAM,GAAG,EAAE,QAAQ;AAC9D,YAAM,qBAAqB,cAAc,MAAM,GAAG,EAAE,QAAQ;AAC5D,YAAM,kBAAiC,CAAA;AACnB,0BAAA,QAAQ,CAAC,MAAM,UAAU;AACvC,YAAA,SAAS,mBAAmB,KAAK,GAAG;AACtC,0BAAgB,KAAK,IAAI;AAAA,QAC3B;AAAA,MAAA,CACD;AAED,sBAAgB,gBAAgB,QAAU,EAAA,KAAK,GAAG;AAAA,IACpD;AAGI,QAAA,aAAa,KAAK,aAAa;AAAmB,sBAAA;AAGhD,UAAA,uBAAuB,gBACzB,MAAM,KAAK,aAAa,IACtB,gBACA,IAAI,aAAa,KACnB;AAKJ,QAAI,gBAAgB;AAClB;AAAA,QACE;AAAA,QACA,QAAQ,SAAS,KAAK,UAAU;AAAA,QAChC;AAAA,QACA;AAAA,MAAA;AAEF;AAAA,QACE;AAAA,QACA,QAAQ,SAAS,KAAK,UAAU;AAAA,QAChC;AAAA,QACA;AAAA,MAAA;AAAA,IACF,OACK;AACK,gBAAA,WAAW,IAAI,GAAG,oBAAoB;AACtC,gBAAA,WAAW,IAAI,GAAG,oBAAoB;AAAA,IAClD;AAAA,KACC,CAAC,SAAS,gBAAgB,QAAQ,cAAc,CAAC;AACtD;AAEA,SAAS,UACP,MACA,OACA,QACA,QACM;AACG,WAAA,SAAS,UAAU,MAAM,OAAO;AAAA,IACvC;AAAA,IACA;AAAA,IACA,UAAU;AAAA,IACV,MAAM;AAAA,EAAA,CACP;AACH;"}
|
|
@@ -7,10 +7,25 @@ const cookiesUtils = require("./cookies-utils.js");
|
|
|
7
7
|
const longTermLength = 60 * 60 * 24 * 360 * 1;
|
|
8
8
|
const shortTermLength = 60 * 30;
|
|
9
9
|
function useShopifyCookies(options) {
|
|
10
|
-
const {
|
|
10
|
+
const {
|
|
11
|
+
hasUserConsent = false,
|
|
12
|
+
domain = "",
|
|
13
|
+
checkoutDomain = ""
|
|
14
|
+
} = options || {};
|
|
11
15
|
React.useEffect(() => {
|
|
12
16
|
const cookies = cookiesUtils.getShopifyCookies(document.cookie);
|
|
13
17
|
let currentDomain = domain || window.document.location.host;
|
|
18
|
+
if (checkoutDomain) {
|
|
19
|
+
const checkoutDomainParts = checkoutDomain.split(".").reverse();
|
|
20
|
+
const currentDomainParts = currentDomain.split(".").reverse();
|
|
21
|
+
const sameDomainParts = [];
|
|
22
|
+
checkoutDomainParts.forEach((part, index) => {
|
|
23
|
+
if (part === currentDomainParts[index]) {
|
|
24
|
+
sameDomainParts.push(part);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
currentDomain = sameDomainParts.reverse().join(".");
|
|
28
|
+
}
|
|
14
29
|
if (/^localhost/.test(currentDomain))
|
|
15
30
|
currentDomain = "";
|
|
16
31
|
const domainWithLeadingDot = currentDomain ? /^\./.test(currentDomain) ? currentDomain : `.${currentDomain}` : "";
|
|
@@ -31,7 +46,7 @@ function useShopifyCookies(options) {
|
|
|
31
46
|
setCookie(cartConstants.SHOPIFY_Y, "", 0, domainWithLeadingDot);
|
|
32
47
|
setCookie(cartConstants.SHOPIFY_S, "", 0, domainWithLeadingDot);
|
|
33
48
|
}
|
|
34
|
-
}, [options, hasUserConsent, domain]);
|
|
49
|
+
}, [options, hasUserConsent, domain, checkoutDomain]);
|
|
35
50
|
}
|
|
36
51
|
function setCookie(name, value, maxage, domain) {
|
|
37
52
|
document.cookie = cookie.stringify(name, value, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useShopifyCookies.js","sources":["../../src/useShopifyCookies.tsx"],"sourcesContent":["import {useEffect} from 'react';\nimport {stringify} from 'worktop/cookie';\nimport {SHOPIFY_Y, SHOPIFY_S} from './cart-constants.js';\nimport {buildUUID, getShopifyCookies} from './cookies-utils.js';\n\nconst longTermLength = 60 * 60 * 24 * 360 * 1; // ~1 year expiry\nconst shortTermLength = 60 * 30; // 30 mins\n\ntype UseShopifyCookiesOptions = {\n /**\n * If set to `false`, Shopify cookies will be removed.\n * If set to `true`, Shopify unique user token cookie will have cookie expiry of 1 year.\n * Defaults to false.\n **/\n hasUserConsent?: boolean;\n /**\n * The domain scope of the cookie. Defaults to empty string.\n **/\n domain?: string;\n};\n\nexport function useShopifyCookies(options?: UseShopifyCookiesOptions): void {\n const {hasUserConsent = false
|
|
1
|
+
{"version":3,"file":"useShopifyCookies.js","sources":["../../src/useShopifyCookies.tsx"],"sourcesContent":["import {useEffect} from 'react';\nimport {stringify} from 'worktop/cookie';\nimport {SHOPIFY_Y, SHOPIFY_S} from './cart-constants.js';\nimport {buildUUID, getShopifyCookies} from './cookies-utils.js';\n\nconst longTermLength = 60 * 60 * 24 * 360 * 1; // ~1 year expiry\nconst shortTermLength = 60 * 30; // 30 mins\n\ntype UseShopifyCookiesOptions = {\n /**\n * If set to `false`, Shopify cookies will be removed.\n * If set to `true`, Shopify unique user token cookie will have cookie expiry of 1 year.\n * Defaults to false.\n **/\n hasUserConsent?: boolean;\n /**\n * The domain scope of the cookie. Defaults to empty string.\n **/\n domain?: string;\n /**\n * The checkout domain of the shop. Defaults to empty string. If set, the cookie domain will check if it can be set with the checkout domain.\n */\n checkoutDomain?: string;\n};\n\nexport function useShopifyCookies(options?: UseShopifyCookiesOptions): void {\n const {\n hasUserConsent = false,\n domain = '',\n checkoutDomain = '',\n } = options || {};\n useEffect(() => {\n const cookies = getShopifyCookies(document.cookie);\n\n /**\n * Setting cookie with domain\n *\n * If no domain is provided, the cookie will be set for the current host.\n * For Shopify, we need to ensure this domain is set with a leading dot.\n */\n\n // Use override domain or current host\n let currentDomain = domain || window.document.location.host;\n\n if (checkoutDomain) {\n const checkoutDomainParts = checkoutDomain.split('.').reverse();\n const currentDomainParts = currentDomain.split('.').reverse();\n const sameDomainParts: Array<string> = [];\n checkoutDomainParts.forEach((part, index) => {\n if (part === currentDomainParts[index]) {\n sameDomainParts.push(part);\n }\n });\n\n currentDomain = sameDomainParts.reverse().join('.');\n }\n\n // Reset domain if localhost\n if (/^localhost/.test(currentDomain)) currentDomain = '';\n\n // Shopify checkout only consumes cookies set with leading dot domain\n const domainWithLeadingDot = currentDomain\n ? /^\\./.test(currentDomain)\n ? currentDomain\n : `.${currentDomain}`\n : '';\n\n /**\n * Set user and session cookies and refresh the expiry time\n */\n if (hasUserConsent) {\n setCookie(\n SHOPIFY_Y,\n cookies[SHOPIFY_Y] || buildUUID(),\n longTermLength,\n domainWithLeadingDot,\n );\n setCookie(\n SHOPIFY_S,\n cookies[SHOPIFY_S] || buildUUID(),\n shortTermLength,\n domainWithLeadingDot,\n );\n } else {\n setCookie(SHOPIFY_Y, '', 0, domainWithLeadingDot);\n setCookie(SHOPIFY_S, '', 0, domainWithLeadingDot);\n }\n }, [options, hasUserConsent, domain, checkoutDomain]);\n}\n\nfunction setCookie(\n name: string,\n value: string,\n maxage: number,\n domain: string,\n): void {\n document.cookie = stringify(name, value, {\n maxage,\n domain,\n samesite: 'Lax',\n path: '/',\n });\n}\n"],"names":["useEffect","getShopifyCookies","SHOPIFY_Y","buildUUID","SHOPIFY_S","stringify"],"mappings":";;;;;;AAKA,MAAM,iBAAiB,KAAK,KAAK,KAAK,MAAM;AAC5C,MAAM,kBAAkB,KAAK;AAmBtB,SAAS,kBAAkB,SAA0C;AACpE,QAAA;AAAA,IACJ,iBAAiB;AAAA,IACjB,SAAS;AAAA,IACT,iBAAiB;AAAA,EAAA,IACf,WAAW,CAAA;AACfA,QAAAA,UAAU,MAAM;AACR,UAAA,UAAUC,aAAAA,kBAAkB,SAAS,MAAM;AAUjD,QAAI,gBAAgB,UAAU,OAAO,SAAS,SAAS;AAEvD,QAAI,gBAAgB;AAClB,YAAM,sBAAsB,eAAe,MAAM,GAAG,EAAE,QAAQ;AAC9D,YAAM,qBAAqB,cAAc,MAAM,GAAG,EAAE,QAAQ;AAC5D,YAAM,kBAAiC,CAAA;AACnB,0BAAA,QAAQ,CAAC,MAAM,UAAU;AACvC,YAAA,SAAS,mBAAmB,KAAK,GAAG;AACtC,0BAAgB,KAAK,IAAI;AAAA,QAC3B;AAAA,MAAA,CACD;AAED,sBAAgB,gBAAgB,QAAU,EAAA,KAAK,GAAG;AAAA,IACpD;AAGI,QAAA,aAAa,KAAK,aAAa;AAAmB,sBAAA;AAGhD,UAAA,uBAAuB,gBACzB,MAAM,KAAK,aAAa,IACtB,gBACA,IAAI,aAAa,KACnB;AAKJ,QAAI,gBAAgB;AAClB;AAAA,QACEC,cAAA;AAAA,QACA,QAAQA,cAAAA,SAAS,KAAKC,uBAAU;AAAA,QAChC;AAAA,QACA;AAAA,MAAA;AAEF;AAAA,QACEC,cAAA;AAAA,QACA,QAAQA,cAAAA,SAAS,KAAKD,uBAAU;AAAA,QAChC;AAAA,QACA;AAAA,MAAA;AAAA,IACF,OACK;AACK,gBAAAD,cAAAA,WAAW,IAAI,GAAG,oBAAoB;AACtC,gBAAAE,cAAAA,WAAW,IAAI,GAAG,oBAAoB;AAAA,IAClD;AAAA,KACC,CAAC,SAAS,gBAAgB,QAAQ,cAAc,CAAC;AACtD;AAEA,SAAS,UACP,MACA,OACA,QACA,QACM;AACG,WAAA,SAASC,iBAAU,MAAM,OAAO;AAAA,IACvC;AAAA,IACA;AAAA,IACA,UAAU;AAAA,IACV,MAAM;AAAA,EAAA,CACP;AACH;;"}
|
|
@@ -5,10 +5,25 @@ import { getShopifyCookies, buildUUID } from "./cookies-utils.mjs";
|
|
|
5
5
|
const longTermLength = 60 * 60 * 24 * 360 * 1;
|
|
6
6
|
const shortTermLength = 60 * 30;
|
|
7
7
|
function useShopifyCookies(options) {
|
|
8
|
-
const {
|
|
8
|
+
const {
|
|
9
|
+
hasUserConsent = false,
|
|
10
|
+
domain = "",
|
|
11
|
+
checkoutDomain = ""
|
|
12
|
+
} = options || {};
|
|
9
13
|
useEffect(() => {
|
|
10
14
|
const cookies = getShopifyCookies(document.cookie);
|
|
11
15
|
let currentDomain = domain || window.document.location.host;
|
|
16
|
+
if (checkoutDomain) {
|
|
17
|
+
const checkoutDomainParts = checkoutDomain.split(".").reverse();
|
|
18
|
+
const currentDomainParts = currentDomain.split(".").reverse();
|
|
19
|
+
const sameDomainParts = [];
|
|
20
|
+
checkoutDomainParts.forEach((part, index) => {
|
|
21
|
+
if (part === currentDomainParts[index]) {
|
|
22
|
+
sameDomainParts.push(part);
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
currentDomain = sameDomainParts.reverse().join(".");
|
|
26
|
+
}
|
|
12
27
|
if (/^localhost/.test(currentDomain))
|
|
13
28
|
currentDomain = "";
|
|
14
29
|
const domainWithLeadingDot = currentDomain ? /^\./.test(currentDomain) ? currentDomain : `.${currentDomain}` : "";
|
|
@@ -29,7 +44,7 @@ function useShopifyCookies(options) {
|
|
|
29
44
|
setCookie(SHOPIFY_Y, "", 0, domainWithLeadingDot);
|
|
30
45
|
setCookie(SHOPIFY_S, "", 0, domainWithLeadingDot);
|
|
31
46
|
}
|
|
32
|
-
}, [options, hasUserConsent, domain]);
|
|
47
|
+
}, [options, hasUserConsent, domain, checkoutDomain]);
|
|
33
48
|
}
|
|
34
49
|
function setCookie(name, value, maxage, domain) {
|
|
35
50
|
document.cookie = stringify(name, value, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useShopifyCookies.mjs","sources":["../../src/useShopifyCookies.tsx"],"sourcesContent":["import {useEffect} from 'react';\nimport {stringify} from 'worktop/cookie';\nimport {SHOPIFY_Y, SHOPIFY_S} from './cart-constants.js';\nimport {buildUUID, getShopifyCookies} from './cookies-utils.js';\n\nconst longTermLength = 60 * 60 * 24 * 360 * 1; // ~1 year expiry\nconst shortTermLength = 60 * 30; // 30 mins\n\ntype UseShopifyCookiesOptions = {\n /**\n * If set to `false`, Shopify cookies will be removed.\n * If set to `true`, Shopify unique user token cookie will have cookie expiry of 1 year.\n * Defaults to false.\n **/\n hasUserConsent?: boolean;\n /**\n * The domain scope of the cookie. Defaults to empty string.\n **/\n domain?: string;\n};\n\nexport function useShopifyCookies(options?: UseShopifyCookiesOptions): void {\n const {hasUserConsent = false
|
|
1
|
+
{"version":3,"file":"useShopifyCookies.mjs","sources":["../../src/useShopifyCookies.tsx"],"sourcesContent":["import {useEffect} from 'react';\nimport {stringify} from 'worktop/cookie';\nimport {SHOPIFY_Y, SHOPIFY_S} from './cart-constants.js';\nimport {buildUUID, getShopifyCookies} from './cookies-utils.js';\n\nconst longTermLength = 60 * 60 * 24 * 360 * 1; // ~1 year expiry\nconst shortTermLength = 60 * 30; // 30 mins\n\ntype UseShopifyCookiesOptions = {\n /**\n * If set to `false`, Shopify cookies will be removed.\n * If set to `true`, Shopify unique user token cookie will have cookie expiry of 1 year.\n * Defaults to false.\n **/\n hasUserConsent?: boolean;\n /**\n * The domain scope of the cookie. Defaults to empty string.\n **/\n domain?: string;\n /**\n * The checkout domain of the shop. Defaults to empty string. If set, the cookie domain will check if it can be set with the checkout domain.\n */\n checkoutDomain?: string;\n};\n\nexport function useShopifyCookies(options?: UseShopifyCookiesOptions): void {\n const {\n hasUserConsent = false,\n domain = '',\n checkoutDomain = '',\n } = options || {};\n useEffect(() => {\n const cookies = getShopifyCookies(document.cookie);\n\n /**\n * Setting cookie with domain\n *\n * If no domain is provided, the cookie will be set for the current host.\n * For Shopify, we need to ensure this domain is set with a leading dot.\n */\n\n // Use override domain or current host\n let currentDomain = domain || window.document.location.host;\n\n if (checkoutDomain) {\n const checkoutDomainParts = checkoutDomain.split('.').reverse();\n const currentDomainParts = currentDomain.split('.').reverse();\n const sameDomainParts: Array<string> = [];\n checkoutDomainParts.forEach((part, index) => {\n if (part === currentDomainParts[index]) {\n sameDomainParts.push(part);\n }\n });\n\n currentDomain = sameDomainParts.reverse().join('.');\n }\n\n // Reset domain if localhost\n if (/^localhost/.test(currentDomain)) currentDomain = '';\n\n // Shopify checkout only consumes cookies set with leading dot domain\n const domainWithLeadingDot = currentDomain\n ? /^\\./.test(currentDomain)\n ? currentDomain\n : `.${currentDomain}`\n : '';\n\n /**\n * Set user and session cookies and refresh the expiry time\n */\n if (hasUserConsent) {\n setCookie(\n SHOPIFY_Y,\n cookies[SHOPIFY_Y] || buildUUID(),\n longTermLength,\n domainWithLeadingDot,\n );\n setCookie(\n SHOPIFY_S,\n cookies[SHOPIFY_S] || buildUUID(),\n shortTermLength,\n domainWithLeadingDot,\n );\n } else {\n setCookie(SHOPIFY_Y, '', 0, domainWithLeadingDot);\n setCookie(SHOPIFY_S, '', 0, domainWithLeadingDot);\n }\n }, [options, hasUserConsent, domain, checkoutDomain]);\n}\n\nfunction setCookie(\n name: string,\n value: string,\n maxage: number,\n domain: string,\n): void {\n document.cookie = stringify(name, value, {\n maxage,\n domain,\n samesite: 'Lax',\n path: '/',\n });\n}\n"],"names":[],"mappings":";;;;AAKA,MAAM,iBAAiB,KAAK,KAAK,KAAK,MAAM;AAC5C,MAAM,kBAAkB,KAAK;AAmBtB,SAAS,kBAAkB,SAA0C;AACpE,QAAA;AAAA,IACJ,iBAAiB;AAAA,IACjB,SAAS;AAAA,IACT,iBAAiB;AAAA,EAAA,IACf,WAAW,CAAA;AACf,YAAU,MAAM;AACR,UAAA,UAAU,kBAAkB,SAAS,MAAM;AAUjD,QAAI,gBAAgB,UAAU,OAAO,SAAS,SAAS;AAEvD,QAAI,gBAAgB;AAClB,YAAM,sBAAsB,eAAe,MAAM,GAAG,EAAE,QAAQ;AAC9D,YAAM,qBAAqB,cAAc,MAAM,GAAG,EAAE,QAAQ;AAC5D,YAAM,kBAAiC,CAAA;AACnB,0BAAA,QAAQ,CAAC,MAAM,UAAU;AACvC,YAAA,SAAS,mBAAmB,KAAK,GAAG;AACtC,0BAAgB,KAAK,IAAI;AAAA,QAC3B;AAAA,MAAA,CACD;AAED,sBAAgB,gBAAgB,QAAU,EAAA,KAAK,GAAG;AAAA,IACpD;AAGI,QAAA,aAAa,KAAK,aAAa;AAAmB,sBAAA;AAGhD,UAAA,uBAAuB,gBACzB,MAAM,KAAK,aAAa,IACtB,gBACA,IAAI,aAAa,KACnB;AAKJ,QAAI,gBAAgB;AAClB;AAAA,QACE;AAAA,QACA,QAAQ,SAAS,KAAK,UAAU;AAAA,QAChC;AAAA,QACA;AAAA,MAAA;AAEF;AAAA,QACE;AAAA,QACA,QAAQ,SAAS,KAAK,UAAU;AAAA,QAChC;AAAA,QACA;AAAA,MAAA;AAAA,IACF,OACK;AACK,gBAAA,WAAW,IAAI,GAAG,oBAAoB;AACtC,gBAAA,WAAW,IAAI,GAAG,oBAAoB;AAAA,IAClD;AAAA,KACC,CAAC,SAAS,gBAAgB,QAAQ,cAAc,CAAC;AACtD;AAEA,SAAS,UACP,MACA,OACA,QACA,QACM;AACG,WAAA,SAAS,UAAU,MAAM,OAAO;AAAA,IACvC;AAAA,IACA;AAAA,IACA,UAAU;AAAA,IACV,MAAM;AAAA,EAAA,CACP;AACH;"}
|
|
@@ -9,6 +9,10 @@ type UseShopifyCookiesOptions = {
|
|
|
9
9
|
* The domain scope of the cookie. Defaults to empty string.
|
|
10
10
|
**/
|
|
11
11
|
domain?: string;
|
|
12
|
+
/**
|
|
13
|
+
* The checkout domain of the shop. Defaults to empty string. If set, the cookie domain will check if it can be set with the checkout domain.
|
|
14
|
+
*/
|
|
15
|
+
checkoutDomain?: string;
|
|
12
16
|
};
|
|
13
17
|
export declare function useShopifyCookies(options?: UseShopifyCookiesOptions): void;
|
|
14
18
|
export {};
|
|
@@ -4150,10 +4150,25 @@ Refer to the authentication https://shopify.dev/api/storefront#authentication do
|
|
|
4150
4150
|
const longTermLength = 60 * 60 * 24 * 360 * 1;
|
|
4151
4151
|
const shortTermLength = 60 * 30;
|
|
4152
4152
|
function useShopifyCookies(options) {
|
|
4153
|
-
const {
|
|
4153
|
+
const {
|
|
4154
|
+
hasUserConsent = false,
|
|
4155
|
+
domain = "",
|
|
4156
|
+
checkoutDomain = ""
|
|
4157
|
+
} = options || {};
|
|
4154
4158
|
React$1.useEffect(() => {
|
|
4155
4159
|
const cookies = getShopifyCookies(document.cookie);
|
|
4156
4160
|
let currentDomain = domain || window.document.location.host;
|
|
4161
|
+
if (checkoutDomain) {
|
|
4162
|
+
const checkoutDomainParts = checkoutDomain.split(".").reverse();
|
|
4163
|
+
const currentDomainParts = currentDomain.split(".").reverse();
|
|
4164
|
+
const sameDomainParts = [];
|
|
4165
|
+
checkoutDomainParts.forEach((part, index2) => {
|
|
4166
|
+
if (part === currentDomainParts[index2]) {
|
|
4167
|
+
sameDomainParts.push(part);
|
|
4168
|
+
}
|
|
4169
|
+
});
|
|
4170
|
+
currentDomain = sameDomainParts.reverse().join(".");
|
|
4171
|
+
}
|
|
4157
4172
|
if (/^localhost/.test(currentDomain))
|
|
4158
4173
|
currentDomain = "";
|
|
4159
4174
|
const domainWithLeadingDot = currentDomain ? /^\./.test(currentDomain) ? currentDomain : `.${currentDomain}` : "";
|
|
@@ -4174,7 +4189,7 @@ Refer to the authentication https://shopify.dev/api/storefront#authentication do
|
|
|
4174
4189
|
setCookie(SHOPIFY_Y, "", 0, domainWithLeadingDot);
|
|
4175
4190
|
setCookie(SHOPIFY_S, "", 0, domainWithLeadingDot);
|
|
4176
4191
|
}
|
|
4177
|
-
}, [options, hasUserConsent, domain]);
|
|
4192
|
+
}, [options, hasUserConsent, domain, checkoutDomain]);
|
|
4178
4193
|
}
|
|
4179
4194
|
function setCookie(name, value, maxage, domain) {
|
|
4180
4195
|
document.cookie = l(name, value, {
|