From f8a9574a8a32194f6bab9cddfb44557d3398bd9c Mon Sep 17 00:00:00 2001 From: Christopher Rogers Date: Mon, 8 Oct 2018 15:16:59 -0700 Subject: [PATCH] Fixes storage availability check - When chrome users disable third-party cookies, iframes from recurly.com on merchant pages are forbidden from accessing localStorage. This catches exceptions in that scenario and presents the objectStore as a replacement --- lib/util/web-storage.js | 20 +++++++++----------- webpack.config.js | 2 +- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/lib/util/web-storage.js b/lib/util/web-storage.js index b3db6804b..6a34582de 100644 --- a/lib/util/web-storage.js +++ b/lib/util/web-storage.js @@ -1,10 +1,9 @@ const debug = require('debug')('recurly:web-storage'); export const NAMESPACE = '__recurly__'; - export const STORES = { - local: window.localStorage, - session: window.sessionStorage + local: () => window.localStorage, + session: () => window.sessionStorage }; export const objectStore = { @@ -16,7 +15,6 @@ export const objectStore = { }; const namespaced = key => `${NAMESPACE}.${key}`; -const store = scope => available() ? STORES[scope] : objectStore; /** * Fetches a value from web storage, optionally setting its value @@ -44,14 +42,14 @@ export function set ({ scope = 'local', key, value }) { return store(scope).getItem(namespaced(key)); } -// Test for availability of storage by setting and removing the namespace -function available () { +function store (scope) { try { - STORES.local.setItem(NAMESPACE, NAMESPACE); - STORES.local.removeItem(NAMESPACE); + const store = STORES[scope](); + store.setItem(NAMESPACE, NAMESPACE); + store.removeItem(NAMESPACE); + return store; } catch (e) { debug('Web storage is not available due to', e); - return false; + return objectStore; } - return true; -}; +} diff --git a/webpack.config.js b/webpack.config.js index aea587d54..9202254f9 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,4 +1,4 @@ -var ExtractTextPlugin = require("extract-text-webpack-plugin"); +var ExtractTextPlugin = require('extract-text-webpack-plugin'); var path = require('path'); var minify = ~process.argv.indexOf('-p');