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
feat(nuxt): add dedupe
option for data fetching composables
#24564
Conversation
Run & review this pull request in StackBlitz Codeflow. |
β Live Preview ready!
|
Codecov ReportAll modified and coverable lines are covered by tests β
Additional details and impacted files@@ Coverage Diff @@
## main #24564 +/- ##
=======================================
Coverage ? 58.65%
=======================================
Files ? 5
Lines ? 861
Branches ? 46
=======================================
Hits ? 505
Misses ? 356
Partials ? 0 β View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a nice idea.
Note that it introduces a use of dedupe
that is the opposite from the current usage (as an option to refresh
). refresh({ dedupe: false })
means 'do not cancel existing requests in favour of this new one'.
But this dedupe: true
means `do not make any new requests if there is an existing pending request.
I am not in fact sure that my original choice of dedupe
as the option for refresh was the most semantically correct but I think the usage should align between option to useAsyncData
and the option to refresh()
.
I would propose making dedupe
accept these options:
'cancel'
(ortrue
) - cancels existing requests when a new one is made <- default option'defer'
(orfalse
) - does not make new requests at all if there is a pending request
We would mark true
or false
types as deprecated and remove at a type level in a future minor.
Suggests welcome from @nuxt/core on naming choices here. π
Thank you! Yes it allows you to use the fetch anywhere without worrying about it being duplicated. Very useful in large companies when it's hard to keep track. I updated my PM but not sure I understood you correctly. Please take a look when you get a chance. Thank you so much! β€οΈ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you add a test in this section (that is, the 'describe' block) of the runtime composables test? π
nuxt/test/nuxt/composables.test.ts
Lines 130 to 147 in 6e44b1b
describe('useAsyncData', () => { | |
it('should work at basic level', async () => { | |
const res = useAsyncData(() => Promise.resolve('test')) | |
expect(Object.keys(res)).toMatchInlineSnapshot(` | |
[ | |
"data", | |
"pending", | |
"error", | |
"status", | |
"execute", | |
"refresh", | |
] | |
`) | |
expect(res instanceof Promise).toBeTruthy() | |
expect(res.data.value).toBe(null) | |
await res | |
expect(res.data.value).toBe('test') | |
}) |
ofc. Here it is 8aa08d4 Let me know please if I can improve it somehow. Thank you! :) |
Love this functionality! I implemented this by storing the useFetch in a pinia store, but for bigger projects with a lot of data sources, this is better! |
dedupe
option to useAsyncData
and useFetch
dedupe
option to useAsyncData
and useFetch
dedupe
option for data fetching composables
π Linked issue
β Type of change
π Description
Added dedupe option to allow deduplicating requests to be used only once at the time. It is super useful in such cases:
I'm using something similar already in real prod project. But! I had to build another hook on top of that. It looks like this...
So I think it would be cool to have it internally π Let me know please if you see some issues. That's my first PR in Nuxt. π₯
π Checklist