feat: replaces noop store with memory store#29
Open
gabteles wants to merge 2 commits intomarvelapp:masterfrom
Open
feat: replaces noop store with memory store#29gabteles wants to merge 2 commits intomarvelapp:masterfrom
gabteles wants to merge 2 commits intomarvelapp:masterfrom
Conversation
Member
|
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 |
Author
|
I think memoryStorage should be the last fallback option, indeed, since it's not persistent across navigation through pages. interface IStorage {
isAvailable(): boolean;
getItem(key: string): string | null;
setItem(key: string, value: string): void;
}So we could select storage like this: const localStorageWrapper = {
isAvailable() {
if (typeof window === 'undefined' || !'localStorage' in window) {
return false
}
try {
let key = '__pushtell_react__';
window.localStorage.setItem(key, key);
if (window.localStorage.getItem(key) === key) {
window.localStorage.removeItem(key);
return true;
}
} catch (e) {
// DO NOTHING
}
return false
},
getItem(key) { return localStorage.getItem(key) },
setItem(key, value) { localStorage.setItem(key, value) }
}
const cookieStorageWrapper = {
// ...
}
const memoryStorageWrapper = {
values: {},
isAvailable() { return true },
getItem(key) { return this.values[key] },
setItem(key, value) { this.values[key] = value }
}
// Ordered by preference
const storages = [
localStorageWrapper,
cookieStorageWrapper,
memoryStorageWrapper
]
const availableStorage = storages.find((storage) => storage.isAvailable())
export default availableStorage; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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: