Closed eltorio closed 1 year ago
I looked into the code and did some tests. It's caused by the initial rehydration (and hooks) being done before the $hydrate
and $persist
functions are bound to the store.
Im just sceptical about the use of these methods in hooks ?
However the error of the methods being undefined at hook call time can be fixed. Its just kinda dangerous because you risk running infinite recursion if you dont specify { runHooks: false }
Dear Praz, thank you for your answer, you can be sceptical ! But this is only a small code for reproducing the $hydrate call. Let me explain my objective: I have a store filled during navigation by remote requests to some remote URLs, this is time consuming and since I need to proxy the request for CORS, it is server consuming. Today if my store is already filled I return the store content and there is no remote request nor proxy request… My store is a sort of cache. No problem with the expiration of datas because while the user closes its browser or hits refresh the store is deleted and it will be refilled…
But I'd like to make my store partially persistent for 7 days, so I added some timestamps and want to check in afterRestore if I need to reconstruct the cache. If nedded I'd like to delete the localStorage and next run $hydrate() for having a clean "persistence ready" store.
That's why I said that is not necessary a "bug" but probably a misunderstand Ronan
my current workaround is ugly: In beforeRestore I manually deserialize the localStorage keys for the stores, retrieve all the timestamps and if necessary issue some localStorage.removeItem('storeId') It works…
Describe the bug
I try to force hydration based on a timestamp in afterRestore hook
For reproducting:
Launch a browser on the dev port (probably http://127.0.0.1:5173), open the console and refresh the page I get:
The repo was basically created with:
only main.ts was modified https://github.com/highcanfly-club/pinia-hydration-test/blob/51919cabac6d1ac317ca07aebc369182b56a9779/src/main.ts#L4 store was defined like this
uncommenting the two commented lines makes the same result
Reproduction
https://github.com/highcanfly-club/pinia-hydration-test.git
System Info
Used Package Manager
npm
Validations