$persist - an option to save a variable value to the Session Storage (instead of the Local Storage) #1988
Replies: 3 comments 7 replies
-
The only problem with sessionStorage is that it doesn't work as people expect. The session is per tab so 2 tabs opened next to each other and visiting the same page have different sessionStorages which I find really annoying and I'm sure it would trick people. |
Beta Was this translation helpful? Give feedback.
-
I played a bit with that idea and we can have It would also support any custom storage object exposing getItem and setItem so it's possible to have <script>
window.cookieStorage = {
getItem(key) {
let cookies = document.cookie.split(";");
for (let i = 0; i < cookies.length; i++) {
let cookie = cookies[i].split("=");
if (key == cookie[0].trim()) {
return JSON.parse(decodeURIComponent(cookie[1]));
}
}
return null;
},
setItem(key, value) {
document.cookie = key+' = '+encodeURIComponent(JSON.stringify(value))
}
}
</script>
<div x-data="{ open: $persist(false).in(cookieStorage) }">
<button @click="open = !open">Toggle</button>
<span x-show="open">
Content...
</span>
</div> I've just created a PR to fix another issue with $persist. I'll mention this thread so we can see if Caleb would accept the feature. |
Beta Was this translation helpful? Give feedback.
-
Just found out about .using(sessionStorage). This is brilliant for this "annoyance bar" I had to add to a site. Thanks @SimoTod. |
Beta Was this translation helpful? Give feedback.
-
Hello,
Thank you for an amazing library and additional tools such as the Persist helper.
The Persist plugin allows to save the values to the Local Storage. It may be worth giving an option to save into the Session Storage. I see a use case in the use of page banners which you may want to show every time the user comes back. A key message/news could change and the Local Storage variable would hide the banner if the user dismissed it in the previous session(s). I believe that Local Storage has no expiration time, whereas Session Storage clears all data at the end of the page session.
Syntax could look like this for instance:
$persist.session(var)
or$persist(var).via('sessionStorage')
/$persist.indefinitely(var)
or$persist.via('localStorage')
ETC. (other suggestions are welcomed)What do you think?:)
PS: eventually cookies could be included with expiry time periods as well
Beta Was this translation helpful? Give feedback.
All reactions