Uh oh!
There was an error while loading. Please reload this page.
feat: replaces noop store with memory store - #29
Conversation
moretti
commented
Sep 23, 2021
Hey Gabriel, thank you for this! There's another PR that it's adding a cookie storage for browsers that do not support Basically when |
I think memoryStorage should be the last fallback option, indeed, since it's not persistent across navigation through pages. interfaceIStorage{isAvailable(): boolean;getItem(key: string): string|null;setItem(key: string,value: string): void;}So we could select storage like this: constlocalStorageWrapper={isAvailable(){if(typeofwindow==='undefined'||!'localStorage'inwindow){returnfalse}try{letkey='__pushtell_react__';window.localStorage.setItem(key,key);if(window.localStorage.getItem(key)===key){window.localStorage.removeItem(key);returntrue;}}catch(e){// DO NOTHING }returnfalse},getItem(key){returnlocalStorage.getItem(key)},setItem(key,value){localStorage.setItem(key,value)}}constcookieStorageWrapper={// ...}constmemoryStorageWrapper={values: {},isAvailable(){returntrue},getItem(key){returnthis.values[key]},setItem(key,value){this.values[key]=value}}// Ordered by preference conststorages=[localStorageWrapper,cookieStorageWrapper,memoryStorageWrapper]constavailableStorage=storages.find((storage)=>storage.isAvailable())exportdefaultavailableStorage; |
Table of Contents
Description
This implements a basic memory store instead of noop.
Motivation and Context
When server-side rendering, having a noop store causes
useExperimentcalls to fail even if they were made after aemitter.defineVariantscall.How Has This Been Tested?
I've added tests to store.jsx to check if it could set and get items from it. The most important part is to delete
window.localStorageand keep it working properly.Screenshots (if appropriate):
Types of changes
Checklist: