Skip to content
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

Add a staleTime to queries #1167

Merged
merged 14 commits into from
Oct 20, 2022
Merged

Add a staleTime to queries #1167

merged 14 commits into from
Oct 20, 2022

Conversation

Janpot
Copy link
Member

@Janpot Janpot commented Oct 17, 2022

Adding a staleTime of 10m to react-query to tame the amount of API calls a bit until we figure out how we should allow users to finetune this behavior.

Also adding a way to enable the react-query devtools in the application.

This also takes care of invalidating cache during editing. The method is straightforward. Alongside the dom we supply a map of hashes of the dom nodes that we have confirmed being saved on the server. We use this hash in the query key to invalidate its cache when serverside node changes. During deployed runtime we don't need to supply these hashes.

@render
Copy link

render bot commented Oct 17, 2022

@oliviertassinari oliviertassinari requested a deployment to staleTime - toolpad-db PR #1167 October 17, 2022 11:50 — with Render Abandoned
@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Oct 17, 2022
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Oct 18, 2022
@oliviertassinari oliviertassinari added the enhancement This is not a bug, nor a new feature label Oct 19, 2022
@@ -872,6 +878,7 @@ const queryClient = new QueryClient({
defaultOptions: {
queries: {
retry: false,
staleTime: 10 * 60 * 1000,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that we would be OK with:

Suggested change
staleTime: 10 * 60 * 1000,
staleTime: 1000,

From what I understand, it would mean that we never query more than once every second, so 3600 requests per hour, below the 5000 limits of GitHub. It's not pretty, but I wouldn't expect anyone to want to refresh more than once per second. But for example in https://master--toolpad.mui.com/_toolpad/app/cl6rqzry10009arlv9sto6qja/pages/ip23ggo I would definitely want to use the refresh button after I label a few issues.

Copy link
Member Author

@Janpot Janpot Oct 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would mean that we never query more than once every second

But it's not an unrealistic scenario to have more than one github query on one page. Perhaps we can make it slightly longer, say 10s? I'm also planning to make this configurable at some point (#1093).

Copy link
Member

@oliviertassinari oliviertassinari Oct 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my perspective, when using a refresh button, we should feel like the results returned are up to date, reliably. I doubt that a staleTime > 1s can allow this UX.
A second concern is that if we set 10s and we fix the over-fetching problem, then we might be blind to a regression.

What I think would be great is to first solve the over-fetching, and then consider caching but to solve API requests that are slow.

Copy link
Member Author

@Janpot Janpot Oct 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you call refetch it should ignore cache and always refetch. So the button should always work, regardless of how long we make the staleTime.

Copy link
Member

@oliviertassinari oliviertassinari Oct 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you call refetch it should ignore cache and always refetch.

Oh yeah, this would be even better.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oliviertassinari I took care of the cache busting in the editor. Do you still think we should shorten the staleTime when we have the refetch function?

Copy link
Member

@oliviertassinari oliviertassinari Oct 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Janpot Per #1163 (comment), I think that we could even remove staleTime if it allows us to not fight with cache invalidation issues, e.g. when editing records. But to be honest, I don't fully understand what this property does so I might not be the best one to answer.

Copy link
Member Author

@Janpot Janpot Oct 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e.g. when editing records

The current architecture doesn't help with that neither. You'd still have to call refetch after updating the row to make the table up to date. There is no way to do it automatic as the list/delete/update methods are not linked in any way. For that we'd need something like a collection that we can link as a whole to a datagrid (#385)

But to be honest, I don't fully understand what this property does so I might not be the best one to answer.

staleTime sets the time after which data is considered out of date. That doesn't mean react-query will automatically refetch it after this time. It just means that after this time has passed, when certain triggers fire (e.g. window focus) react-query will refetch the query in the background.

Will set it at 0 again. The rest of this PR can stay.

Copy link
Member

@oliviertassinari oliviertassinari Oct 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Janpot Ok, then agree 10s sounds better than 1s, it could even be 1min 👍

Screenshot 2022-10-20 at 15 29 10

https://tanstack.com/query/v4/docs/guides/important-defaults

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding feature request that could help with invalidation: #1184

Copy link
Member

@oliviertassinari oliviertassinari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

@Janpot Janpot enabled auto-merge (squash) October 20, 2022 14:20
@Janpot Janpot disabled auto-merge October 20, 2022 14:29
@Janpot Janpot enabled auto-merge (squash) October 20, 2022 14:30
@Janpot Janpot disabled auto-merge October 20, 2022 14:35
@Janpot Janpot merged commit 315f440 into master Oct 20, 2022
@Janpot Janpot deleted the staleTime branch October 20, 2022 14:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This is not a bug, nor a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants