swrv icon indicating copy to clipboard operation
swrv copied to clipboard

is there support for indexeddb?

Open christopherFindon opened this issue 3 years ago • 9 comments

for our needs the size of local storage is not sufficient, the goal is to load from cache on subsequent visits

I know indexeddb is an old solution but I am not aware of another solution that can replace it.

christopherFindon avatar Mar 08 '22 23:03 christopherFindon

Yea we could add a new cache adapter re: src/cache/adapters/localStorage.ts

darrenjennings avatar Mar 09 '22 00:03 darrenjennings

I tried making one using idb-key-value, but the inherent issue is that the Cache adapter methods cannot be async, while IndexedDB's API is. So this would also require a change to the Caching base class and implementation.

ZeroThe2nd avatar Aug 09 '22 11:08 ZeroThe2nd

even with localforage?

CavalcanteLeo avatar Aug 23 '22 19:08 CavalcanteLeo

@CavalcanteLeo As long as a library/anything requires async operations, it currently cannot be done as the cache adapter's methods don't allow async methods. That includes LocalForage, as per their own documentation:

image

.then indicates a promise, which we cannot use.

The entire IndexedDB API (the low-level browser type) is a promise-based API. As long as the Cache adapters can't work with get (k: string): ICacheItem<CacheData> | Promise<ICacheItem<CacheData>>, making it accept a promise or a normal return type, IDB can't be done properly. So this would require a library-side change.

As my current schedule is a bit tight, I can't promise (pun intended) I'll have time shortly, but I could try looking into making a PR to resolve the issue.

The only alternative for a short-term fix is use some workaround to de-promise-ify the library calls. That would mean using some wrapper function that waits until the promise is resolved before continuing, but this will definitely hurt performance, which is what you don't want.

ZeroThe2nd avatar Aug 24 '22 08:08 ZeroThe2nd

@darrenjennings Are there any plans to make the cache adapters optionally support promises, or is this something that's not planned?

ZeroThe2nd avatar Sep 02 '22 16:09 ZeroThe2nd

May be we can add in dexie db.

edson-pro avatar Feb 12 '23 21:02 edson-pro

@edson-pro That would retain the same issue. Dexie relies on Indexed DB, so you'd need to un-async the asynchronous API from Indexed DB itself. This would require a change in the library to accept async (promise) type functions for its cache adapter. Which, seemingly, we're not getting. As this issue is still open without any response from the devs 😞

ZeroThe2nd avatar Mar 09 '23 10:03 ZeroThe2nd

yeah, I get it @ZeroThe2nd, Thanks for the response.

edson-pro avatar Mar 09 '23 11:03 edson-pro

Hey, sorry everyone I just saw this thread. If anyone is open to contributing, I'm happy to help review any suggested changes.

adamdehaven avatar Mar 09 '23 13:03 adamdehaven