Failure to deduplicate network requests #170
Replies: 14 comments 1 reply
-
I'm not sure what you mean, so PR is welcome. |
Beta Was this translation helpful? Give feedback.
-
Hi, thanks for your response. Basically, I am seeing multiple canceled network requests in the network tab of chrome when using a query in multiple places at the same time. (see stackblitz). I am not 100% sure what the expected behavior should be. But I think it shouldn't happen that there are multiple concurrent network requests for a single query. I am willing to create to try to fix this, but I prefer to both agree first that this is in fact a bug. |
Beta Was this translation helpful? Give feedback.
-
Multiple queries with the same key should produce one request. |
Beta Was this translation helpful? Give feedback.
-
The queries in my example have the same key, but produce multiple requests, So I guess seeing multiple canceled network requests is an error. |
Beta Was this translation helpful? Give feedback.
-
Can you reproduce on stackblitz, please? |
Beta Was this translation helpful? Give feedback.
-
Ah I mixed up code sandbox and stackblitz. I have reproduced on stackblitz (https://stackblitz.com/~/github.com/roel-de-jong-iqvia/request_deduplication) The canceled network requests appear the second time you press the "invalidate" button. |
Beta Was this translation helpful? Give feedback.
-
it doesn't load |
Beta Was this translation helpful? Give feedback.
-
Works in my browser. Press the "invalidate query" button. Second time you press the button there are multiple canceled requests (Perhaps it wasn't working at first because it requires changes to be pushed to the github repository) |
Beta Was this translation helpful? Give feedback.
-
Seems like the issue is in take(1). this.characters$ = this.event.pipe(
switchMap(() => this.queryService.character$("joehoe"))
); This works |
Beta Was this translation helpful? Give feedback.
-
btw why do you use take 1? |
Beta Was this translation helpful? Give feedback.
-
Indeed, I also noticed this:
In the minimal reproduction I posted it admittedly does not make a lot of sense. However, in our project we are migrating from Akita stores to Tanstack Query. In response to an Akita store change, I need to calculate / derive some state from the Tanstack query data, I can remove the Without having studied the library code I suspect some query context is lost when all query observers unsubscribe? |
Beta Was this translation helpful? Give feedback.
-
If you only need to get the data once you can use ensureQueryData or getQueryData. https://tanstack.com/query/latest/docs/reference/QueryClient#queryclientgetquerydata |
Beta Was this translation helpful? Give feedback.
-
I can take a look on sunday to see if I can fix this. If I cannot fix this, I will create a PR to update the documentation with a warning. |
Beta Was this translation helpful? Give feedback.
-
I found the root cause of the problem. In both the example and my project I used the Because I only took the first emission with I solved the issue by replacing the use of the
This makes the use of for example |
Beta Was this translation helpful? Give feedback.
-
Which @ngneat/query-* package(s) are the source of the bug?
query
Is this a regression?
Yes
Description
Given
There are multiple components which "use" a query a single time (
take(1)
)When
Then
Expected: a single network request is initiated
Actual: It appears requests are not de-duplicated, and the network tab shows multiple canceled queries.
When I continuously observe the query, only a single network request is fired.
Please provide a link to a minimal reproduction of the bug
https://stackblitz.com/~/github.com/roel-de-jong-iqvia/request_deduplication
Please provide the exception or error you saw
No response
Please provide the environment you discovered this bug in
No response
Anything else?
No response
Do you want to create a pull request?
Yes
Beta Was this translation helpful? Give feedback.
All reactions