@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.
@@ -1,4 +1,4 @@
1
- import { createStorefrontClient as createStorefrontClient$1, SHOPIFY_STOREFRONT_ID_HEADER, getShopifyCookies, SHOPIFY_Y, SHOPIFY_STOREFRONT_Y_HEADER, SHOPIFY_S, SHOPIFY_STOREFRONT_S_HEADER, flattenConnection, ShopPayButton as ShopPayButton$1, useLoadScript, RichText as RichText$1, useShopifyCookies, sendShopifyAnalytics, AnalyticsEventName, AnalyticsPageType, parseGid, getClientBrowserParameters } from '@shopify/hydrogen-react';
1
+ import { useLoadScript, createStorefrontClient as createStorefrontClient$1, SHOPIFY_STOREFRONT_ID_HEADER, getShopifyCookies, SHOPIFY_Y, SHOPIFY_STOREFRONT_Y_HEADER, SHOPIFY_S, SHOPIFY_STOREFRONT_S_HEADER, flattenConnection, ShopPayButton as ShopPayButton$1, RichText as RichText$1, useShopifyCookies, sendShopifyAnalytics, AnalyticsEventName, AnalyticsPageType, parseGid, getClientBrowserParameters } from '@shopify/hydrogen-react';
2
2
  export { AnalyticsEventName, AnalyticsPageType, ExternalVideo, IMAGE_FRAGMENT, Image, MediaFile, ModelViewer, Money, ShopifySalesChannel, Video, customerAccountApiCustomScalars, flattenConnection, getClientBrowserParameters, getShopifyCookies, parseGid, parseMetafield, sendShopifyAnalytics, storefrontApiCustomScalars, useLoadScript, useMoney, useShopifyCookies } from '@shopify/hydrogen-react';
3
3
  import { lazy, createContext, forwardRef, useMemo, createElement, Suspense, Fragment, useRef, useEffect, useState, useContext } from 'react';
4
4
  import { useMatches, useLocation, useNavigation, Link, useNavigate, useFetcher, useFetchers } from '@remix-run/react';
@@ -417,7 +417,7 @@ var errorOnce = (string) => {
417
417
  };
418
418
 
419
419
  // src/version.ts
420
- var LIB_VERSION = "2024.7.2";
420
+ var LIB_VERSION = "2024.7.4";
421
421
 
422
422
  // src/utils/graphql.ts
423
423
  function minifyQuery(string) {
@@ -806,7 +806,10 @@ function formatAPIResult(data, errors2) {
806
806
 
807
807
  // src/utils/request.ts
808
808
  function getHeader(request, key) {
809
- const value = request.headers?.get?.(key) ?? request.headers?.[key];
809
+ return getHeaderValue(request.headers, key);
810
+ }
811
+ function getHeaderValue(headers, key) {
812
+ const value = headers?.get?.(key) ?? headers?.[key];
810
813
  return typeof value === "string" ? value : null;
811
814
  }
812
815
  function getDebugHeaders(request) {
@@ -817,11 +820,8 @@ function getDebugHeaders(request) {
817
820
  }
818
821
 
819
822
  // src/cache/create-with-cache.ts
820
- function createWithCache({
821
- cache,
822
- waitUntil,
823
- request
824
- }) {
823
+ function createWithCache(cacheOptions) {
824
+ const { cache, waitUntil, request } = cacheOptions;
825
825
  return function withCache(cacheKey, strategy, actionFn) {
826
826
  return runWithCache(cacheKey, actionFn, {
827
827
  strategy,
@@ -1721,7 +1721,7 @@ function Seo({ debug }) {
1721
1721
  const matches = useMatches();
1722
1722
  const location = useLocation();
1723
1723
  console.warn(
1724
- "[h2:warn:Seo] The `<Seo/>` component is deprecated. Use `getSeoMeta` instead.\nSee: https://shopify.dev/docs/api/hydrogen/2024-01/utilities/getseometa"
1724
+ "[h2:warn:Seo] The `<Seo/>` component is deprecated. Use `getSeoMeta` instead.\nSee: https://shopify.dev/docs/api/hydrogen/2024-07/utilities/getseometa"
1725
1725
  );
1726
1726
  const seoConfig = useMemo(() => {
1727
1727
  return matches.flatMap((match) => {
@@ -2480,6 +2480,11 @@ function createCustomerAccountClient({
2480
2480
  `[h2:warn:createCustomerAccountClient] You are using Customer Account API version ${customerApiVersion} when this version of Hydrogen was built for ${DEFAULT_CUSTOMER_API_VERSION}.`
2481
2481
  );
2482
2482
  }
2483
+ if (!session) {
2484
+ console.warn(
2485
+ `[h2:warn:createCustomerAccountClient] session is required to use Customer Account API. Ensure the session object passed in exist.`
2486
+ );
2487
+ }
2483
2488
  if (!request?.url) {
2484
2489
  throw new Error(
2485
2490
  "[h2:error:createCustomerAccountClient] The request object does not contain a URL."
@@ -3475,7 +3480,7 @@ var CART_METAFIELD_DELETE_MUTATION = () => `#graphql
3475
3480
  }
3476
3481
  `;
3477
3482
  var cartGetIdDefault = (requestHeaders) => {
3478
- const cookies = parse(requestHeaders.get("Cookie") || "");
3483
+ const cookies = parse(getHeaderValue(requestHeaders, "Cookie") || "");
3479
3484
  return () => {
3480
3485
  return cookies.cart ? `gid://shopify/Cart/${cookies.cart}` : void 0;
3481
3486
  };
@@ -3894,10 +3899,34 @@ function addCspDirective(currentValue, value) {
3894
3899
  }
3895
3900
  var Script = forwardRef(
3896
3901
  (props, ref) => {
3902
+ const { waitForHydration, src, ...rest } = props;
3903
+ if (waitForHydration) return /* @__PURE__ */ jsx(LazyScript, { src, options: rest });
3897
3904
  const nonce = useNonce();
3898
- return /* @__PURE__ */ jsx("script", { suppressHydrationWarning: true, ...props, nonce, ref });
3905
+ return /* @__PURE__ */ jsx(
3906
+ "script",
3907
+ {
3908
+ suppressHydrationWarning: true,
3909
+ ...rest,
3910
+ src,
3911
+ nonce,
3912
+ ref
3913
+ }
3914
+ );
3899
3915
  }
3900
3916
  );
3917
+ function LazyScript({
3918
+ src,
3919
+ options
3920
+ }) {
3921
+ if (!src)
3922
+ throw new Error(
3923
+ "`waitForHydration` with the Script component requires a `src` prop"
3924
+ );
3925
+ useLoadScript(src, {
3926
+ attributes: options
3927
+ });
3928
+ return null;
3929
+ }
3901
3930
  function useOptimisticData(identifier) {
3902
3931
  const fetchers = useFetchers();
3903
3932
  const data = {};
@@ -4111,6 +4140,7 @@ function ShopifyAnalytics({
4111
4140
  const { subscribe: subscribe2, register: register2, canTrack } = useAnalytics();
4112
4141
  const [shopifyReady, setShopifyReady] = useState(false);
4113
4142
  const [privacyReady, setPrivacyReady] = useState(false);
4143
+ const init = useRef(false);
4114
4144
  const { ready: shopifyAnalyticsReady } = register2("Internal_Shopify_Analytics");
4115
4145
  const { ready: customerPrivacyReady } = register2(
4116
4146
  "Internal_Shopify_CustomerPrivacy"
@@ -4139,6 +4169,8 @@ function ShopifyAnalytics({
4139
4169
  checkoutDomain
4140
4170
  });
4141
4171
  useEffect(() => {
4172
+ if (init.current) return;
4173
+ init.current = true;
4142
4174
  subscribe2(AnalyticsEvent.PAGE_VIEWED, pageViewHandler);
4143
4175
  subscribe2(AnalyticsEvent.PRODUCT_VIEWED, productViewHandler);
4144
4176
  subscribe2(AnalyticsEvent.COLLECTION_VIEWED, collectionViewHandler);
@@ -4592,7 +4624,6 @@ function AnalyticsProvider({
4592
4624
  consent,
4593
4625
  customData = {},
4594
4626
  shop: shopProp = null,
4595
- disableThrowOnError = false,
4596
4627
  cookieDomain
4597
4628
  }) {
4598
4629
  const listenerSet = useRef(false);
@@ -4753,6 +4784,84 @@ var RichText = function(props) {
4753
4784
  }
4754
4785
  );
4755
4786
  };
4787
+
4788
+ // src/createHydrogenContext.ts
4789
+ function createHydrogenContext(options) {
4790
+ const {
4791
+ env,
4792
+ request,
4793
+ cache,
4794
+ waitUntil,
4795
+ i18n,
4796
+ session,
4797
+ logErrors,
4798
+ storefront: storefrontOptions = {},
4799
+ customerAccount: customerAccountOptions,
4800
+ cart: cartOptions = {}
4801
+ } = options;
4802
+ if (!session) {
4803
+ console.warn(
4804
+ `[h2:warn:createHydrogenContext] A session object is required to create hydrogen context.`
4805
+ );
4806
+ }
4807
+ const { storefront } = createStorefrontClient({
4808
+ // share options
4809
+ cache,
4810
+ waitUntil,
4811
+ i18n,
4812
+ logErrors,
4813
+ // storefrontOptions
4814
+ storefrontHeaders: storefrontOptions.headers || getStorefrontHeaders(request),
4815
+ storefrontApiVersion: storefrontOptions.apiVersion,
4816
+ // defaults
4817
+ storefrontId: env.PUBLIC_STOREFRONT_ID,
4818
+ storeDomain: env.PUBLIC_STORE_DOMAIN,
4819
+ privateStorefrontToken: env.PRIVATE_STOREFRONT_API_TOKEN,
4820
+ publicStorefrontToken: env.PUBLIC_STOREFRONT_API_TOKEN
4821
+ });
4822
+ const customerAccount = createCustomerAccountClient({
4823
+ // share options
4824
+ session,
4825
+ request,
4826
+ waitUntil,
4827
+ logErrors,
4828
+ // customerAccountOptions
4829
+ customerApiVersion: customerAccountOptions?.apiVersion,
4830
+ authUrl: customerAccountOptions?.authUrl,
4831
+ customAuthStatusHandler: customerAccountOptions?.customAuthStatusHandler,
4832
+ unstableB2b: customerAccountOptions?.unstableB2b,
4833
+ // defaults
4834
+ customerAccountId: env.PUBLIC_CUSTOMER_ACCOUNT_API_CLIENT_ID,
4835
+ customerAccountUrl: env.PUBLIC_CUSTOMER_ACCOUNT_API_URL
4836
+ });
4837
+ const cart = createCartHandler({
4838
+ // cartOptions
4839
+ getCartId: cartOptions.getId || cartGetIdDefault(request.headers),
4840
+ setCartId: cartOptions.setId || cartSetIdDefault(),
4841
+ cartQueryFragment: cartOptions.queryFragment,
4842
+ cartMutateFragment: cartOptions.mutateFragment,
4843
+ customMethods: cartOptions.customMethods,
4844
+ // defaults
4845
+ storefront,
4846
+ customerAccount
4847
+ });
4848
+ return {
4849
+ storefront,
4850
+ customerAccount,
4851
+ cart,
4852
+ env,
4853
+ waitUntil,
4854
+ session
4855
+ };
4856
+ }
4857
+ function getStorefrontHeaders(request) {
4858
+ return {
4859
+ requestGroupId: getHeader(request, "request-id"),
4860
+ buyerIp: getHeader(request, "oxygen-buyer-ip"),
4861
+ cookie: getHeader(request, "cookie"),
4862
+ purpose: getHeader(request, "purpose")
4863
+ };
4864
+ }
4756
4865
  //! @see: https://shopify.dev/docs/api/storefront/latest/mutations/cartCreate
4757
4866
  //! @see https://shopify.dev/docs/api/storefront/latest/queries/cart
4758
4867
  //! @see: https://shopify.dev/docs/api/storefront/latest/mutations/cartLinesAdd
@@ -4765,6 +4874,6 @@ var RichText = function(props) {
4765
4874
  //! @see https://shopify.dev/docs/api/storefront/latest/mutations/cartMetafieldsSet
4766
4875
  //! @see https://shopify.dev/docs/api/storefront/2024-07/mutations/cartMetafieldDelete
4767
4876
 
4768
- export { Analytics, AnalyticsEvent, CacheCustom, CacheLong, CacheNone, CacheShort, CartForm, InMemoryCache, OptimisticInput, Pagination, RichText, Script, Seo, ShopPayButton, VariantSelector, cartAttributesUpdateDefault, cartBuyerIdentityUpdateDefault, cartCreateDefault, cartDiscountCodesUpdateDefault, cartGetDefault, cartGetIdDefault, cartLinesAddDefault, cartLinesRemoveDefault, cartLinesUpdateDefault, cartMetafieldDeleteDefault, cartMetafieldsSetDefault, cartNoteUpdateDefault, cartSelectedDeliveryOptionsUpdateDefault, cartSetIdDefault, changelogHandler, createCartHandler, createContentSecurityPolicy, createCustomerAccountClient, createStorefrontClient, createWithCache, formatAPIResult, generateCacheControlHeader, getCustomerPrivacy, getPaginationVariables, getSelectedProductOptions, getSeoMeta, getShopAnalytics, graphiqlLoader, storefrontRedirect, useAnalytics, useCustomerPrivacy, useNonce, useOptimisticCart, useOptimisticData, useOptimisticVariant };
4877
+ export { Analytics, AnalyticsEvent, CacheCustom, CacheLong, CacheNone, CacheShort, CartForm, InMemoryCache, OptimisticInput, Pagination, RichText, Script, Seo, ShopPayButton, VariantSelector, cartAttributesUpdateDefault, cartBuyerIdentityUpdateDefault, cartCreateDefault, cartDiscountCodesUpdateDefault, cartGetDefault, cartGetIdDefault, cartLinesAddDefault, cartLinesRemoveDefault, cartLinesUpdateDefault, cartMetafieldDeleteDefault, cartMetafieldsSetDefault, cartNoteUpdateDefault, cartSelectedDeliveryOptionsUpdateDefault, cartSetIdDefault, changelogHandler, createCartHandler, createContentSecurityPolicy, createCustomerAccountClient, createHydrogenContext, createStorefrontClient, createWithCache, formatAPIResult, generateCacheControlHeader, getCustomerPrivacy, getPaginationVariables, getSelectedProductOptions, getSeoMeta, getShopAnalytics, graphiqlLoader, storefrontRedirect, useAnalytics, useCustomerPrivacy, useNonce, useOptimisticCart, useOptimisticData, useOptimisticVariant };
4769
4878
  //# sourceMappingURL=out.js.map
4770
4879
  //# sourceMappingURL=index.js.map