Replies: 2 comments
-
Might it be better to use the Cache API? it persists the data to apps installed locally on mobile devices, and it works inside WebWorkers https://developer.mozilla.org/en-US/docs/Web/API/Cache https://caniuse.com/?search=cache also, what about customizing the "get" in addition to the "set" ? |
Beta Was this translation helpful? Give feedback.
-
Thanks for putting this up! Since the cache config (especially import { createCache, SWRConfig } from 'swr'
const __cache = new Map([['key', 'value']]) // no need to pass this as "initialData"
const { cache } = createCache<T>({
set: (key: string, value: any): void => __cache.set(key, value)},
get: (key: string): any => __cache.get(key),
})
function App () {
return <SWRConfig value={{cache}}>...</SWRConfig>
} This can help build the mental modal that the cache provider is a singleton. And the returned object can also contain more APIs in the future which is more flexible. Generally I'm -1 on the idea of letting users manipulating the internal cache directly (the current impl.). It can easily mess up the application state if done wrong. |
Beta Was this translation helpful? Give feedback.
-
Background
Currently swr cache (0.4.x by the time) is a kind of hidden functionality, which exposed one experimental
cache
but not really fully documented with guaranteed. swr will leverage cache to store the states of keys, andmutate
API can also manipulate states and cache at the same time.We received few issues/Q&A about how to manipulate the cache directly and make its value aligned with swr's state, which confuse users sometimes.
Goals
Proposal
Add a new property
cache
to swr context and let user be able to customize their cache. Fiting one cache API contract, and could be able to switch the underlayer providers like localstorage, RN AsyncStorage, any global state, etc. Note:cache
is not available for swr hookDiscussion
This is just a draft or an idea, won't be happened immediantely or break the current swr usage. Any comments or suggestions please let us hear from you. Thanks
Beta Was this translation helpful? Give feedback.
All reactions