Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean
congue cursus efficitur. Pellentesque odio tortor, suscipit nec tortor ut.
diff --git a/src/pages/product.tsx b/src/pages/product.tsx
index 3b2e5f9..40fce3e 100644
--- a/src/pages/product.tsx
+++ b/src/pages/product.tsx
@@ -38,6 +38,10 @@ const SingleProductPage = ({ product }: { product: Product }) => {
}
});
+ if (!product) {
+ return null;
+ }
+
const { variants, description }: {variants: ProductVariantConnection, description: string } = product;
const options: Array
diff --git a/src/state/Cart.tsx b/src/state/Cart.tsx
index 52b60d9..67d7da9 100644
--- a/src/state/Cart.tsx
+++ b/src/state/Cart.tsx
@@ -1,4 +1,5 @@
import React, { createContext, useReducer, ReactNode } from 'react';
+import { isLocalStorageAvailable } from 'utils/functions';
import { Checkout } from 'shopify-storefront-api-typings';
type Props = {
@@ -11,7 +12,7 @@ type InitialStateType = {
};
const initialState = {
- cart: JSON.parse(localStorage.getItem('cart')) || null,
+ cart: isLocalStorageAvailable() && JSON.parse(localStorage.getItem('cart')) || null,
setCart: () => {}
};
@@ -23,9 +24,9 @@ const reducer = (state: any, action: any) => {
switch (action.type) {
case actions.SET_CART:
if (action.cart) {
- localStorage.setItem('cart', JSON.stringify(action.cart));
+ isLocalStorageAvailable() && localStorage.setItem('cart', JSON.stringify(action.cart));
} else {
- localStorage.removeItem('cart');
+ isLocalStorageAvailable() && localStorage.removeItem('cart');
}
return { ...state, cart: action.cart };
default:
diff --git a/src/state/Customer.tsx b/src/state/Customer.tsx
index de002d9..14b009e 100644
--- a/src/state/Customer.tsx
+++ b/src/state/Customer.tsx
@@ -1,4 +1,5 @@
import React, { createContext, useReducer, ReactNode } from 'react';
+import { isLocalStorageAvailable } from 'utils/functions';
import { Customer } from 'shopify-storefront-api-typings';
import Cookies from 'js-cookie';
@@ -14,7 +15,7 @@ type InitialStateType = {
};
const initialState = {
- customer: JSON.parse(localStorage.getItem('customer') !== undefined ? localStorage.getItem('customer') : null) || null,
+ customer: isLocalStorageAvailable() && JSON.parse(localStorage.getItem('customer') !== undefined ? localStorage.getItem('customer') : null) || null,
setCustomer: () => {},
token: Cookies.get('vtok') || null,
setToken: () => {}
@@ -29,9 +30,9 @@ const reducer = (state: any, action: any) => {
switch (action.type) {
case actions.SET_CUSTOMER:
if (action.customer === null) {
- localStorage.removeItem('customer');
+ isLocalStorageAvailable() && localStorage.removeItem('customer');
} else {
- localStorage.setItem('customer', JSON.stringify(action.customer));
+ isLocalStorageAvailable() && localStorage.setItem('customer', JSON.stringify(action.customer));
}
return { ...state, customer: action.customer };
case actions.SET_TOKEN:
diff --git a/src/state/Shipping.tsx b/src/state/Shipping.tsx
index a5a7439..048c92f 100644
--- a/src/state/Shipping.tsx
+++ b/src/state/Shipping.tsx
@@ -1,4 +1,5 @@
import React, { createContext, useReducer, ReactNode } from 'react';
+import { isLocalStorageAvailable } from 'utils/functions';
import { MailingAddress } from 'shopify-storefront-api-typings';
type Props = {
@@ -11,7 +12,7 @@ type InitialStateType = {
};
const initialState = {
- shipping: JSON.parse(localStorage.getItem('shipping') !== undefined ? localStorage.getItem('shipping') : null) || null,
+ shipping: isLocalStorageAvailable() && JSON.parse(localStorage.getItem('shipping') !== undefined ? localStorage.getItem('shipping') : null) || null,
setShipping: () => {},
};
@@ -23,9 +24,9 @@ const reducer = (state: any, action: any) => {
switch (action.type) {
case actions.SET_SHIPPING:
if (action.shipping === null) {
- localStorage.removeItem('shipping');
+ isLocalStorageAvailable() && localStorage.removeItem('shipping');
} else {
- localStorage.setItem('shipping', JSON.stringify(action.shipping));
+ isLocalStorageAvailable() && localStorage.setItem('shipping', JSON.stringify(action.shipping));
}
return { ...state, shipping: action.shipping };
default:
diff --git a/src/utils/functions.ts b/src/utils/functions.ts
index 27b3860..41e675f 100644
--- a/src/utils/functions.ts
+++ b/src/utils/functions.ts
@@ -31,4 +31,8 @@ export const saveNewCart = (checkout, setCart) => {
} else {
console.log('nope');
}
-};
\ No newline at end of file
+};
+
+export const isLocalStorageAvailable = () => {
+ return typeof window === 'object' || typeof window !== 'undefined';
+}
\ No newline at end of file
Terms of Service