You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm currently digging a bit more on how SWR works and I'm facing to an issue.
From my understanding, useSWR fetch data once and cache it (key => data). Then if you refresh your page OR switch from component and go back to the one before, useSWR will:
1: get data from cache and set this data to the data argument of useSWR hook (so your application will display a data instantly (maybe stale, but better than no data)
2: at the same time it will revalidate the data to get the latest up to dated data
Am I right?
Because I don't have at all this expected behavior on my app.
Here's a first log I have:
The data is undefined, the cache already has the data (maybe stale, but he has the data), and the useSWR is currently fetching the data (isLoading).
The problem is: Why SWR doesn't set the data (userData) from what he has in the cache?
Because I need to wait the isLoading to be true in order to have my data (userData) not undefined anymore:
I understand why he refetch the data, but don't understand why he doesn't use the existing cache (while the fetch is pending).
So from what I see the "stale-while-revalidate" mechanism seems not to work here.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hey! 👋
I'm currently digging a bit more on how SWR works and I'm facing to an issue.
From my understanding, useSWR fetch data once and cache it (key => data). Then if you refresh your page OR switch from component and go back to the one before, useSWR will:
Am I right?
Because I don't have at all this expected behavior on my app.
Here's a first log I have:

The data is undefined, the cache already has the data (maybe stale, but he has the data), and the useSWR is currently fetching the data (isLoading).
The problem is: Why SWR doesn't set the data (userData) from what he has in the cache?
Because I need to wait the isLoading to be true in order to have my data (userData) not undefined anymore:

I understand why he refetch the data, but don't understand why he doesn't use the existing cache (while the fetch is pending).
So from what I see the "stale-while-revalidate" mechanism seems not to work here.
FYI:
Here's my hook:
Here is my fetcher:
Also second (bonus 😅) question:
Is there a way to refetch data only if a specific hour is passed in the day?
Thanks in advance for your answers! 🙏
Beta Was this translation helpful? Give feedback.
All reactions