@shopify/hydrogen 0.23.0 → 0.24.0

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 (74) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/dist/esnext/components/CartLinePrice/CartLinePrice.client.js +5 -8
  3. package/dist/esnext/components/CartLineProvider/context.d.ts +12 -2
  4. package/dist/esnext/components/CartProvider/CartProvider.client.js +2 -5
  5. package/dist/esnext/components/CartProvider/cart-queries.d.ts +1 -1
  6. package/dist/esnext/components/CartProvider/cart-queries.js +11 -0
  7. package/dist/esnext/components/CartProvider/graphql/CartAttributesUpdateMutation.d.ts +11 -1
  8. package/dist/esnext/components/CartProvider/graphql/CartBuyerIdentityUpdateMutation.d.ts +11 -1
  9. package/dist/esnext/components/CartProvider/graphql/CartCreateMutation.d.ts +11 -1
  10. package/dist/esnext/components/CartProvider/graphql/CartDiscountCodesUpdateMutation.d.ts +11 -1
  11. package/dist/esnext/components/CartProvider/graphql/CartFragment.d.ts +11 -1
  12. package/dist/esnext/components/CartProvider/graphql/CartLineAddMutation.d.ts +11 -1
  13. package/dist/esnext/components/CartProvider/graphql/CartLineRemoveMutation.d.ts +11 -1
  14. package/dist/esnext/components/CartProvider/graphql/CartLineUpdateMutation.d.ts +11 -1
  15. package/dist/esnext/components/CartProvider/graphql/CartNoteUpdateMutation.d.ts +11 -1
  16. package/dist/esnext/components/CartProvider/graphql/CartQuery.d.ts +11 -1
  17. package/dist/esnext/components/CartProvider/hooks.client.js +3 -5
  18. package/dist/esnext/components/CartProvider/types.d.ts +2 -0
  19. package/dist/esnext/components/ProductOptionsProvider/ProductOptionsProvider.client.js +6 -5
  20. package/dist/esnext/entry-server.js +2 -2
  21. package/dist/esnext/foundation/Analytics/ClientAnalytics.js +4 -4
  22. package/dist/esnext/foundation/Cache/strategies/index.d.ts +1 -0
  23. package/dist/esnext/foundation/Cache/strategies/index.js +1 -1
  24. package/dist/esnext/foundation/HydrogenRequest/HydrogenRequest.server.d.ts +1 -0
  25. package/dist/esnext/foundation/HydrogenRequest/HydrogenRequest.server.js +2 -1
  26. package/dist/esnext/foundation/ServerRequestProvider/ServerRequestProvider.d.ts +1 -2
  27. package/dist/esnext/foundation/ServerRequestProvider/ServerRequestProvider.js +16 -19
  28. package/dist/esnext/foundation/useQuery/hooks.d.ts +1 -0
  29. package/dist/esnext/foundation/useQuery/hooks.js +16 -2
  30. package/dist/esnext/foundation/useRequestContext/index.d.ts +15 -0
  31. package/dist/esnext/foundation/useRequestContext/index.js +23 -0
  32. package/dist/esnext/hooks/useCartLine/useCartLine.d.ts +12 -2
  33. package/dist/esnext/hooks/useProductOptions/types.d.ts +1 -1
  34. package/dist/esnext/index.d.ts +3 -0
  35. package/dist/esnext/index.js +1 -0
  36. package/dist/esnext/storefront-api-types.d.ts +10 -339
  37. package/dist/esnext/version.d.ts +1 -1
  38. package/dist/esnext/version.js +1 -1
  39. package/dist/node/components/CartLinePrice/CartLinePrice.client.js +5 -8
  40. package/dist/node/components/CartLineProvider/context.d.ts +12 -2
  41. package/dist/node/components/CartProvider/CartProvider.client.js +2 -5
  42. package/dist/node/components/CartProvider/cart-queries.d.ts +1 -1
  43. package/dist/node/components/CartProvider/cart-queries.js +11 -0
  44. package/dist/node/components/CartProvider/graphql/CartAttributesUpdateMutation.d.ts +11 -1
  45. package/dist/node/components/CartProvider/graphql/CartBuyerIdentityUpdateMutation.d.ts +11 -1
  46. package/dist/node/components/CartProvider/graphql/CartCreateMutation.d.ts +11 -1
  47. package/dist/node/components/CartProvider/graphql/CartDiscountCodesUpdateMutation.d.ts +11 -1
  48. package/dist/node/components/CartProvider/graphql/CartFragment.d.ts +11 -1
  49. package/dist/node/components/CartProvider/graphql/CartLineAddMutation.d.ts +11 -1
  50. package/dist/node/components/CartProvider/graphql/CartLineRemoveMutation.d.ts +11 -1
  51. package/dist/node/components/CartProvider/graphql/CartLineUpdateMutation.d.ts +11 -1
  52. package/dist/node/components/CartProvider/graphql/CartNoteUpdateMutation.d.ts +11 -1
  53. package/dist/node/components/CartProvider/graphql/CartQuery.d.ts +11 -1
  54. package/dist/node/components/CartProvider/hooks.client.js +2 -4
  55. package/dist/node/components/CartProvider/types.d.ts +2 -0
  56. package/dist/node/components/ProductOptionsProvider/ProductOptionsProvider.client.js +6 -5
  57. package/dist/node/entry-server.js +2 -2
  58. package/dist/node/foundation/Analytics/ClientAnalytics.js +4 -4
  59. package/dist/node/foundation/Cache/strategies/index.d.ts +1 -0
  60. package/dist/node/foundation/Cache/strategies/index.js +3 -3
  61. package/dist/node/foundation/HydrogenRequest/HydrogenRequest.server.d.ts +1 -0
  62. package/dist/node/foundation/HydrogenRequest/HydrogenRequest.server.js +2 -1
  63. package/dist/node/foundation/ServerRequestProvider/ServerRequestProvider.d.ts +1 -2
  64. package/dist/node/foundation/ServerRequestProvider/ServerRequestProvider.js +16 -19
  65. package/dist/node/foundation/useQuery/hooks.d.ts +1 -0
  66. package/dist/node/foundation/useQuery/hooks.js +17 -2
  67. package/dist/node/hooks/useCartLine/useCartLine.d.ts +12 -2
  68. package/dist/node/hooks/useProductOptions/types.d.ts +1 -1
  69. package/dist/node/storefront-api-types.d.ts +10 -339
  70. package/dist/node/version.d.ts +1 -1
  71. package/dist/node/version.js +1 -1
  72. package/package.json +1 -1
  73. package/vendor/react-server-dom-vite/cjs/react-server-dom-vite-plugin.js +36 -6
  74. package/vendor/react-server-dom-vite/esm/react-server-dom-vite-plugin.js +36 -6
@@ -9,14 +9,20 @@ function requestCacheRSC() {
9
9
  return new Map();
10
10
  }
11
11
  requestCacheRSC.key = Symbol.for('HYDROGEN_REQUEST');
12
+ function getInternalReactDispatcher() {
13
+ // @ts-ignore
14
+ return React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
15
+ .ReactCurrentDispatcher.current;
16
+ }
17
+ function isRsc() {
18
+ // This method is only available during RSC
19
+ return __HYDROGEN_TEST__ || !!getInternalReactDispatcher().getCacheForType;
20
+ }
12
21
  // Note: use this only during RSC/Flight rendering. The React dispatcher
13
22
  // for SSR/Fizz rendering does not implement getCacheForType.
14
23
  function getCacheForType(resource) {
15
24
  var _a;
16
- const dispatcher =
17
- // @ts-ignore
18
- React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
19
- .ReactCurrentDispatcher.current;
25
+ const dispatcher = getInternalReactDispatcher();
20
26
  // @ts-ignore
21
27
  if (__HYDROGEN_TEST__ && !dispatcher.getCacheForType) {
22
28
  // Jest does not have access to the RSC runtime, mock it here:
@@ -25,8 +31,8 @@ function getCacheForType(resource) {
25
31
  }
26
32
  return dispatcher.getCacheForType(resource);
27
33
  }
28
- export function ServerRequestProvider({ isRSC, request, children, }) {
29
- if (isRSC) {
34
+ export function ServerRequestProvider({ request, children, }) {
35
+ if (isRsc()) {
30
36
  // Save the request object in a React cache that is
31
37
  // scoped to this current rendering.
32
38
  const requestCache = getCacheForType(requestCacheRSC);
@@ -38,20 +44,11 @@ export function ServerRequestProvider({ isRSC, request, children, }) {
38
44
  return (React.createElement(RequestContextSSR.Provider, { value: request }, children));
39
45
  }
40
46
  export function useServerRequest() {
41
- let request;
42
- try {
43
- // This cache only works during RSC rendering:
44
- // @ts-ignore
45
- const cache = getCacheForType(requestCacheRSC);
46
- request = cache ? cache.get(requestCacheRSC.key) : null;
47
- }
48
- catch (_a) {
49
- // If RSC cache failed it means this is not an RSC request.
50
- // Try getting SSR context instead:
51
- request = useContext(RequestContextSSR); // eslint-disable-line react-hooks/rules-of-hooks
52
- }
47
+ var _a;
48
+ const request = isRsc()
49
+ ? (_a = getCacheForType(requestCacheRSC)) === null || _a === void 0 ? void 0 : _a.get(requestCacheRSC.key)
50
+ : useContext(RequestContextSSR); // eslint-disable-line react-hooks/rules-of-hooks
53
51
  if (!request) {
54
- // @ts-ignore
55
52
  if (__HYDROGEN_TEST__) {
56
53
  // Unit tests are not wrapped in ServerRequestProvider.
57
54
  // This mocks it, instead of providing it in every test.
@@ -34,3 +34,4 @@ queryOptions?: HydrogenUseQueryOptions): {
34
34
  data: T;
35
35
  error?: undefined;
36
36
  };
37
+ export declare function shouldPreloadQuery(queryOptions?: HydrogenUseQueryOptions): boolean;
@@ -1,7 +1,7 @@
1
1
  import { getLoggerWithContext, collectQueryCacheControlHeaders, collectQueryTimings, } from '../../utilities/log';
2
2
  import { deleteItemFromCache, generateSubRequestCacheControlHeader, getItemFromCache, isStale, setItemInCache, } from '../Cache/cache-sub-request';
3
3
  import { useRequestCacheData, useServerRequest } from '../ServerRequestProvider';
4
- import { CacheSeconds } from '../Cache/strategies';
4
+ import { CacheSeconds, NO_STORE } from '../Cache/strategies';
5
5
  /**
6
6
  * The `useQuery` hook executes an asynchronous operation like `fetch` in a way that
7
7
  * supports [Suspense](https://reactjs.org/docs/concurrent-mode-suspense.html). You can use this
@@ -24,7 +24,7 @@ queryOptions) {
24
24
  ];
25
25
  const fetcher = cachedQueryFnBuilder(withCacheIdKey, queryFn, queryOptions);
26
26
  collectQueryTimings(request, withCacheIdKey, 'requested');
27
- if (queryOptions === null || queryOptions === void 0 ? void 0 : queryOptions.preload) {
27
+ if (shouldPreloadQuery(queryOptions)) {
28
28
  request.savePreloadQuery({
29
29
  preload: queryOptions === null || queryOptions === void 0 ? void 0 : queryOptions.preload,
30
30
  key: withCacheIdKey,
@@ -33,6 +33,20 @@ queryOptions) {
33
33
  }
34
34
  return useRequestCacheData(withCacheIdKey, fetcher);
35
35
  }
36
+ export function shouldPreloadQuery(queryOptions) {
37
+ var _a, _b;
38
+ if (!queryOptions)
39
+ return true;
40
+ const hasCacheOverride = typeof ((_a = queryOptions === null || queryOptions === void 0 ? void 0 : queryOptions.cache) === null || _a === void 0 ? void 0 : _a.mode) !== 'undefined';
41
+ const hasPreloadOverride = typeof (queryOptions === null || queryOptions === void 0 ? void 0 : queryOptions.preload) !== 'undefined';
42
+ const cacheValue = (_b = queryOptions === null || queryOptions === void 0 ? void 0 : queryOptions.cache) === null || _b === void 0 ? void 0 : _b.mode;
43
+ const preloadValue = queryOptions === null || queryOptions === void 0 ? void 0 : queryOptions.preload;
44
+ // If preload is explicitly defined, then it takes precedence
45
+ if (hasPreloadOverride) {
46
+ return !!preloadValue;
47
+ }
48
+ return hasCacheOverride ? cacheValue !== NO_STORE : true;
49
+ }
36
50
  function cachedQueryFnBuilder(key, queryFn, queryOptions) {
37
51
  var _a;
38
52
  const resolvedQueryOptions = {
@@ -0,0 +1,15 @@
1
+ declare type ScopedContext = Record<string, any>;
2
+ /**
3
+ * Provides access to the current request context.
4
+ * @param scope - An optional string used to scope the request context. It is recommended to
5
+ * prevent modifying the properties added by other plugins.
6
+ * @returns A request-scoped object that can be modified to provide and consume information
7
+ * across different React components in the tree.
8
+ * @example
9
+ * ```js
10
+ * import {useRequestContext} from '@shopify/hydrogen';
11
+ * useRequestContext('my-plugin-name');
12
+ * ```
13
+ */
14
+ export declare function useRequestContext<T extends ScopedContext>(scope?: string): T;
15
+ export {};
@@ -0,0 +1,23 @@
1
+ import { META_ENV_SSR, useEnvContext } from '../ssr-interop';
2
+ /**
3
+ * Provides access to the current request context.
4
+ * @param scope - An optional string used to scope the request context. It is recommended to
5
+ * prevent modifying the properties added by other plugins.
6
+ * @returns A request-scoped object that can be modified to provide and consume information
7
+ * across different React components in the tree.
8
+ * @example
9
+ * ```js
10
+ * import {useRequestContext} from '@shopify/hydrogen';
11
+ * useRequestContext('my-plugin-name');
12
+ * ```
13
+ */
14
+ export function useRequestContext(scope = 'default') {
15
+ if (__HYDROGEN_DEV__ && !META_ENV_SSR) {
16
+ throw new Error('useRequestContext can only be used in the server');
17
+ }
18
+ const scopes = useEnvContext((req) => req.ctx.scopes);
19
+ if (!scopes.has(scope)) {
20
+ scopes.set(scope, Object.create(null));
21
+ }
22
+ return scopes.get(scope);
23
+ }
@@ -7,15 +7,25 @@ export declare function useCartLine(): {
7
7
  attributes: ({
8
8
  __typename?: "Attribute" | undefined;
9
9
  } & Pick<import("../../storefront-api-types").Attribute, "key" | "value">)[];
10
+ estimatedCost: {
11
+ __typename?: "CartLineEstimatedCost" | undefined;
12
+ } & {
13
+ totalAmount: {
14
+ __typename?: "MoneyV2" | undefined;
15
+ } & Pick<import("../../storefront-api-types").MoneyV2, "amount" | "currencyCode">;
16
+ compareAtAmount?: import("../../storefront-api-types").Maybe<{
17
+ __typename?: "MoneyV2" | undefined;
18
+ } & Pick<import("../../storefront-api-types").MoneyV2, "amount" | "currencyCode">> | undefined;
19
+ };
10
20
  merchandise: {
11
21
  __typename?: "ProductVariant" | undefined;
12
22
  } & Pick<import("../../storefront-api-types").ProductVariant, "id" | "title" | "availableForSale" | "requiresShipping"> & {
13
23
  compareAtPriceV2?: import("../../storefront-api-types").Maybe<{
14
24
  __typename?: "MoneyV2" | undefined;
15
- } & Pick<import("../../storefront-api-types").MoneyV2, "currencyCode" | "amount">> | undefined;
25
+ } & Pick<import("../../storefront-api-types").MoneyV2, "amount" | "currencyCode">> | undefined;
16
26
  priceV2: {
17
27
  __typename?: "MoneyV2" | undefined;
18
- } & Pick<import("../../storefront-api-types").MoneyV2, "currencyCode" | "amount">;
28
+ } & Pick<import("../../storefront-api-types").MoneyV2, "amount" | "currencyCode">;
19
29
  image?: import("../../storefront-api-types").Maybe<{
20
30
  __typename?: "Image" | undefined;
21
31
  } & Pick<import("../../storefront-api-types").Image, "id" | "height" | "width" | "url" | "altText">> | undefined;
@@ -3,7 +3,7 @@ import type { PartialDeep } from 'type-fest';
3
3
  export declare type SelectedOptions = {
4
4
  [key: string]: string;
5
5
  };
6
- export declare type SelectVariantCallback = (variant: ProductVariantType) => void;
6
+ export declare type SelectVariantCallback = (variant: ProductVariantType | null) => void;
7
7
  export declare type SelectOptionCallback = (name: SelectedOptionType['name'], value: SelectedOptionType['value']) => void;
8
8
  export declare type SelectOptionsCallback = (options: SelectedOptions) => void;
9
9
  export declare type OptionsInStockCallback = (name: SelectedOptionType['name'], value: SelectedOptionType['value']) => boolean;
@@ -20,6 +20,7 @@ export { log, type Logger } from './utilities/log';
20
20
  export { LocalizationProvider } from './components/LocalizationProvider/LocalizationProvider.server';
21
21
  export { ShopifyProvider } from './foundation/ShopifyProvider/ShopifyProvider.server';
22
22
  export { generateCacheControlHeader, NoStore, CacheSeconds, CacheMinutes, CacheHours, CacheDays, CacheWeeks, CacheMonths, CacheCustom, } from './foundation/Cache/strategies';
23
+ export { useRequestContext } from './foundation/useRequestContext';
23
24
  export { useServerAnalytics } from './foundation/Analytics/hook';
24
25
  export { ShopifyAnalytics } from './foundation/Analytics/connectors/Shopify/ShopifyAnalytics.server';
25
26
  export { ShopifyAnalyticsConstants } from './foundation/Analytics/connectors/Shopify/const';
@@ -38,3 +39,5 @@ export { CartQuery } from './components/CartProvider/cart-queries';
38
39
  * Override the client version of `fetchSync` with the server version.
39
40
  */
40
41
  export { fetchSync } from './foundation/fetchSync/server/fetchSync';
42
+ export { type HydrogenRequest } from './foundation/HydrogenRequest/HydrogenRequest.server';
43
+ export { type HydrogenResponse } from './foundation/HydrogenResponse/HydrogenResponse.server';
@@ -20,6 +20,7 @@ export { log } from './utilities/log';
20
20
  export { LocalizationProvider } from './components/LocalizationProvider/LocalizationProvider.server';
21
21
  export { ShopifyProvider } from './foundation/ShopifyProvider/ShopifyProvider.server';
22
22
  export { generateCacheControlHeader, NoStore, CacheSeconds, CacheMinutes, CacheHours, CacheDays, CacheWeeks, CacheMonths, CacheCustom, } from './foundation/Cache/strategies';
23
+ export { useRequestContext } from './foundation/useRequestContext';
23
24
  export { useServerAnalytics } from './foundation/Analytics/hook';
24
25
  export { ShopifyAnalytics } from './foundation/Analytics/connectors/Shopify/ShopifyAnalytics.server';
25
26
  export { ShopifyAnalyticsConstants } from './foundation/Analytics/connectors/Shopify/const';