Skip to content

Commit

Permalink
Merge pull request #482 from recurly/fix-storage-availability-chrome
Browse files Browse the repository at this point in the history
Fixes storage availability check within hosted fields
  • Loading branch information
chrissrogers authored Oct 9, 2018
2 parents 5fb4c1f + f8a9574 commit 2b6994d
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 2b6994d

Please sign in to comment.