@shopify/hydrogen 2023.7.1 → 2023.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.
@@ -164,7 +164,9 @@ function CacheLong(overrideOptions) {
164
164
  return {
165
165
  mode: PUBLIC,
166
166
  maxAge: 3600,
167
+ // 1 hour
167
168
  staleWhileRevalidate: 82800,
169
+ // 23 Hours
168
170
  ...overrideOptions
169
171
  };
170
172
  }
@@ -218,7 +220,7 @@ async function setItem(cache, request, response, userCacheOptions) {
218
220
  );
219
221
  response.headers.set("cache-control", paddedCacheControlString);
220
222
  response.headers.set("real-cache-control", cacheControlString);
221
- response.headers.set("cache-put-date", new Date().toUTCString());
223
+ response.headers.set("cache-put-date", (/* @__PURE__ */ new Date()).toUTCString());
222
224
  await cache.put(request, response);
223
225
  }
224
226
  async function deleteItem(cache, request) {
@@ -235,7 +237,7 @@ function calculateAge(response, responseDate) {
235
237
  responseMaxAge = parseFloat(maxAgeMatch[1]);
236
238
  }
237
239
  }
238
- const ageInMs = new Date().valueOf() - new Date(responseDate).valueOf();
240
+ const ageInMs = (/* @__PURE__ */ new Date()).valueOf() - new Date(responseDate).valueOf();
239
241
  return [ageInMs / 1e3, responseMaxAge];
240
242
  }
241
243
  function isStale(request, response) {
@@ -321,6 +323,7 @@ async function runWithCache(cacheKey, actionFn, {
321
323
  return actionFn();
322
324
  }
323
325
  const key = hashKey([
326
+ // '__HYDROGEN_CACHE_ID__', // TODO purgeQueryCacheOnBuild
324
327
  ...typeof cacheKey === "string" ? [cacheKey] : cacheKey
325
328
  ]);
326
329
  const cachedItem = await getItemFromCache(cacheInstance, key);
@@ -378,7 +381,13 @@ async function fetchWithServerCache(url, requestInit, {
378
381
  try {
379
382
  data = await response[returnType]();
380
383
  } catch {
381
- data = await response.text();
384
+ try {
385
+ data = await response.text();
386
+ } catch {
387
+ throw new Error(
388
+ `Storefront API response code: ${response.status} (Request Id: ${response.headers.get("x-request-id")})`
389
+ );
390
+ }
382
391
  }
383
392
  return toSerializableResponse(data, response);
384
393
  },
@@ -413,7 +422,7 @@ var warnOnce = (string) => {
413
422
  };
414
423
 
415
424
  // src/version.ts
416
- var LIB_VERSION = "2023.7.1";
425
+ var LIB_VERSION = "2023.7.3";
417
426
 
418
427
  // src/storefront.ts
419
428
  var StorefrontApiError = class extends Error {
@@ -531,6 +540,20 @@ function createStorefrontClient(options) {
531
540
  }
532
541
  return {
533
542
  storefront: {
543
+ /**
544
+ * Sends a GraphQL query to the Storefront API.
545
+ *
546
+ * Example:
547
+ *
548
+ * ```js
549
+ * async function loader ({context: {storefront}}) {
550
+ * const data = await storefront.query('query { ... }', {
551
+ * variables: {},
552
+ * cache: storefront.CacheLong()
553
+ * });
554
+ * }
555
+ * ```
556
+ */
534
557
  query: (query, payload) => {
535
558
  query = minifyQuery(query);
536
559
  if (isMutationRE.test(query)) {
@@ -540,6 +563,19 @@ function createStorefrontClient(options) {
540
563
  }
541
564
  return fetchStorefrontApi({ ...payload, query });
542
565
  },
566
+ /**
567
+ * Sends a GraphQL mutation to the Storefront API.
568
+ *
569
+ * Example:
570
+ *
571
+ * ```js
572
+ * async function loader ({context: {storefront}}) {
573
+ * await storefront.mutate('mutation { ... }', {
574
+ * variables: {},
575
+ * });
576
+ * }
577
+ * ```
578
+ */
543
579
  mutate: (mutation, payload) => {
544
580
  mutation = minifyQuery(mutation);
545
581
  if (isQueryRE.test(mutation)) {
@@ -559,6 +595,25 @@ function createStorefrontClient(options) {
559
595
  getPrivateTokenHeaders,
560
596
  getShopifyDomain,
561
597
  getApiUrl: getStorefrontApiUrl,
598
+ /**
599
+ * Wether it's a GraphQL error returned in the Storefront API response.
600
+ *
601
+ * Example:
602
+ *
603
+ * ```js
604
+ * async function loader ({context: {storefront}}) {
605
+ * try {
606
+ * await storefront.query(...);
607
+ * } catch(error) {
608
+ * if (storefront.isApiError(error)) {
609
+ * // ...
610
+ * }
611
+ *
612
+ * throw error;
613
+ * }
614
+ * }
615
+ * ```
616
+ */
562
617
  isApiError: isStorefrontApiError,
563
618
  i18n: i18n ?? defaultI18n
564
619
  }
@@ -781,11 +836,11 @@ var graphiqlLoader = async function graphiqlLoader2({
781
836
  <script
782
837
  crossorigin
783
838
  src="https://unpkg.com/react@18/umd/react.development.js"
784
- ><\/script>
839
+ ></script>
785
840
  <script
786
841
  crossorigin
787
842
  src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"
788
- ><\/script>
843
+ ></script>
789
844
  <link rel="stylesheet" href="https://unpkg.com/graphiql@3/graphiql.min.css" />
790
845
  </head>
791
846
 
@@ -794,7 +849,7 @@ var graphiqlLoader = async function graphiqlLoader2({
794
849
  <script
795
850
  src="https://unpkg.com/graphiql@3/graphiql.min.js"
796
851
  type="application/javascript"
797
- ><\/script>
852
+ ></script>
798
853
  <script>
799
854
  const windowUrl = new URL(document.URL);
800
855
 
@@ -824,7 +879,7 @@ var graphiqlLoader = async function graphiqlLoader2({
824
879
  variables
825
880
  }),
826
881
  );
827
- <\/script>
882
+ </script>
828
883
  </body>
829
884
  </html>
830
885
  `,
@@ -1007,6 +1062,7 @@ function generateSeoTags(seoInput) {
1007
1062
  type: "application/ld+json",
1008
1063
  children: JSON.stringify(block)
1009
1064
  },
1065
+ // @ts-expect-error
1010
1066
  `json-ld-${block?.["@type"] || block?.name || index++}`
1011
1067
  );
1012
1068
  tagResults.push(tag);
@@ -1392,18 +1448,17 @@ function CartForm({
1392
1448
  route
1393
1449
  }) {
1394
1450
  const fetcher = react$1.useFetcher();
1395
- return /* @__PURE__ */ jsxRuntime.jsxs(fetcher.Form, {
1396
- action: route || "",
1397
- method: "post",
1398
- children: [
1399
- (action || inputs) && /* @__PURE__ */ jsxRuntime.jsx("input", {
1451
+ return /* @__PURE__ */ jsxRuntime.jsxs(fetcher.Form, { action: route || "", method: "post", children: [
1452
+ (action || inputs) && /* @__PURE__ */ jsxRuntime.jsx(
1453
+ "input",
1454
+ {
1400
1455
  type: "hidden",
1401
1456
  name: INPUT_NAME,
1402
1457
  value: JSON.stringify({ action, inputs })
1403
- }),
1404
- typeof children === "function" ? children(fetcher) : children
1405
- ]
1406
- });
1458
+ }
1459
+ ),
1460
+ typeof children === "function" ? children(fetcher) : children
1461
+ ] });
1407
1462
  }
1408
1463
  CartForm.INPUT_NAME = INPUT_NAME;
1409
1464
  CartForm.ACTIONS = {
@@ -2119,7 +2174,10 @@ function VariantSelector({
2119
2174
  )
2120
2175
  );
2121
2176
  const currentParam = searchParams.get(option.name);
2122
- const calculatedActiveValue = currentParam ? currentParam === value : false;
2177
+ const calculatedActiveValue = currentParam ? (
2178
+ // If a URL parameter exists for the current option, check if it equals the current value
2179
+ currentParam === value
2180
+ ) : false;
2123
2181
  if (calculatedActiveValue) {
2124
2182
  activeValue = value;
2125
2183
  }
@@ -2162,22 +2220,25 @@ function useVariantPath(handle) {
2162
2220
  const searchParams = new URLSearchParams(search);
2163
2221
  return {
2164
2222
  searchParams,
2223
+ // If the current pathname matches the product page, we need to make sure
2224
+ // that we append to the current search params. Otherwise all the search
2225
+ // params can be generated new.
2165
2226
  alreadyOnProductPage: path === pathname,
2166
2227
  path
2167
2228
  };
2168
2229
  }, [pathname, search, handle]);
2169
2230
  }
2170
- //! @see https://shopify.dev/docs/api/storefront/2023-07/mutations/cartMetafieldDelete
2171
- //! @see https://shopify.dev/docs/api/storefront/latest/mutations/cartBuyerIdentityUpdate
2172
- //! @see https://shopify.dev/docs/api/storefront/latest/mutations/cartDiscountCodesUpdate
2173
- //! @see https://shopify.dev/docs/api/storefront/latest/mutations/cartMetafieldsSet
2174
- //! @see https://shopify.dev/docs/api/storefront/latest/mutations/cartNoteUpdate
2175
- //! @see https://shopify.dev/docs/api/storefront/latest/mutations/cartSelectedDeliveryOptionsUpdate
2176
- //! @see https://shopify.dev/docs/api/storefront/latest/queries/cart
2177
2231
  //! @see: https://shopify.dev/docs/api/storefront/latest/mutations/cartCreate
2232
+ //! @see https://shopify.dev/docs/api/storefront/latest/queries/cart
2178
2233
  //! @see: https://shopify.dev/docs/api/storefront/latest/mutations/cartLinesAdd
2179
- //! @see: https://shopify.dev/docs/api/storefront/latest/mutations/cartLinesRemove
2180
2234
  //! @see: https://shopify.dev/docs/api/storefront/latest/mutations/cartLinesUpdate
2235
+ //! @see: https://shopify.dev/docs/api/storefront/latest/mutations/cartLinesRemove
2236
+ //! @see https://shopify.dev/docs/api/storefront/latest/mutations/cartDiscountCodesUpdate
2237
+ //! @see https://shopify.dev/docs/api/storefront/latest/mutations/cartBuyerIdentityUpdate
2238
+ //! @see https://shopify.dev/docs/api/storefront/latest/mutations/cartNoteUpdate
2239
+ //! @see https://shopify.dev/docs/api/storefront/latest/mutations/cartSelectedDeliveryOptionsUpdate
2240
+ //! @see https://shopify.dev/docs/api/storefront/latest/mutations/cartMetafieldsSet
2241
+ //! @see https://shopify.dev/docs/api/storefront/2023-07/mutations/cartMetafieldDelete
2181
2242
 
2182
2243
  Object.defineProperty(exports, 'AnalyticsEventName', {
2183
2244
  enumerable: true,