-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add missing clear method to Cache type definition #1936
base: main
Are you sure you want to change the base?
Conversation
The Cache has been exposed since [1], which supposedly includes a .clear() method [2], which in turn is not actually present in the type definition [3]. This adds it to the type definition to align with the implementation. Since Map also contains a .clear() method [4], this shouldn't break the ability to pass that in as a map. [1] vercel#231 [2] vercel#161 (comment) [3] vercel#161 (comment) [4] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/clear
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 763c473:
|
We can just add this, but I'd love to hear more feedback about why there's a need to clear the cache (and why it can't be done via changing the cache provider). This is because manually updating the cache is dangerous and can lead into inconsistency in the UI, so we're very cautious here about it. |
@shuding Sure thing! In my case, it's because we log the user out by forgetting their auth tokens, after which future API requests fail. At that point, we want to make sure that no user data is known anymore. (Unfortunately, this is only one of the authentication methods, used in development; we don't always have a user-specific string that we can add to the cache keys.) Edit: oh, just noticed I missed the question about why it can't be done via changing the cache provider. I guess we could set up a custom cache provider and try to replace that with a new one on logout, but that's suddenly a globally-available object that we need to manage - I'm not sure I understand yet why that would be better than clearing the one we have. |
Unclear to me what the best approach is (replacing cache provider vs. a |
And if |
The case when we log in/out is very common for apps. And usually when we logged in we work with private endpoints which leads us to the state with private data cached and accessed by users even when they are not supposed to have it. And there are more cases, for example with 'policy' pattern based on data from endpoints. I'm fine with the idea to create and maintain global object for the app and pass it as custom provider, however as the case common for apps it would be great to have a way to do so out of the box. So I support these changes by @Vinnl as clean and robust way to do hard reset. |
Sometimes logout logic isn't in hooks so we need a static function/method to call. |
(Re)adding method to an interface is a breaking change, I suggest this should come in v2.0 |
The Cache has been exposed since [1], which supposedly includes a
.clear() method [2], which in turn is not actually present in the
type definition [3]. This adds it to the type definition to align
with the implementation.
Since Map also contains a .clear() method [4], this shouldn't break
the ability to pass that in as a map.
[1] #231
[2] #161 (comment) and https://swr.vercel.app/docs/advanced/cache#access-to-the-cache
[3] #161 (comment)
[4] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/clear