Need to access async methods inside getCachedData, idea?
As the title says, my goal is to access an
IndexedDB
from the getCachedData
as a way to check if the query has been cached or not.
But the issue is that the IndexedDB
API is async and getCachedData
does not support async inside it.
I'm not sure to understand if there was a specific reason for that choice or if it's just something that defaulted like that.
If the maintainers are open to suggestion/API change (changing getCachedData to async, I'm willing to open a PR for that).
Thanks π2 Replies
Hi @Martichou. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
You're right that the current implementation of
getCachedData
in Nuxt 3 doesn't support asynchronous operations. This limitation does present a challenge when trying to access asynchronous storage methods like IndexedDB.
The getCachedData
function was introduced in Nuxt 3.8 as a way to provide custom caching mechanisms for useAsyncData
and useFetch
composables. However, it was designed to be synchronous, likely for simplicity and performance reasons.
Currently, there's no direct way to use async methods inside getCachedData
. However, there are a few potential workarounds you could consider:
1. Use a synchronous storage method instead of IndexedDB, if possible.
2. Implement a synchronous cache layer that's updated asynchronously in the background.
As for changing the API to support async operations, it's a reasonable suggestion. However, making getCachedData
asynchronous would likely require significant changes to the internal implementation of useAsyncData
and useFetch
.
If you're interested in proposing this change, you could open an issue on the Nuxt GitHub repository to discuss the idea with the maintainers. They might be open to considering such a change for a future version of Nuxt.
For now, you might want to explore alternative caching strategies that can work within the current synchronous constraints of getCachedData
.
For more information on how getCachedData
currently works, you can check out this video by Alex Lichter: Nuxt 3.8 - Client-side caching with getCachedData (https://www.youtube.com/watch?v=aQPR0xn-MMk).
Remember, any significant changes to the API would need careful consideration of backwards compatibility and performance implications.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@358295169042219008> Kapa.ai is still learning and improving, please let me know how I did by reacting below.