From ca768cb2fac3a53d6f94a838b314f168e990783d Mon Sep 17 00:00:00 2001 From: Elliot Courant Date: Thu, 5 Dec 2019 15:21:21 -0600 Subject: [PATCH] Added support for sessionStorage for cache. This adds support for specifying localStorage or sessionStorage when bootstrapping the LD JS client. This can be used if the user's hash changes frequently and can cause a build-up of old records in the client's local storage by instead storing data in the session storage. --- src/__tests__/browserPlatform-test.js | 26 ++++++++++++++++++++++++++ src/browserPlatform.js | 16 ++++++++++++---- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/__tests__/browserPlatform-test.js b/src/__tests__/browserPlatform-test.js index 659188c4..63f4ba14 100644 --- a/src/__tests__/browserPlatform-test.js +++ b/src/__tests__/browserPlatform-test.js @@ -134,6 +134,32 @@ describe('browserPlatform', () => { }); }); + describe('sessionStorage', () => { + const plat = browserPlatform({ bootstrap: 'sessionStorage' }); + + // Make sure that all of the get/set tests for the localStorage tests + // also work with sessionStorage. + it('returns null or undefined for missing value', async () => { + const value = await plat.localStorage.get(lsKeyPrefix + 'unused-key'); + expect(value).not.toBe(expect.anything()); + }); + + it('can get and set value', async () => { + const key = lsKeyPrefix + 'get-set-key'; + await plat.localStorage.set(key, 'hello'); + const value = await plat.localStorage.get(key); + expect(value).toEqual('hello'); + }); + + it('can delete value', async () => { + const key = lsKeyPrefix + 'delete-key'; + await plat.localStorage.set(key, 'hello'); + await plat.localStorage.clear(key); + const value = plat.localStorage.get(key); + expect(value).not.toBe(expect.anything()); + }); + }); + describe('localStorage', () => { // Since we're not currently running these tests in an actual browser, this is really using a // mock implementation of window.localStorage, but these tests still verify that our async diff --git a/src/browserPlatform.js b/src/browserPlatform.js index e828abce..6c287099 100644 --- a/src/browserPlatform.js +++ b/src/browserPlatform.js @@ -39,20 +39,28 @@ export default function makeBrowserPlatform(options) { }; try { - if (window.localStorage) { + // Allow the user to specify which storage type to use. If the user specifies session storage + // then use that. Otherwise default to local storage. Both of these implement the same + // storage interface, documentation here: https://developer.mozilla.org/en-US/docs/Web/API/Storage + const storage = + ((options || {}).bootstrap || '').toUpperCase() === 'SESSIONSTORAGE' + ? window.sessionStorage + : window.localStorage; + + if (storage) { ret.localStorage = { get: key => new Promise(resolve => { - resolve(window.localStorage.getItem(key)); + resolve(storage.getItem(key)); }), set: (key, value) => new Promise(resolve => { - window.localStorage.setItem(key, value); + storage.setItem(key, value); resolve(); }), clear: key => new Promise(resolve => { - window.localStorage.removeItem(key); + storage.removeItem(key); resolve(); }), };