Skip to content

Commit

Permalink
Fixes storage availability check
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
chrissrogers committed Oct 8, 2018
1 parent 5fb4c1f commit f8a9574
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
20 changes: 9 additions & 11 deletions lib/util/web-storage.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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
Expand Down Expand Up @@ -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;
};
}
2 changes: 1 addition & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
@@ -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');

Expand Down

0 comments on commit f8a9574

Please sign in to comment.