-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[REF-2219] Computed and cached vars based on rx.Cookie and rx.LocalStorage not getting updated starting from 0.4.3 #2851
Comments
@PasqualePuzio How is the cookie / localstorage being set in your app? I'm not able to reproduce this when setting a cookie and navigating to another page. |
Or is the problem when the cookie expires it still appears to be set in the state? |
We call Everything works perfectly until version 0.4.2, the issue shows up only starting from version 0.4.3. |
Are you using redis? Do you see this discrepancy in dev mode? |
Yes we're using REDIS and we haven't seen any difference between dev and prod. |
Okay glad to hear you're using redis because I believe the fix I posted a few hours ago addresses this issue. If you have time to run on that branch to prove the theory that would be amazing.
I couldn't quite get to a simple repro example, but a larger app where I'm using cookies and cached_var was able to reproduce it for me to discover the problem. |
@masenf I'm sorry but I have to reopen this issue :) We have upgraded to the latest version (0.4.5) which includes this fix if I'm not mistaken, however the issue still occurs. Let me give you some more details on the structure of our app, maybe it'll help narrow down this issue. We have a base state called AppState which declares the both the cookie/local storage vars and the cached vars. Something like this:
then we have a number of children states which inherit from the parent state above:
Here's what we have observed:
based on the observations above, I suspect it has to do with hydration or state management (maybe some vars are not saved/loaded correctly?). Please let me know if there's anything else we can do to help narrow this down. PS in this latest version the flickering effect I was mentioning here https://github.com/orgs/reflex-dev/discussions/2885 is way less noticeable, it lasts just a fraction of a second instead of 1-2 seconds like it was until in version 0.4.2 |
I think we have narrowed this one down to #2950, trying to work out an actual minimal repro/smoking gun so the fix can be verified. |
Yes! Minimal repro achieved! import reflex as rx
class State(rx.State):
cookie: str = rx.Cookie()
@rx.var
def v(self):
pass
class Child(State):
cookie2: str = rx.Cookie()
def index():
return rx.button(
f"Set cookie: '{State.cookie}' '{Child.cookie2}'",
on_click=[State.set_cookie("foo"), Child.set_cookie2("bar")],
)
app = rx.App()
app.add_page(index) Load the page. Click the "Set cookie" button. Notice that the button text changes to include Screen.Recording.2024-03-28.at.12.46.41.mov |
The already cached states may have unsaved changes which can be wiped out if they are refetched from redis in the middle of handling an event. If the root state already knows about one of the potentially missing states, then use the instance that is already cached. Fix #2851
* Add test_get_state_from_sibling_not_cached A better unit test to catch issues with refetching parent states and calculating the wrong parent state names to fetch. * _determine_missing_parent_states: correctly generate state names Prepend only the previous state name to the current relative_parent_state_name instead of joining all of the previous state names together. * [REF-2219] Avoid refetching states that are already cached The already cached states may have unsaved changes which can be wiped out if they are refetched from redis in the middle of handling an event. If the root state already knows about one of the potentially missing states, then use the instance that is already cached. Fix #2851
Describe the bug
In our project we have a few computed (some of which are cached) vars which are based (they perform some calculations on) on other state vars of type rx.Cookie and rx.LocalStorage.
All of them belong to the same state.
We have noticed that starting from 0.4.3 (until 0.4.2 everything is fine according to our tests) the value of these vars are not consistent with the value of the cookie or the local storage entry. It looks like they're not updated when they should.
To Reproduce
Expected behavior
We would expect the same behavior we had until 0.4.2, meaning that the values of these vars reflect the value of the underlying rx.Cookie or rx.LocalStorage vars and get updated upon hydration and whenever the cookie value changes.
Specifics
REF-2219
The text was updated successfully, but these errors were encountered: