diff --git a/blocks/product-recommendations/product-recommendations.js b/blocks/product-recommendations/product-recommendations.js index e18d3b49ea..9d9d2bc810 100644 --- a/blocks/product-recommendations/product-recommendations.js +++ b/blocks/product-recommendations/product-recommendations.js @@ -158,8 +158,12 @@ async function loadRecommendation(block, context, visibility, filters) { const viewHistory = window.localStorage.getItem(`${storeViewCode}:productViewHistory`) || '[]'; context.userViewHistory = JSON.parse(viewHistory); } catch (e) { - window.localStorage.removeItem('productViewHistory'); - console.error('Error parsing product view history', e); + try { + window.localStorage.removeItem('productViewHistory'); + } catch (f) { + // Do nothing + } + console.warn('Error parsing product view history', e); } // Get purchase history @@ -167,8 +171,12 @@ async function loadRecommendation(block, context, visibility, filters) { const purchaseHistory = window.localStorage.getItem(`${storeViewCode}:purchaseHistory`) || '[]'; context.userPurchaseHistory = JSON.parse(purchaseHistory); } catch (e) { - window.localStorage.removeItem('purchaseHistory'); - console.error('Error parsing purchase history', e); + try { + window.localStorage.removeItem('purchaseHistory'); + } catch (f) { + // Do nothing + } + console.warn('Error parsing purchase history', e); } window.adobeDataLayer.push((dl) => { diff --git a/scripts/configs.js b/scripts/configs.js index dc6a5a53f9..95d803fcfa 100644 --- a/scripts/configs.js +++ b/scripts/configs.js @@ -16,10 +16,13 @@ export const calcEnvironment = () => { if (href.includes('localhost')) { environment = 'dev'; } - - const environmentFromConfig = window.sessionStorage.getItem('environment'); - if (environmentFromConfig && ALLOWED_CONFIGS.includes(environmentFromConfig) && environment !== 'prod') { - return environmentFromConfig; + try { + const environmentFromConfig = window.sessionStorage.getItem('environment'); + if (environmentFromConfig && ALLOWED_CONFIGS.includes(environmentFromConfig) && environment !== 'prod') { + return environmentFromConfig; + } + } catch (e) { + // Do nothing } return environment; @@ -37,15 +40,31 @@ function buildConfigURL(environment) { const getConfigForEnvironment = async (environment) => { const env = environment || calcEnvironment(); - let configJSON = window.sessionStorage.getItem(`config:${env}`); + let configJSON; + + try { + configJSON = window.sessionStorage.getItem(`config:${env}`); + if (!configJSON) { + throw new Error('No config in sessionStorage'); + } + } catch (e) { + // Do nothing + } + if (!configJSON) { configJSON = await fetch(buildConfigURL(env)); if (!configJSON.ok) { throw new Error(`Failed to fetch config for ${env}`); } configJSON = await configJSON.text(); - window.sessionStorage.setItem(`config:${env}`, configJSON); + + try { + window.sessionStorage.setItem(`config:${env}`, configJSON); + } catch (e) { + // Do nothing + } } + return configJSON; }; diff --git a/scripts/minicart/api.js b/scripts/minicart/api.js index 5b579ed721..a2fd5d3e49 100644 --- a/scripts/minicart/api.js +++ b/scripts/minicart/api.js @@ -25,7 +25,12 @@ class Store { }; static getCartId() { - const cartIdField = window.localStorage.getItem(Store.CARTID_STORE); + let cartIdField; + try { + cartIdField = window.localStorage.getItem(Store.CARTID_STORE); + } catch (e) { + // Do nothing + } if (!cartIdField) { return null; } @@ -91,14 +96,22 @@ class Store { const parsed = JSON.parse(window.localStorage.getItem(`${this.key}_${this.cartId}`)) || Store.DEFAULT_CART; return parsed; } catch (err) { - console.error('Failed to parse cart from localStore. Resetting it.'); - window.localStorage.removeItem(`${this.key}_${this.cartId}`); + console.warn('Failed to parse cart from localStore. Resetting it.'); + try { + window.localStorage.removeItem(`${this.key}_${this.cartId}`); + } catch (e) { + // Do nothing + } } return Store.DEFAULT_CART; } resetCart() { - window.localStorage.removeItem(`${this.key}_${this.cartId}`); + try { + window.localStorage.removeItem(`${this.key}_${this.cartId}`); + } catch (e) { + // Do nothing + } this.cartId = null; } diff --git a/scripts/scripts.js b/scripts/scripts.js index 5e86e9bf13..639c4cb632 100644 --- a/scripts/scripts.js +++ b/scripts/scripts.js @@ -247,8 +247,12 @@ async function loadLazy(doc) { import('./acdl/adobe-client-data-layer.min.js'), ]); - if (sessionStorage.getItem('acdl:debug')) { - import('./acdl/validate.js'); + try { + if (sessionStorage.getItem('acdl:debug')) { + import('./acdl/validate.js'); + } + } catch (e) { + // do nothing } trackHistory();