@shopify/hydrogen 2024.7.2 → 2024.7.3

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.3";
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,31 @@ function addCspDirective(currentValue, value) {
3988
3993
  }
3989
3994
  var Script = react.forwardRef(
3990
3995
  (props, ref) => {
3996
+ const { waitForHydration, src, ...rest } = props;
3997
+ if (!src) throw new Error("Script components require a `src` prop");
3998
+ if (waitForHydration) return /* @__PURE__ */ jsxRuntime.jsx(LazyScript, { src, options: rest });
3991
3999
  const nonce = useNonce();
3992
- return /* @__PURE__ */ jsxRuntime.jsx("script", { suppressHydrationWarning: true, ...props, nonce, ref });
4000
+ return /* @__PURE__ */ jsxRuntime.jsx(
4001
+ "script",
4002
+ {
4003
+ suppressHydrationWarning: true,
4004
+ ...rest,
4005
+ src,
4006
+ nonce,
4007
+ ref
4008
+ }
4009
+ );
3993
4010
  }
3994
4011
  );
4012
+ function LazyScript({
4013
+ src,
4014
+ options
4015
+ }) {
4016
+ hydrogenReact.useLoadScript(src, {
4017
+ attributes: options
4018
+ });
4019
+ return null;
4020
+ }
3995
4021
  function useOptimisticData(identifier) {
3996
4022
  const fetchers = react$1.useFetchers();
3997
4023
  const data = {};
@@ -4205,6 +4231,7 @@ function ShopifyAnalytics({
4205
4231
  const { subscribe: subscribe2, register: register2, canTrack } = useAnalytics();
4206
4232
  const [shopifyReady, setShopifyReady] = react.useState(false);
4207
4233
  const [privacyReady, setPrivacyReady] = react.useState(false);
4234
+ const init = react.useRef(false);
4208
4235
  const { ready: shopifyAnalyticsReady } = register2("Internal_Shopify_Analytics");
4209
4236
  const { ready: customerPrivacyReady } = register2(
4210
4237
  "Internal_Shopify_CustomerPrivacy"
@@ -4233,6 +4260,8 @@ function ShopifyAnalytics({
4233
4260
  checkoutDomain
4234
4261
  });
4235
4262
  react.useEffect(() => {
4263
+ if (init.current) return;
4264
+ init.current = true;
4236
4265
  subscribe2(AnalyticsEvent.PAGE_VIEWED, pageViewHandler);
4237
4266
  subscribe2(AnalyticsEvent.PRODUCT_VIEWED, productViewHandler);
4238
4267
  subscribe2(AnalyticsEvent.COLLECTION_VIEWED, collectionViewHandler);
@@ -4686,7 +4715,6 @@ function AnalyticsProvider({
4686
4715
  consent,
4687
4716
  customData = {},
4688
4717
  shop: shopProp = null,
4689
- disableThrowOnError = false,
4690
4718
  cookieDomain
4691
4719
  }) {
4692
4720
  const listenerSet = react.useRef(false);
@@ -4847,6 +4875,84 @@ var RichText = function(props) {
4847
4875
  }
4848
4876
  );
4849
4877
  };
4878
+
4879
+ // src/createHydrogenContext.ts
4880
+ function createHydrogenContext(options) {
4881
+ const {
4882
+ env,
4883
+ request,
4884
+ cache,
4885
+ waitUntil,
4886
+ i18n,
4887
+ session,
4888
+ logErrors,
4889
+ storefront: storefrontOptions = {},
4890
+ customerAccount: customerAccountOptions,
4891
+ cart: cartOptions = {}
4892
+ } = options;
4893
+ if (!session) {
4894
+ console.warn(
4895
+ `[h2:warn:createHydrogenContext] A session object is required to create hydrogen context.`
4896
+ );
4897
+ }
4898
+ const { storefront } = createStorefrontClient({
4899
+ // share options
4900
+ cache,
4901
+ waitUntil,
4902
+ i18n,
4903
+ logErrors,
4904
+ // storefrontOptions
4905
+ storefrontHeaders: storefrontOptions.headers || getStorefrontHeaders(request),
4906
+ storefrontApiVersion: storefrontOptions.apiVersion,
4907
+ // defaults
4908
+ storefrontId: env.PUBLIC_STOREFRONT_ID,
4909
+ storeDomain: env.PUBLIC_STORE_DOMAIN,
4910
+ privateStorefrontToken: env.PRIVATE_STOREFRONT_API_TOKEN,
4911
+ publicStorefrontToken: env.PUBLIC_STOREFRONT_API_TOKEN
4912
+ });
4913
+ const customerAccount = createCustomerAccountClient({
4914
+ // share options
4915
+ session,
4916
+ request,
4917
+ waitUntil,
4918
+ logErrors,
4919
+ // customerAccountOptions
4920
+ customerApiVersion: customerAccountOptions?.apiVersion,
4921
+ authUrl: customerAccountOptions?.authUrl,
4922
+ customAuthStatusHandler: customerAccountOptions?.customAuthStatusHandler,
4923
+ unstableB2b: customerAccountOptions?.unstableB2b,
4924
+ // defaults
4925
+ customerAccountId: env.PUBLIC_CUSTOMER_ACCOUNT_API_CLIENT_ID,
4926
+ customerAccountUrl: env.PUBLIC_CUSTOMER_ACCOUNT_API_URL
4927
+ });
4928
+ const cart = createCartHandler({
4929
+ // cartOptions
4930
+ getCartId: cartOptions.getId || cartGetIdDefault(request.headers),
4931
+ setCartId: cartOptions.setId || cartSetIdDefault(),
4932
+ cartQueryFragment: cartOptions.queryFragment,
4933
+ cartMutateFragment: cartOptions.mutateFragment,
4934
+ customMethods: cartOptions.customMethods,
4935
+ // defaults
4936
+ storefront,
4937
+ customerAccount
4938
+ });
4939
+ return {
4940
+ storefront,
4941
+ customerAccount,
4942
+ cart,
4943
+ env,
4944
+ waitUntil,
4945
+ session
4946
+ };
4947
+ }
4948
+ function getStorefrontHeaders(request) {
4949
+ return {
4950
+ requestGroupId: getHeader(request, "request-id"),
4951
+ buyerIp: getHeader(request, "oxygen-buyer-ip"),
4952
+ cookie: getHeader(request, "cookie"),
4953
+ purpose: getHeader(request, "purpose")
4954
+ };
4955
+ }
4850
4956
  //! @see: https://shopify.dev/docs/api/storefront/latest/mutations/cartCreate
4851
4957
  //! @see https://shopify.dev/docs/api/storefront/latest/queries/cart
4852
4958
  //! @see: https://shopify.dev/docs/api/storefront/latest/mutations/cartLinesAdd
@@ -4976,6 +5082,7 @@ exports.changelogHandler = changelogHandler;
4976
5082
  exports.createCartHandler = createCartHandler;
4977
5083
  exports.createContentSecurityPolicy = createContentSecurityPolicy;
4978
5084
  exports.createCustomerAccountClient = createCustomerAccountClient;
5085
+ exports.createHydrogenContext = createHydrogenContext;
4979
5086
  exports.createStorefrontClient = createStorefrontClient;
4980
5087
  exports.createWithCache = createWithCache;
4981
5088
  exports.formatAPIResult = formatAPIResult;