@shopify/hydrogen 1.4.2 → 1.4.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.
@@ -6,6 +6,28 @@ import { useCartFetch } from './hooks.client.js';
6
6
  import { CartContext } from './context.js';
7
7
  import { CART_ID_STORAGE_KEY } from './constants.js';
8
8
  import { ClientAnalytics } from '../../foundation/Analytics/ClientAnalytics.js';
9
+ function getLocalStoragePolyfill() {
10
+ const storage = {};
11
+ return {
12
+ removeItem(key) {
13
+ delete storage[key];
14
+ },
15
+ setItem(key, value) {
16
+ storage[key] = value;
17
+ },
18
+ getItem(key) {
19
+ return storage[key];
20
+ },
21
+ };
22
+ }
23
+ const localStorage = (function () {
24
+ try {
25
+ return window.localStorage || getLocalStoragePolyfill();
26
+ }
27
+ catch (e) {
28
+ return getLocalStoragePolyfill();
29
+ }
30
+ })();
9
31
  function cartReducer(state, action) {
10
32
  switch (action.type) {
11
33
  case 'cartFetch': {
@@ -179,7 +201,7 @@ export function CartProvider({ children, numCartLines, onCreate, onLineAdd, onLi
179
201
  },
180
202
  });
181
203
  if (!data?.cart) {
182
- window.localStorage.removeItem(CART_ID_STORAGE_KEY);
204
+ localStorage.removeItem(CART_ID_STORAGE_KEY);
183
205
  dispatch({ type: 'resetCart' });
184
206
  return;
185
207
  }
@@ -226,7 +248,7 @@ export function CartProvider({ children, numCartLines, onCreate, onLineAdd, onLi
226
248
  type: 'resolve',
227
249
  cart: cartFromGraphQL(data.cartCreate.cart),
228
250
  });
229
- window.localStorage.setItem(CART_ID_STORAGE_KEY, data.cartCreate.cart.id);
251
+ localStorage.setItem(CART_ID_STORAGE_KEY, data.cartCreate.cart.id);
230
252
  }
231
253
  }, [
232
254
  onCreate,
@@ -18,6 +18,7 @@ export function CartProviderV2({ children, numCartLines, onCreate, onLineAdd, on
18
18
  }
19
19
  const [cartState, cartSend] = useCartAPIStateMachine({
20
20
  numCartLines,
21
+ data: cart,
21
22
  cartFragment,
22
23
  countryCode,
23
24
  onCartActionEntry(context, event) {
@@ -131,10 +132,7 @@ export function CartProviderV2({ children, numCartLines, onCreate, onLineAdd, on
131
132
  */
132
133
  useEffect(() => {
133
134
  if (!cartReady.current) {
134
- if (cart) {
135
- cartSend({ type: 'CART_SET', payload: { cart } });
136
- }
137
- else if (storageAvailable('localStorage')) {
135
+ if (!cart && storageAvailable('localStorage')) {
138
136
  try {
139
137
  const cartId = window.localStorage.getItem(CART_ID_STORAGE_KEY);
140
138
  if (cartId) {
@@ -2,7 +2,7 @@ import { StateMachine } from '@xstate/fsm';
2
2
  import { CartFragmentFragment } from './graphql/CartFragment.js';
3
3
  import { Cart, CartMachineActionEvent, CartMachineContext, CartMachineEvent, CartMachineFetchResultEvent, CartMachineTypeState } from './types.js';
4
4
  import { CountryCode } from '../../storefront-api-types.js';
5
- export declare function useCartAPIStateMachine({ numCartLines, onCartActionEntry, onCartActionOptimisticUI, onCartActionComplete, data, cartFragment, countryCode, }: {
5
+ export declare function useCartAPIStateMachine({ numCartLines, onCartActionEntry, onCartActionOptimisticUI, onCartActionComplete, data: cart, cartFragment, countryCode, }: {
6
6
  /** Maximum number of cart lines to fetch. Defaults to 250 cart lines. */
7
7
  numCartLines?: number;
8
8
  /** A callback that is invoked just before a Cart API action executes. */
@@ -86,46 +86,52 @@ const UPDATING_CART_EVENTS = {
86
86
  target: 'discountCodesUpdating',
87
87
  },
88
88
  };
89
- const cartMachine = createMachine({
90
- id: 'Cart',
91
- initial: 'uninitialized',
92
- states: {
93
- uninitialized: {
94
- on: INITIALIZING_CART_EVENTS,
89
+ function createCartMachine(initialCart) {
90
+ return createMachine({
91
+ id: 'Cart',
92
+ initial: initialCart ? 'idle' : 'uninitialized',
93
+ context: {
94
+ cart: initialCart && cartFromGraphQL(initialCart),
95
95
  },
96
- cartCompleted: {
97
- on: INITIALIZING_CART_EVENTS,
98
- },
99
- initializationError: {
100
- on: INITIALIZING_CART_EVENTS,
101
- },
102
- idle: {
103
- on: { ...INITIALIZING_CART_EVENTS, ...UPDATING_CART_EVENTS },
104
- },
105
- error: {
106
- on: { ...INITIALIZING_CART_EVENTS, ...UPDATING_CART_EVENTS },
96
+ states: {
97
+ uninitialized: {
98
+ on: INITIALIZING_CART_EVENTS,
99
+ },
100
+ cartCompleted: {
101
+ on: INITIALIZING_CART_EVENTS,
102
+ },
103
+ initializationError: {
104
+ on: INITIALIZING_CART_EVENTS,
105
+ },
106
+ idle: {
107
+ on: { ...INITIALIZING_CART_EVENTS, ...UPDATING_CART_EVENTS },
108
+ },
109
+ error: {
110
+ on: { ...INITIALIZING_CART_EVENTS, ...UPDATING_CART_EVENTS },
111
+ },
112
+ cartFetching: invokeCart('cartFetchAction', {
113
+ errorTarget: 'initializationError',
114
+ }),
115
+ cartCreating: invokeCart('cartCreateAction', {
116
+ errorTarget: 'initializationError',
117
+ }),
118
+ cartLineRemoving: invokeCart('cartLineRemoveAction'),
119
+ cartLineUpdating: invokeCart('cartLineUpdateAction'),
120
+ cartLineAdding: invokeCart('cartLineAddAction'),
121
+ noteUpdating: invokeCart('noteUpdateAction'),
122
+ buyerIdentityUpdating: invokeCart('buyerIdentityUpdateAction'),
123
+ cartAttributesUpdating: invokeCart('cartAttributesUpdateAction'),
124
+ discountCodesUpdating: invokeCart('discountCodesUpdateAction'),
107
125
  },
108
- cartFetching: invokeCart('cartFetchAction', {
109
- errorTarget: 'initializationError',
110
- }),
111
- cartCreating: invokeCart('cartCreateAction', {
112
- errorTarget: 'initializationError',
113
- }),
114
- cartLineRemoving: invokeCart('cartLineRemoveAction'),
115
- cartLineUpdating: invokeCart('cartLineUpdateAction'),
116
- cartLineAdding: invokeCart('cartLineAddAction'),
117
- noteUpdating: invokeCart('noteUpdateAction'),
118
- buyerIdentityUpdating: invokeCart('buyerIdentityUpdateAction'),
119
- cartAttributesUpdating: invokeCart('cartAttributesUpdateAction'),
120
- discountCodesUpdating: invokeCart('discountCodesUpdateAction'),
121
- },
122
- });
126
+ });
127
+ }
123
128
  export function useCartAPIStateMachine({ numCartLines, onCartActionEntry, onCartActionOptimisticUI, onCartActionComplete, data: cart, cartFragment, countryCode, }) {
124
129
  const { cartFetch, cartCreate, cartLineAdd, cartLineUpdate, cartLineRemove, noteUpdate, buyerIdentityUpdate, cartAttributesUpdate, discountCodesUpdate, } = useCartActions({
125
130
  numCartLines,
126
131
  cartFragment,
127
132
  countryCode,
128
133
  });
134
+ const cartMachine = useMemo(() => createCartMachine(cart), [cart]);
129
135
  const [state, send, service] = useMachine(cartMachine, {
130
136
  actions: {
131
137
  cartFetchAction: async (_, event) => {
@@ -1 +1 @@
1
- export declare const LIB_VERSION = "1.4.2";
1
+ export declare const LIB_VERSION = "1.4.3";
@@ -1 +1 @@
1
- export const LIB_VERSION = '1.4.2';
1
+ export const LIB_VERSION = '1.4.3';
package/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  "engines": {
8
8
  "node": ">=14"
9
9
  },
10
- "version": "1.4.2",
10
+ "version": "1.4.3",
11
11
  "description": "Modern custom Shopify storefronts",
12
12
  "license": "MIT",
13
13
  "main": "dist/esnext/index.js",