@shopify/hydrogen 2024.7.2 → 2024.7.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.
@@ -511,7 +511,7 @@ var errorOnce = (string) => {
511
511
  };
512
512
 
513
513
  // src/version.ts
514
- var LIB_VERSION = "2024.7.2";
514
+ var LIB_VERSION = "2024.7.4";
515
515
 
516
516
  // src/utils/graphql.ts
517
517
  function minifyQuery(string) {
@@ -900,7 +900,10 @@ function formatAPIResult(data, errors2) {
900
900
 
901
901
  // src/utils/request.ts
902
902
  function getHeader(request, key) {
903
- const value = request.headers?.get?.(key) ?? request.headers?.[key];
903
+ return getHeaderValue(request.headers, key);
904
+ }
905
+ function getHeaderValue(headers, key) {
906
+ const value = headers?.get?.(key) ?? headers?.[key];
904
907
  return typeof value === "string" ? value : null;
905
908
  }
906
909
  function getDebugHeaders(request) {
@@ -911,11 +914,8 @@ function getDebugHeaders(request) {
911
914
  }
912
915
 
913
916
  // src/cache/create-with-cache.ts
914
- function createWithCache({
915
- cache,
916
- waitUntil,
917
- request
918
- }) {
917
+ function createWithCache(cacheOptions) {
918
+ const { cache, waitUntil, request } = cacheOptions;
919
919
  return function withCache(cacheKey, strategy, actionFn) {
920
920
  return runWithCache(cacheKey, actionFn, {
921
921
  strategy,
@@ -1815,7 +1815,7 @@ function Seo({ debug }) {
1815
1815
  const matches = react$1.useMatches();
1816
1816
  const location = react$1.useLocation();
1817
1817
  console.warn(
1818
- "[h2:warn:Seo] The `<Seo/>` component is deprecated. Use `getSeoMeta` instead.\nSee: https://shopify.dev/docs/api/hydrogen/2024-01/utilities/getseometa"
1818
+ "[h2:warn:Seo] The `<Seo/>` component is deprecated. Use `getSeoMeta` instead.\nSee: https://shopify.dev/docs/api/hydrogen/2024-07/utilities/getseometa"
1819
1819
  );
1820
1820
  const seoConfig = react.useMemo(() => {
1821
1821
  return matches.flatMap((match) => {
@@ -2574,6 +2574,11 @@ function createCustomerAccountClient({
2574
2574
  `[h2:warn:createCustomerAccountClient] You are using Customer Account API version ${customerApiVersion} when this version of Hydrogen was built for ${DEFAULT_CUSTOMER_API_VERSION}.`
2575
2575
  );
2576
2576
  }
2577
+ if (!session) {
2578
+ console.warn(
2579
+ `[h2:warn:createCustomerAccountClient] session is required to use Customer Account API. Ensure the session object passed in exist.`
2580
+ );
2581
+ }
2577
2582
  if (!request?.url) {
2578
2583
  throw new Error(
2579
2584
  "[h2:error:createCustomerAccountClient] The request object does not contain a URL."
@@ -3569,7 +3574,7 @@ var CART_METAFIELD_DELETE_MUTATION = () => `#graphql
3569
3574
  }
3570
3575
  `;
3571
3576
  var cartGetIdDefault = (requestHeaders) => {
3572
- const cookies = cookie.parse(requestHeaders.get("Cookie") || "");
3577
+ const cookies = cookie.parse(getHeaderValue(requestHeaders, "Cookie") || "");
3573
3578
  return () => {
3574
3579
  return cookies.cart ? `gid://shopify/Cart/${cookies.cart}` : void 0;
3575
3580
  };
@@ -3988,10 +3993,34 @@ function addCspDirective(currentValue, value) {
3988
3993
  }
3989
3994
  var Script = react.forwardRef(
3990
3995
  (props, ref) => {
3996
+ const { waitForHydration, src, ...rest } = props;
3997
+ if (waitForHydration) return /* @__PURE__ */ jsxRuntime.jsx(LazyScript, { src, options: rest });
3991
3998
  const nonce = useNonce();
3992
- return /* @__PURE__ */ jsxRuntime.jsx("script", { suppressHydrationWarning: true, ...props, nonce, ref });
3999
+ return /* @__PURE__ */ jsxRuntime.jsx(
4000
+ "script",
4001
+ {
4002
+ suppressHydrationWarning: true,
4003
+ ...rest,
4004
+ src,
4005
+ nonce,
4006
+ ref
4007
+ }
4008
+ );
3993
4009
  }
3994
4010
  );
4011
+ function LazyScript({
4012
+ src,
4013
+ options
4014
+ }) {
4015
+ if (!src)
4016
+ throw new Error(
4017
+ "`waitForHydration` with the Script component requires a `src` prop"
4018
+ );
4019
+ hydrogenReact.useLoadScript(src, {
4020
+ attributes: options
4021
+ });
4022
+ return null;
4023
+ }
3995
4024
  function useOptimisticData(identifier) {
3996
4025
  const fetchers = react$1.useFetchers();
3997
4026
  const data = {};
@@ -4205,6 +4234,7 @@ function ShopifyAnalytics({
4205
4234
  const { subscribe: subscribe2, register: register2, canTrack } = useAnalytics();
4206
4235
  const [shopifyReady, setShopifyReady] = react.useState(false);
4207
4236
  const [privacyReady, setPrivacyReady] = react.useState(false);
4237
+ const init = react.useRef(false);
4208
4238
  const { ready: shopifyAnalyticsReady } = register2("Internal_Shopify_Analytics");
4209
4239
  const { ready: customerPrivacyReady } = register2(
4210
4240
  "Internal_Shopify_CustomerPrivacy"
@@ -4233,6 +4263,8 @@ function ShopifyAnalytics({
4233
4263
  checkoutDomain
4234
4264
  });
4235
4265
  react.useEffect(() => {
4266
+ if (init.current) return;
4267
+ init.current = true;
4236
4268
  subscribe2(AnalyticsEvent.PAGE_VIEWED, pageViewHandler);
4237
4269
  subscribe2(AnalyticsEvent.PRODUCT_VIEWED, productViewHandler);
4238
4270
  subscribe2(AnalyticsEvent.COLLECTION_VIEWED, collectionViewHandler);
@@ -4686,7 +4718,6 @@ function AnalyticsProvider({
4686
4718
  consent,
4687
4719
  customData = {},
4688
4720
  shop: shopProp = null,
4689
- disableThrowOnError = false,
4690
4721
  cookieDomain
4691
4722
  }) {
4692
4723
  const listenerSet = react.useRef(false);
@@ -4847,6 +4878,84 @@ var RichText = function(props) {
4847
4878
  }
4848
4879
  );
4849
4880
  };
4881
+
4882
+ // src/createHydrogenContext.ts
4883
+ function createHydrogenContext(options) {
4884
+ const {
4885
+ env,
4886
+ request,
4887
+ cache,
4888
+ waitUntil,
4889
+ i18n,
4890
+ session,
4891
+ logErrors,
4892
+ storefront: storefrontOptions = {},
4893
+ customerAccount: customerAccountOptions,
4894
+ cart: cartOptions = {}
4895
+ } = options;
4896
+ if (!session) {
4897
+ console.warn(
4898
+ `[h2:warn:createHydrogenContext] A session object is required to create hydrogen context.`
4899
+ );
4900
+ }
4901
+ const { storefront } = createStorefrontClient({
4902
+ // share options
4903
+ cache,
4904
+ waitUntil,
4905
+ i18n,
4906
+ logErrors,
4907
+ // storefrontOptions
4908
+ storefrontHeaders: storefrontOptions.headers || getStorefrontHeaders(request),
4909
+ storefrontApiVersion: storefrontOptions.apiVersion,
4910
+ // defaults
4911
+ storefrontId: env.PUBLIC_STOREFRONT_ID,
4912
+ storeDomain: env.PUBLIC_STORE_DOMAIN,
4913
+ privateStorefrontToken: env.PRIVATE_STOREFRONT_API_TOKEN,
4914
+ publicStorefrontToken: env.PUBLIC_STOREFRONT_API_TOKEN
4915
+ });
4916
+ const customerAccount = createCustomerAccountClient({
4917
+ // share options
4918
+ session,
4919
+ request,
4920
+ waitUntil,
4921
+ logErrors,
4922
+ // customerAccountOptions
4923
+ customerApiVersion: customerAccountOptions?.apiVersion,
4924
+ authUrl: customerAccountOptions?.authUrl,
4925
+ customAuthStatusHandler: customerAccountOptions?.customAuthStatusHandler,
4926
+ unstableB2b: customerAccountOptions?.unstableB2b,
4927
+ // defaults
4928
+ customerAccountId: env.PUBLIC_CUSTOMER_ACCOUNT_API_CLIENT_ID,
4929
+ customerAccountUrl: env.PUBLIC_CUSTOMER_ACCOUNT_API_URL
4930
+ });
4931
+ const cart = createCartHandler({
4932
+ // cartOptions
4933
+ getCartId: cartOptions.getId || cartGetIdDefault(request.headers),
4934
+ setCartId: cartOptions.setId || cartSetIdDefault(),
4935
+ cartQueryFragment: cartOptions.queryFragment,
4936
+ cartMutateFragment: cartOptions.mutateFragment,
4937
+ customMethods: cartOptions.customMethods,
4938
+ // defaults
4939
+ storefront,
4940
+ customerAccount
4941
+ });
4942
+ return {
4943
+ storefront,
4944
+ customerAccount,
4945
+ cart,
4946
+ env,
4947
+ waitUntil,
4948
+ session
4949
+ };
4950
+ }
4951
+ function getStorefrontHeaders(request) {
4952
+ return {
4953
+ requestGroupId: getHeader(request, "request-id"),
4954
+ buyerIp: getHeader(request, "oxygen-buyer-ip"),
4955
+ cookie: getHeader(request, "cookie"),
4956
+ purpose: getHeader(request, "purpose")
4957
+ };
4958
+ }
4850
4959
  //! @see: https://shopify.dev/docs/api/storefront/latest/mutations/cartCreate
4851
4960
  //! @see https://shopify.dev/docs/api/storefront/latest/queries/cart
4852
4961
  //! @see: https://shopify.dev/docs/api/storefront/latest/mutations/cartLinesAdd
@@ -4976,6 +5085,7 @@ exports.changelogHandler = changelogHandler;
4976
5085
  exports.createCartHandler = createCartHandler;
4977
5086
  exports.createContentSecurityPolicy = createContentSecurityPolicy;
4978
5087
  exports.createCustomerAccountClient = createCustomerAccountClient;
5088
+ exports.createHydrogenContext = createHydrogenContext;
4979
5089
  exports.createStorefrontClient = createStorefrontClient;
4980
5090
  exports.createWithCache = createWithCache;
4981
5091
  exports.formatAPIResult = formatAPIResult;