@shopify/hydrogen-react 2025.5.0 → 2025.5.1

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.
Files changed (101) hide show
  1. package/dist/browser-dev/ShopifyProvider.mjs +18 -1
  2. package/dist/browser-dev/ShopifyProvider.mjs.map +1 -1
  3. package/dist/browser-dev/analytics.mjs +4 -5
  4. package/dist/browser-dev/analytics.mjs.map +1 -1
  5. package/dist/browser-dev/cart-hooks.mjs +25 -7
  6. package/dist/browser-dev/cart-hooks.mjs.map +1 -1
  7. package/dist/browser-dev/cookies-utils.mjs +4 -4
  8. package/dist/browser-dev/cookies-utils.mjs.map +1 -1
  9. package/dist/browser-dev/index.mjs +4 -0
  10. package/dist/browser-dev/index.mjs.map +1 -1
  11. package/dist/browser-dev/packages/hydrogen-react/package.json.mjs +1 -1
  12. package/dist/browser-dev/tracking-utils.mjs +92 -0
  13. package/dist/browser-dev/tracking-utils.mjs.map +1 -0
  14. package/dist/browser-dev/useShopifyCookies.mjs +96 -9
  15. package/dist/browser-dev/useShopifyCookies.mjs.map +1 -1
  16. package/dist/browser-prod/ShopifyProvider.mjs +18 -1
  17. package/dist/browser-prod/ShopifyProvider.mjs.map +1 -1
  18. package/dist/browser-prod/analytics.mjs +4 -5
  19. package/dist/browser-prod/analytics.mjs.map +1 -1
  20. package/dist/browser-prod/cart-hooks.mjs +25 -7
  21. package/dist/browser-prod/cart-hooks.mjs.map +1 -1
  22. package/dist/browser-prod/cookies-utils.mjs +4 -4
  23. package/dist/browser-prod/cookies-utils.mjs.map +1 -1
  24. package/dist/browser-prod/index.mjs +4 -0
  25. package/dist/browser-prod/index.mjs.map +1 -1
  26. package/dist/browser-prod/packages/hydrogen-react/package.json.mjs +1 -1
  27. package/dist/browser-prod/tracking-utils.mjs +92 -0
  28. package/dist/browser-prod/tracking-utils.mjs.map +1 -0
  29. package/dist/browser-prod/useShopifyCookies.mjs +96 -9
  30. package/dist/browser-prod/useShopifyCookies.mjs.map +1 -1
  31. package/dist/node-dev/ShopifyProvider.js +18 -1
  32. package/dist/node-dev/ShopifyProvider.js.map +1 -1
  33. package/dist/node-dev/ShopifyProvider.mjs +18 -1
  34. package/dist/node-dev/ShopifyProvider.mjs.map +1 -1
  35. package/dist/node-dev/analytics.js +4 -5
  36. package/dist/node-dev/analytics.js.map +1 -1
  37. package/dist/node-dev/analytics.mjs +4 -5
  38. package/dist/node-dev/analytics.mjs.map +1 -1
  39. package/dist/node-dev/cart-hooks.js +24 -6
  40. package/dist/node-dev/cart-hooks.js.map +1 -1
  41. package/dist/node-dev/cart-hooks.mjs +25 -7
  42. package/dist/node-dev/cart-hooks.mjs.map +1 -1
  43. package/dist/node-dev/cookies-utils.js +4 -4
  44. package/dist/node-dev/cookies-utils.js.map +1 -1
  45. package/dist/node-dev/cookies-utils.mjs +4 -4
  46. package/dist/node-dev/cookies-utils.mjs.map +1 -1
  47. package/dist/node-dev/index.js +4 -0
  48. package/dist/node-dev/index.js.map +1 -1
  49. package/dist/node-dev/index.mjs +4 -0
  50. package/dist/node-dev/index.mjs.map +1 -1
  51. package/dist/node-dev/packages/hydrogen-react/package.json.js +1 -1
  52. package/dist/node-dev/packages/hydrogen-react/package.json.mjs +1 -1
  53. package/dist/node-dev/tracking-utils.js +92 -0
  54. package/dist/node-dev/tracking-utils.js.map +1 -0
  55. package/dist/node-dev/tracking-utils.mjs +92 -0
  56. package/dist/node-dev/tracking-utils.mjs.map +1 -0
  57. package/dist/node-dev/useShopifyCookies.js +94 -7
  58. package/dist/node-dev/useShopifyCookies.js.map +1 -1
  59. package/dist/node-dev/useShopifyCookies.mjs +96 -9
  60. package/dist/node-dev/useShopifyCookies.mjs.map +1 -1
  61. package/dist/node-prod/ShopifyProvider.js +18 -1
  62. package/dist/node-prod/ShopifyProvider.js.map +1 -1
  63. package/dist/node-prod/ShopifyProvider.mjs +18 -1
  64. package/dist/node-prod/ShopifyProvider.mjs.map +1 -1
  65. package/dist/node-prod/analytics.js +4 -5
  66. package/dist/node-prod/analytics.js.map +1 -1
  67. package/dist/node-prod/analytics.mjs +4 -5
  68. package/dist/node-prod/analytics.mjs.map +1 -1
  69. package/dist/node-prod/cart-hooks.js +24 -6
  70. package/dist/node-prod/cart-hooks.js.map +1 -1
  71. package/dist/node-prod/cart-hooks.mjs +25 -7
  72. package/dist/node-prod/cart-hooks.mjs.map +1 -1
  73. package/dist/node-prod/cookies-utils.js +4 -4
  74. package/dist/node-prod/cookies-utils.js.map +1 -1
  75. package/dist/node-prod/cookies-utils.mjs +4 -4
  76. package/dist/node-prod/cookies-utils.mjs.map +1 -1
  77. package/dist/node-prod/index.js +4 -0
  78. package/dist/node-prod/index.js.map +1 -1
  79. package/dist/node-prod/index.mjs +4 -0
  80. package/dist/node-prod/index.mjs.map +1 -1
  81. package/dist/node-prod/packages/hydrogen-react/package.json.js +1 -1
  82. package/dist/node-prod/packages/hydrogen-react/package.json.mjs +1 -1
  83. package/dist/node-prod/tracking-utils.js +92 -0
  84. package/dist/node-prod/tracking-utils.js.map +1 -0
  85. package/dist/node-prod/tracking-utils.mjs +92 -0
  86. package/dist/node-prod/tracking-utils.mjs.map +1 -0
  87. package/dist/node-prod/useShopifyCookies.js +94 -7
  88. package/dist/node-prod/useShopifyCookies.js.map +1 -1
  89. package/dist/node-prod/useShopifyCookies.mjs +96 -9
  90. package/dist/node-prod/useShopifyCookies.mjs.map +1 -1
  91. package/dist/types/ShopifyProvider.d.ts +5 -0
  92. package/dist/types/cookies-utils.d.ts +4 -0
  93. package/dist/types/index.d.cts +1 -0
  94. package/dist/types/index.d.ts +1 -0
  95. package/dist/types/tracking-utils.d.ts +21 -0
  96. package/dist/types/useShopifyCookies.d.ts +28 -2
  97. package/dist/umd/hydrogen-react.dev.js +275 -89
  98. package/dist/umd/hydrogen-react.dev.js.map +1 -1
  99. package/dist/umd/hydrogen-react.prod.js +18 -18
  100. package/dist/umd/hydrogen-react.prod.js.map +1 -1
  101. package/package.json +1 -1
@@ -69,7 +69,7 @@
69
69
  return t2 === n2;
70
70
  };
71
71
  }
72
- function u$1(t2) {
72
+ function u(t2) {
73
73
  return "string" == typeof t2 ? { type: t2 } : t2;
74
74
  }
75
75
  function c(t2, n2) {
@@ -93,7 +93,7 @@
93
93
  var s2 = t(f(r(n2.states[n2.initial].entry).map(function(t2) {
94
94
  return o(t2, i2.actions);
95
95
  }), n2.context, e), 2), l2 = s2[0], v2 = s2[1], y = { config: n2, _options: i2, initialState: { value: n2.initial, actions: l2, context: v2, matches: a(n2.initial) }, transition: function(e2, i3) {
96
- var s3, l3, v3 = "string" == typeof e2 ? { value: e2, context: n2.context } : e2, p = v3.value, g2 = v3.context, d = u$1(i3), x = n2.states[p];
96
+ var s3, l3, v3 = "string" == typeof e2 ? { value: e2, context: n2.context } : e2, p = v3.value, g = v3.context, d = u(i3), x = n2.states[p];
97
97
  if (x.on) {
98
98
  var m = r(x.on[d.type]);
99
99
  try {
@@ -106,16 +106,16 @@
106
106
  throw new TypeError(n3 ? "Object is not iterable." : "Symbol.iterator is not defined.");
107
107
  }(m), b = h.next(); !b.done; b = h.next()) {
108
108
  var S = b.value;
109
- if (void 0 === S) return c(p, g2);
109
+ if (void 0 === S) return c(p, g);
110
110
  var w = "string" == typeof S ? { target: S } : S, j = w.target, E = w.actions, R = void 0 === E ? [] : E, N = w.cond, O = void 0 === N ? function() {
111
111
  return true;
112
112
  } : N, _ = void 0 === j, k = null != j ? j : p, T = n2.states[k];
113
- if (O(g2, d)) {
113
+ if (O(g, d)) {
114
114
  var q = t(f((_ ? r(R) : [].concat(x.exit, R, T.entry).filter(function(t2) {
115
115
  return t2;
116
116
  })).map(function(t2) {
117
117
  return o(t2, y._options.actions);
118
- }), g2, d), 3), z = q[0], A = q[1], B = q[2], C = null != j ? j : p;
118
+ }), g, d), 3), z = q[0], A = q[1], B = q[2], C = null != j ? j : p;
119
119
  return { value: C, context: A, actions: z, changed: j !== p || z.length > 0 || B, matches: a(C) };
120
120
  }
121
121
  }
@@ -129,7 +129,7 @@
129
129
  }
130
130
  }
131
131
  }
132
- return c(p, g2);
132
+ return c(p, g);
133
133
  } };
134
134
  return y;
135
135
  }
@@ -141,7 +141,7 @@
141
141
  };
142
142
  function v(t2) {
143
143
  var r2 = t2.initialState, i2 = n.NotStarted, o2 = /* @__PURE__ */ new Set(), c2 = { _machine: t2, send: function(e2) {
144
- i2 === n.Running && (r2 = t2.transition(r2, e2), l$1(r2, u$1(e2)), o2.forEach(function(t3) {
144
+ i2 === n.Running && (r2 = t2.transition(r2, e2), l$1(r2, u(e2)), o2.forEach(function(t3) {
145
145
  return t3(r2);
146
146
  }));
147
147
  }, subscribe: function(t3) {
@@ -929,6 +929,21 @@ Refer to the authentication https://shopify.dev/api/storefront#authentication do
929
929
  const ShopifyContext = React$1.createContext(
930
930
  defaultShopifyContext
931
931
  );
932
+ function isSfapiProxyEnabled() {
933
+ var _a, _b, _c;
934
+ if (typeof window === "undefined") return false;
935
+ try {
936
+ const navigationEntry = (_b = (_a = window.performance) == null ? void 0 : _a.getEntriesByType) == null ? void 0 : _b.call(
937
+ _a,
938
+ "navigation"
939
+ )[0];
940
+ return !!((_c = navigationEntry == null ? void 0 : navigationEntry.serverTiming) == null ? void 0 : _c.some(
941
+ (entry) => entry.name === "_sfapi_proxy"
942
+ ));
943
+ } catch (e2) {
944
+ return false;
945
+ }
946
+ }
932
947
  function ShopifyProvider({
933
948
  children,
934
949
  ...shopifyConfig
@@ -944,12 +959,14 @@ Refer to the authentication https://shopify.dev/api/storefront#authentication do
944
959
  );
945
960
  }
946
961
  const finalConfig = React$1.useMemo(() => {
962
+ const sameDomainForStorefrontApi = shopifyConfig.sameDomainForStorefrontApi ?? isSfapiProxyEnabled();
947
963
  function getShopifyDomain(overrideProps) {
948
964
  const domain = (overrideProps == null ? void 0 : overrideProps.storeDomain) ?? shopifyConfig.storeDomain;
949
965
  return domain.includes("://") ? domain : `https://${domain}`;
950
966
  }
951
967
  return {
952
968
  ...shopifyConfig,
969
+ sameDomainForStorefrontApi,
953
970
  getPublicTokenHeaders(overrideProps) {
954
971
  return getPublicTokenHeadersRaw(
955
972
  overrideProps.contentType,
@@ -959,7 +976,7 @@ Refer to the authentication https://shopify.dev/api/storefront#authentication do
959
976
  },
960
977
  getShopifyDomain,
961
978
  getStorefrontApiUrl(overrideProps) {
962
- const finalDomainUrl = getShopifyDomain({
979
+ const finalDomainUrl = sameDomainForStorefrontApi && typeof window !== "undefined" ? window.location.origin : getShopifyDomain({
963
980
  storeDomain: (overrideProps == null ? void 0 : overrideProps.storeDomain) ?? shopifyConfig.storeDomain
964
981
  });
965
982
  return `${finalDomainUrl}${finalDomainUrl.endsWith("/") ? "" : "/"}api/${(overrideProps == null ? void 0 : overrideProps.storefrontApiVersion) ?? shopifyConfig.storefrontApiVersion}/graphql.json`;
@@ -981,77 +998,98 @@ Refer to the authentication https://shopify.dev/api/storefront#authentication do
981
998
  const SHOPIFY_STOREFRONT_S_HEADER = "Shopify-Storefront-S";
982
999
  const SHOPIFY_Y = "_shopify_y";
983
1000
  const SHOPIFY_S = "_shopify_s";
984
- var g = /* @__PURE__ */ new Set([
985
- "domain",
986
- "path",
987
- "max-age",
988
- "expires",
989
- "samesite",
990
- "secure",
991
- "httponly"
992
- ]);
993
- function u(a2) {
994
- let r2 = {}, e2, t2, n2 = 0, m = a2.split(/;\s*/g), s2, i2;
995
- for (; n2 < m.length; n2++)
996
- if (t2 = m[n2], e2 = t2.indexOf("="), ~e2) {
997
- if (s2 = t2.substring(0, e2++).trim(), i2 = t2.substring(e2).trim(), i2[0] === '"' && (i2 = i2.substring(1, i2.length - 1)), ~i2.indexOf("%"))
998
- try {
999
- i2 = decodeURIComponent(i2);
1000
- } catch (f2) {
1001
+ const SHOPIFY_VISIT_TOKEN_HEADER = "X-Shopify-VisitToken";
1002
+ const SHOPIFY_UNIQUE_TOKEN_HEADER = "X-Shopify-UniqueToken";
1003
+ const cachedTrackingValues = { current: null };
1004
+ function getTrackingValues() {
1005
+ var _a, _b, _c;
1006
+ let trackingValues;
1007
+ if (typeof window !== "undefined" && typeof window.performance !== "undefined") {
1008
+ try {
1009
+ const resourceRE = /^https?:\/\/([^/]+)(\/api\/(?:unstable|2\d{3}-\d{2})\/graphql\.json(?=$|\?))?/;
1010
+ const entries = performance.getEntriesByType(
1011
+ "resource"
1012
+ );
1013
+ let matchedValues;
1014
+ for (let i2 = entries.length - 1; i2 >= 0; i2--) {
1015
+ const entry = entries[i2];
1016
+ if (entry.initiatorType !== "fetch") continue;
1017
+ const currentHost = window.location.host;
1018
+ const match = entry.name.match(resourceRE);
1019
+ if (!match) continue;
1020
+ const [, matchedHost, sfapiPath] = match;
1021
+ const isMatch = (
1022
+ // Same origin (exact host match)
1023
+ matchedHost === currentHost || // Subdomain with SFAPI path
1024
+ sfapiPath && (matchedHost == null ? void 0 : matchedHost.endsWith(`.${currentHost}`))
1025
+ );
1026
+ if (isMatch) {
1027
+ const values = extractFromPerformanceEntry(entry);
1028
+ if (values) {
1029
+ matchedValues = values;
1030
+ break;
1031
+ }
1001
1032
  }
1002
- g.has(t2 = s2.toLowerCase()) ? t2 === "expires" ? r2.expires = new Date(i2) : t2 === "max-age" ? r2.maxage = +i2 : r2[t2] = i2 : r2[s2] = i2;
1003
- } else
1004
- (s2 = t2.trim().toLowerCase()) && (s2 === "httponly" || s2 === "secure") && (r2[s2] = true);
1005
- return r2;
1006
- }
1007
- function l(a2, r2, e2 = {}) {
1008
- let t2 = a2 + "=" + encodeURIComponent(r2);
1009
- return e2.expires && (t2 += "; Expires=" + new Date(e2.expires).toUTCString()), e2.maxage != null && e2.maxage >= 0 && (t2 += "; Max-Age=" + (e2.maxage | 0)), e2.domain && (t2 += "; Domain=" + e2.domain), e2.path && (t2 += "; Path=" + e2.path), e2.samesite && (t2 += "; SameSite=" + e2.samesite), (e2.secure || e2.samesite === "None") && (t2 += "; Secure"), e2.httponly && (t2 += "; HttpOnly"), t2;
1010
- }
1011
- const tokenHash = "xxxx-4xxx-xxxx-xxxxxxxxxxxx";
1012
- function buildUUID() {
1013
- let hash = "";
1014
- try {
1015
- const crypto = window.crypto;
1016
- const randomValuesArray = new Uint16Array(31);
1017
- crypto.getRandomValues(randomValuesArray);
1018
- let i2 = 0;
1019
- hash = tokenHash.replace(/[x]/g, (c2) => {
1020
- const r2 = randomValuesArray[i2] % 16;
1021
- const v2 = c2 === "x" ? r2 : r2 & 3 | 8;
1022
- i2++;
1023
- return v2.toString(16);
1024
- }).toUpperCase();
1025
- } catch (err) {
1026
- hash = tokenHash.replace(/[x]/g, (c2) => {
1027
- const r2 = Math.random() * 16 | 0;
1028
- const v2 = c2 === "x" ? r2 : r2 & 3 | 8;
1029
- return v2.toString(16);
1030
- }).toUpperCase();
1033
+ }
1034
+ if (matchedValues) {
1035
+ trackingValues = matchedValues;
1036
+ }
1037
+ if (trackingValues) {
1038
+ cachedTrackingValues.current = trackingValues;
1039
+ } else if (cachedTrackingValues.current) {
1040
+ trackingValues = cachedTrackingValues.current;
1041
+ }
1042
+ if (!trackingValues) {
1043
+ const navigationEntries = performance.getEntriesByType(
1044
+ "navigation"
1045
+ )[0];
1046
+ trackingValues = extractFromPerformanceEntry(navigationEntries, false);
1047
+ }
1048
+ } catch {
1049
+ }
1031
1050
  }
1032
- return `${hexTime()}-${hash}`;
1033
- }
1034
- function hexTime() {
1035
- let dateNumber = 0;
1036
- let perfNumber = 0;
1037
- dateNumber = (/* @__PURE__ */ new Date()).getTime() >>> 0;
1038
- try {
1039
- perfNumber = performance.now() >>> 0;
1040
- } catch (err) {
1041
- perfNumber = 0;
1051
+ if (!trackingValues) {
1052
+ const cookie = (
1053
+ // Read from arguments to avoid declaring parameters in this function signature.
1054
+ // This logic is only used internally from `getShopifyCookies` and will be deprecated.
1055
+ typeof arguments[0] === "string" ? arguments[0] : typeof document !== "undefined" ? document.cookie : ""
1056
+ );
1057
+ trackingValues = {
1058
+ uniqueToken: ((_a = cookie.match(/\b_shopify_y=([^;]+)/)) == null ? void 0 : _a[1]) || "",
1059
+ visitToken: ((_b = cookie.match(/\b_shopify_s=([^;]+)/)) == null ? void 0 : _b[1]) || "",
1060
+ consent: ((_c = cookie.match(/\b_tracking_consent=([^;]+)/)) == null ? void 0 : _c[1]) || ""
1061
+ };
1042
1062
  }
1043
- const output = Math.abs(dateNumber + perfNumber).toString(16).toLowerCase();
1044
- return output.padStart(8, "0");
1045
- }
1046
- function getShopifyCookies(cookies) {
1047
- const cookieData = u(cookies);
1048
- return {
1049
- [SHOPIFY_Y]: cookieData[SHOPIFY_Y] || "",
1050
- [SHOPIFY_S]: cookieData[SHOPIFY_S] || ""
1051
- };
1063
+ return trackingValues;
1064
+ }
1065
+ function extractFromPerformanceEntry(entry, isConsentRequired = true) {
1066
+ let uniqueToken = "";
1067
+ let visitToken = "";
1068
+ let consent = "";
1069
+ const serverTiming = entry.serverTiming;
1070
+ if (serverTiming && serverTiming.length >= 3) {
1071
+ for (let i2 = serverTiming.length - 1; i2 >= 0; i2--) {
1072
+ const { name, description } = serverTiming[i2];
1073
+ if (!name || !description) continue;
1074
+ if (name === "_y") {
1075
+ uniqueToken = description;
1076
+ } else if (name === "_s") {
1077
+ visitToken = description;
1078
+ } else if (name === "_cmp") {
1079
+ consent = description;
1080
+ }
1081
+ if (uniqueToken && visitToken && consent) break;
1082
+ }
1083
+ }
1084
+ return uniqueToken && visitToken && (isConsentRequired ? consent : true) ? { uniqueToken, visitToken, consent } : void 0;
1052
1085
  }
1053
1086
  function useCartFetch() {
1054
- const { storefrontId, getPublicTokenHeaders, getStorefrontApiUrl } = useShop();
1087
+ const {
1088
+ storefrontId,
1089
+ getPublicTokenHeaders,
1090
+ getStorefrontApiUrl,
1091
+ sameDomainForStorefrontApi
1092
+ } = useShop();
1055
1093
  return React$1.useCallback(
1056
1094
  ({
1057
1095
  query,
@@ -1061,9 +1099,17 @@ Refer to the authentication https://shopify.dev/api/storefront#authentication do
1061
1099
  if (storefrontId) {
1062
1100
  headers[SHOPIFY_STOREFRONT_ID_HEADER] = storefrontId;
1063
1101
  }
1064
- const cookieData = getShopifyCookies(document.cookie);
1065
- headers[SHOPIFY_STOREFRONT_Y_HEADER] = cookieData[SHOPIFY_Y];
1066
- headers[SHOPIFY_STOREFRONT_S_HEADER] = cookieData[SHOPIFY_S];
1102
+ if (!sameDomainForStorefrontApi) {
1103
+ const { uniqueToken, visitToken } = getTrackingValues();
1104
+ if (uniqueToken) {
1105
+ headers[SHOPIFY_STOREFRONT_Y_HEADER] = uniqueToken;
1106
+ headers[SHOPIFY_UNIQUE_TOKEN_HEADER] = uniqueToken;
1107
+ }
1108
+ if (visitToken) {
1109
+ headers[SHOPIFY_STOREFRONT_S_HEADER] = visitToken;
1110
+ headers[SHOPIFY_VISIT_TOKEN_HEADER] = visitToken;
1111
+ }
1112
+ }
1067
1113
  return fetch(getStorefrontApiUrl(), {
1068
1114
  method: "POST",
1069
1115
  headers,
@@ -1081,7 +1127,12 @@ Refer to the authentication https://shopify.dev/api/storefront#authentication do
1081
1127
  };
1082
1128
  });
1083
1129
  },
1084
- [getPublicTokenHeaders, storefrontId, getStorefrontApiUrl]
1130
+ [
1131
+ getPublicTokenHeaders,
1132
+ storefrontId,
1133
+ getStorefrontApiUrl,
1134
+ sameDomainForStorefrontApi
1135
+ ]
1085
1136
  );
1086
1137
  }
1087
1138
  function useCartActions({
@@ -2358,6 +2409,48 @@ Refer to the authentication https://shopify.dev/api/storefront#authentication do
2358
2409
  }
2359
2410
  return false;
2360
2411
  }
2412
+ const tokenHash = "xxxx-4xxx-xxxx-xxxxxxxxxxxx";
2413
+ function buildUUID() {
2414
+ let hash = "";
2415
+ try {
2416
+ const crypto = window.crypto;
2417
+ const randomValuesArray = new Uint16Array(31);
2418
+ crypto.getRandomValues(randomValuesArray);
2419
+ let i2 = 0;
2420
+ hash = tokenHash.replace(/[x]/g, (c2) => {
2421
+ const r2 = randomValuesArray[i2] % 16;
2422
+ const v2 = c2 === "x" ? r2 : r2 & 3 | 8;
2423
+ i2++;
2424
+ return v2.toString(16);
2425
+ }).toUpperCase();
2426
+ } catch (err) {
2427
+ hash = tokenHash.replace(/[x]/g, (c2) => {
2428
+ const r2 = Math.random() * 16 | 0;
2429
+ const v2 = c2 === "x" ? r2 : r2 & 3 | 8;
2430
+ return v2.toString(16);
2431
+ }).toUpperCase();
2432
+ }
2433
+ return `${hexTime()}-${hash}`;
2434
+ }
2435
+ function hexTime() {
2436
+ let dateNumber = 0;
2437
+ let perfNumber = 0;
2438
+ dateNumber = (/* @__PURE__ */ new Date()).getTime() >>> 0;
2439
+ try {
2440
+ perfNumber = performance.now() >>> 0;
2441
+ } catch (err) {
2442
+ perfNumber = 0;
2443
+ }
2444
+ const output = Math.abs(dateNumber + perfNumber).toString(16).toLowerCase();
2445
+ return output.padStart(8, "0");
2446
+ }
2447
+ function getShopifyCookies(cookies) {
2448
+ const trackingValues = getTrackingValues(cookies);
2449
+ return {
2450
+ [SHOPIFY_Y]: trackingValues.uniqueToken,
2451
+ [SHOPIFY_S]: trackingValues.visitToken
2452
+ };
2453
+ }
2361
2454
  const SCHEMA_ID$1 = "trekkie_storefront_page_view/1.4";
2362
2455
  const OXYGEN_DOMAIN = "myshopify.dev";
2363
2456
  function pageView$1(payload) {
@@ -2409,7 +2502,7 @@ Refer to the authentication https://shopify.dev/api/storefront#authentication do
2409
2502
  }
2410
2503
  return false;
2411
2504
  }
2412
- const version = "2025.5.0";
2505
+ const version = "2025.5.1";
2413
2506
  const SCHEMA_ID = "custom_storefront_customer_tracking/1.2";
2414
2507
  const PAGE_RENDERED_EVENT_NAME = "page_rendered";
2415
2508
  const COLLECTION_PAGE_RENDERED_EVENT_NAME = "collection_page_rendered";
@@ -2727,10 +2820,10 @@ Refer to the authentication https://shopify.dev/api/storefront#authentication do
2727
2820
  };
2728
2821
  }
2729
2822
  const [navigationType, navigationApi] = getNavigationType();
2730
- const cookies = getShopifyCookies(document.cookie);
2823
+ const trackingValues = getTrackingValues();
2731
2824
  return {
2732
- uniqueToken: cookies[SHOPIFY_Y],
2733
- visitToken: cookies[SHOPIFY_S],
2825
+ uniqueToken: trackingValues.uniqueToken,
2826
+ visitToken: trackingValues.visitToken,
2734
2827
  url: location.href,
2735
2828
  path: location.pathname,
2736
2829
  search: location.search,
@@ -4557,17 +4650,29 @@ Refer to the authentication https://shopify.dev/api/storefront#authentication do
4557
4650
  }, [JSON.stringify(selectedOptions)]);
4558
4651
  return null;
4559
4652
  }
4653
+ function l(a2, r2, e2 = {}) {
4654
+ let t2 = a2 + "=" + encodeURIComponent(r2);
4655
+ return e2.expires && (t2 += "; Expires=" + new Date(e2.expires).toUTCString()), e2.maxage != null && e2.maxage >= 0 && (t2 += "; Max-Age=" + (e2.maxage | 0)), e2.domain && (t2 += "; Domain=" + e2.domain), e2.path && (t2 += "; Path=" + e2.path), e2.samesite && (t2 += "; SameSite=" + e2.samesite), (e2.secure || e2.samesite === "None") && (t2 += "; Secure"), e2.httponly && (t2 += "; HttpOnly"), t2;
4656
+ }
4560
4657
  const longTermLength = 60 * 60 * 24 * 360 * 1;
4561
4658
  const shortTermLength = 60 * 30;
4562
4659
  function useShopifyCookies(options) {
4563
4660
  const {
4564
- hasUserConsent = false,
4661
+ hasUserConsent,
4565
4662
  domain = "",
4566
- checkoutDomain = ""
4663
+ checkoutDomain = "",
4664
+ storefrontAccessToken,
4665
+ fetchTrackingValues,
4666
+ ignoreDeprecatedCookies = false
4567
4667
  } = options || {};
4668
+ const coreCookiesReady = useCoreShopifyCookies({
4669
+ storefrontAccessToken,
4670
+ fetchTrackingValues,
4671
+ checkoutDomain
4672
+ });
4568
4673
  React$1.useEffect(() => {
4569
- const cookies = getShopifyCookies(document.cookie);
4570
- let currentDomain = domain || window.document.location.host;
4674
+ if (ignoreDeprecatedCookies || !coreCookiesReady) return;
4675
+ let currentDomain = domain || window.location.host;
4571
4676
  if (checkoutDomain) {
4572
4677
  const checkoutDomainParts = checkoutDomain.split(".").reverse();
4573
4678
  const currentDomainParts = currentDomain.split(".").reverse();
@@ -4582,15 +4687,19 @@ Refer to the authentication https://shopify.dev/api/storefront#authentication do
4582
4687
  if (/^localhost/.test(currentDomain)) currentDomain = "";
4583
4688
  const domainWithLeadingDot = currentDomain ? /^\./.test(currentDomain) ? currentDomain : `.${currentDomain}` : "";
4584
4689
  if (hasUserConsent) {
4690
+ const trackingValues = getTrackingValues();
4691
+ if ((trackingValues.uniqueToken || trackingValues.visitToken || "").startsWith("00000000-")) {
4692
+ return;
4693
+ }
4585
4694
  setCookie(
4586
4695
  SHOPIFY_Y,
4587
- cookies[SHOPIFY_Y] || buildUUID(),
4696
+ trackingValues.uniqueToken || buildUUID(),
4588
4697
  longTermLength,
4589
4698
  domainWithLeadingDot
4590
4699
  );
4591
4700
  setCookie(
4592
4701
  SHOPIFY_S,
4593
- cookies[SHOPIFY_S] || buildUUID(),
4702
+ trackingValues.visitToken || buildUUID(),
4594
4703
  shortTermLength,
4595
4704
  domainWithLeadingDot
4596
4705
  );
@@ -4598,7 +4707,14 @@ Refer to the authentication https://shopify.dev/api/storefront#authentication do
4598
4707
  setCookie(SHOPIFY_Y, "", 0, domainWithLeadingDot);
4599
4708
  setCookie(SHOPIFY_S, "", 0, domainWithLeadingDot);
4600
4709
  }
4601
- }, [options, hasUserConsent, domain, checkoutDomain]);
4710
+ }, [
4711
+ coreCookiesReady,
4712
+ hasUserConsent,
4713
+ domain,
4714
+ checkoutDomain,
4715
+ ignoreDeprecatedCookies
4716
+ ]);
4717
+ return coreCookiesReady;
4602
4718
  }
4603
4719
  function setCookie(name, value, maxage, domain) {
4604
4720
  document.cookie = l(name, value, {
@@ -4608,6 +4724,73 @@ Refer to the authentication https://shopify.dev/api/storefront#authentication do
4608
4724
  path: "/"
4609
4725
  });
4610
4726
  }
4727
+ async function fetchTrackingValuesFromBrowser(storefrontAccessToken, storefrontApiDomain = "") {
4728
+ const { uniqueToken, visitToken } = getTrackingValues();
4729
+ const response = await fetch(
4730
+ // TODO: update this endpoint when it becomes stable
4731
+ `${storefrontApiDomain.replace(/\/+$/, "")}/api/unstable/graphql.json`,
4732
+ {
4733
+ method: "POST",
4734
+ headers: {
4735
+ "Content-Type": "application/json",
4736
+ ...storefrontAccessToken && {
4737
+ "X-Shopify-Storefront-Access-Token": storefrontAccessToken
4738
+ },
4739
+ ...visitToken || uniqueToken ? {
4740
+ [SHOPIFY_VISIT_TOKEN_HEADER]: visitToken,
4741
+ [SHOPIFY_UNIQUE_TOKEN_HEADER]: uniqueToken
4742
+ } : void 0
4743
+ },
4744
+ body: JSON.stringify({
4745
+ query: (
4746
+ // This query ensures we get _cmp (consent) server-timing header, which is not available in other queries.
4747
+ // This value can be passed later to consent-tracking-api and privacy-banner scripts to avoid extra requests.
4748
+ "query ensureCookies { consentManagement { cookies(visitorConsent:{}) { cookieDomain } } }"
4749
+ )
4750
+ })
4751
+ }
4752
+ );
4753
+ if (!response.ok) {
4754
+ throw new Error(
4755
+ `Failed to fetch consent from browser: ${response.status} ${response.statusText}`
4756
+ );
4757
+ }
4758
+ await response.json();
4759
+ getTrackingValues();
4760
+ }
4761
+ function useCoreShopifyCookies({
4762
+ checkoutDomain,
4763
+ storefrontAccessToken,
4764
+ fetchTrackingValues = false
4765
+ }) {
4766
+ const [cookiesReady, setCookiesReady] = React$1.useState(!fetchTrackingValues);
4767
+ const hasFetchedTrackingValues = React$1.useRef(false);
4768
+ React$1.useEffect(() => {
4769
+ if (!fetchTrackingValues) {
4770
+ setCookiesReady(true);
4771
+ return;
4772
+ }
4773
+ if (hasFetchedTrackingValues.current) return;
4774
+ hasFetchedTrackingValues.current = true;
4775
+ fetchTrackingValuesFromBrowser(storefrontAccessToken).catch(
4776
+ (error) => checkoutDomain ? (
4777
+ // Retry with checkout domain if available to at least
4778
+ // get the server-timing values for tracking.
4779
+ fetchTrackingValuesFromBrowser(
4780
+ storefrontAccessToken,
4781
+ checkoutDomain
4782
+ )
4783
+ ) : Promise.reject(error)
4784
+ ).catch((error) => {
4785
+ console.warn(
4786
+ "[h2:warn:useShopifyCookies] Failed to fetch tracking values from browser: " + (error instanceof Error ? error.message : String(error))
4787
+ );
4788
+ }).finally(() => {
4789
+ setCookiesReady(true);
4790
+ });
4791
+ }, [checkoutDomain, fetchTrackingValues, storefrontAccessToken]);
4792
+ return cookiesReady;
4793
+ }
4611
4794
  exports2.AddToCartButton = AddToCartButton;
4612
4795
  exports2.AnalyticsEventName = AnalyticsEventName;
4613
4796
  exports2.AnalyticsPageType = AnalyticsPageType;
@@ -4631,6 +4814,8 @@ Refer to the authentication https://shopify.dev/api/storefront#authentication do
4631
4814
  exports2.SHOPIFY_STOREFRONT_ID_HEADER = SHOPIFY_STOREFRONT_ID_HEADER;
4632
4815
  exports2.SHOPIFY_STOREFRONT_S_HEADER = SHOPIFY_STOREFRONT_S_HEADER;
4633
4816
  exports2.SHOPIFY_STOREFRONT_Y_HEADER = SHOPIFY_STOREFRONT_Y_HEADER;
4817
+ exports2.SHOPIFY_UNIQUE_TOKEN_HEADER = SHOPIFY_UNIQUE_TOKEN_HEADER;
4818
+ exports2.SHOPIFY_VISIT_TOKEN_HEADER = SHOPIFY_VISIT_TOKEN_HEADER;
4634
4819
  exports2.SHOPIFY_Y = SHOPIFY_Y;
4635
4820
  exports2.ShopPayButton = ShopPayButton;
4636
4821
  exports2.ShopifyProvider = ShopifyProvider;
@@ -4644,6 +4829,7 @@ Refer to the authentication https://shopify.dev/api/storefront#authentication do
4644
4829
  exports2.getClientBrowserParameters = getClientBrowserParameters;
4645
4830
  exports2.getProductOptions = getProductOptions;
4646
4831
  exports2.getShopifyCookies = getShopifyCookies;
4832
+ exports2.getTrackingValues = getTrackingValues;
4647
4833
  exports2.isOptionValueCombinationInEncodedVariant = isOptionValueCombinationInEncodedVariant;
4648
4834
  exports2.mapSelectedProductOptionToObject = mapSelectedProductOptionToObject;
4649
4835
  exports2.parseGid = parseGid;