forked from pushtell/react-ab-test
-
Notifications
You must be signed in to change notification settings - Fork 44
Cookie storage added as option #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
b10z
wants to merge
2
commits into
marvelapp:master
Choose a base branch
from
b10z:added_cookies_as_storage_option
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| const storeCookie = () => { | ||
| const cookieStorage = { | ||
| getCookie: function (c_name) { | ||
| let c_value = document.cookie, | ||
| c_start = c_value.indexOf(' ' + c_name + '='); | ||
| if (c_start == -1) c_start = c_value.indexOf(c_name + '='); | ||
| if (c_start == -1) { | ||
| c_value = null; | ||
| } else { | ||
| c_start = c_value.indexOf('=', c_start) + 1; | ||
| var c_end = c_value.indexOf(';', c_start); | ||
| if (c_end == -1) { | ||
| c_end = c_value.length; | ||
| } | ||
| c_value = unescape(c_value.substring(c_start, c_end)); | ||
| } | ||
| return c_value; | ||
| }, | ||
| setCookie: function (c_name, variantName) { | ||
| if (!this.getCookie(c_name)) { | ||
| document.cookie = c_name + '=' + variantName; | ||
| } | ||
| }, | ||
| }; | ||
| return cookieStorage; | ||
| }; | ||
|
|
||
| export default storeCookie; |
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey this looks great, thanks for submitting this PR!
Justing thinking: I'm looking at https://caniuse.com/?search=localstorage
and it seems that
window.localStorageis supported by almost any modern browser (96.6% of the users at the moment).So maybe rather than manually setting this option via
emitterwe could extend the existingstoreto fallback to cookies whenlocalStorageis not supported.For example something like:
could replace the existing
storeand be more resilient? 🤔This includes some snippets from Modernizr to perform feature detection and use
js-cookieto read/write cookies. The library is only 800 bytes, so it should be fine to add it as dependency.What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey,
Thanks for the comment! You are right, this might be the correct way of implementing the feature. Also, I believe that the code you provided is nicely written and elegant. Have you tested the code above with the tool? If you haven't, I have no problem of doing it and provide any changes needed!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey!
I added the provided code, reverted my previous commit and installed js-cookie as well. The code tested on both of my dummy react projects, everything worked as expected, even when i blocked local storage the code created a cookie as expected. For any change/fix/test/whatever please let me know.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @b10z sorry for the late reply, unfortunately I didn't have much free time recently, but I'll have a look next week. Thanks again for contributing to the project!