svelte-persistent-store
svelte-persistent-store copied to clipboard
Persistent Store broken since 2.0.0
Persist has not worked for me at all, since 2.0.0.
I've tried persist(), I've tried persistBrowserSession(), but both just forget everything when I hit refresh.
I have watched the console while I hit refresh, and it doesn't appear to serialise / deserialise properly.
Do you have a repository where I can reproduce the error ?
I managed to get it working by explicitly specifying a serialization ... setSerialization(JSON.stringify, JSON.parse). Without this, it doesn't serialise at all, and I get bad data.
I've experienced lack of persistence when using IndexedDB type, completely reproducible for you at my REPL.
persist(writable([]), createIndexedDBStorage(), "myArray")
i discovered that if you persist an empty array as default, without setting another value in immediate execution, then you go and save your array of data to the store (ie: $mystore = [...mydata]), it will NOT actually save the update to the IndexedDB.
No attempts to save any new value at any subsequent execution time will work. So in most cases, the empty value gets saved, then never actually updates after that point.
This is also true for string, but it has a strange additional phenomena where the value very briefly updates, then disappears again.
REPL: https://svelte.dev/repl/e4753fcaebf541d9a33408dede9b5990?version=4.2.0
@MacFJA I am seeing that sometimes the serialized data contains oddities like this
And that when that gets stored, even in localStorage, it will break persistence. This is where @michaelcuneo suggestion of setSerialization comes into play. By doing that, we get clean data in the storage, and no more issues on localStorage (still issues in indexedDB).
These tests always need run on a clean browser storage/db to be accurately reproducing.
The value you see this added by the serialization engine to avoid circular dependency.
But you can change the serialization engine to another one, like @michaelcuneo did.
You can use plain JSON (https://github.com/MacFJA/svelte-persistent-store/blob/main/.docs/How-To/06-Change-Serialization.md#using-json-for-serialization), ESSerializer (https://github.com/MacFJA/svelte-persistent-store/blob/main/.docs/How-To/06-Change-Serialization.md#using-esserializer-for-serialization), or even devalue (from Rich-Harris ; it's the same serialization engine used in SvelteKit).
You can use whatever you want or whatever fit the best to your need.
There are no good or bad choices. They all have their pros and cons.
@MacFJA I'll play around with some of those thanks for the tip - but that last message was more of a side point, want to be sure you noticed the message i had above that - with the REPL showing the bug in IndexedDBStorage
@MacFJA
I also downloaded this repo and tried to reproduce this effect I see in REPL, but was NOT able to reproduce using your test page.
I cant fathom why I can prove the bug in REPL and in my own project, but not in your test page.
I've experienced lack of persistence when using IndexedDB type, completely reproducible for you at my REPL.
persist(writable([]), createIndexedDBStorage(), "myArray")i discovered that if you persist an empty array as default, without setting another value in immediate execution, then you go and save your array of data to the store (ie: $mystore = [...mydata]), it will NOT actually save the update to the IndexedDB.
No attempts to save any new value at any subsequent execution time will work. So in most cases, the empty value gets saved, then never actually updates after that point.
This is also true for string, but it has a strange additional phenomena where the value very briefly updates, then disappears again.
REPL: https://svelte.dev/repl/e4753fcaebf541d9a33408dede9b5990?version=4.2.0