Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Vinnl
Copy link

@Vinnl Vinnl commented Apr 20, 2022

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

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
@codesandbox-ci
Copy link

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:

Sandbox Source
SWR-Basic Configuration
SWR-States Configuration
SWR-Infinite Configuration
SWR-SSR Configuration

@shuding
Copy link
Member

shuding commented Apr 20, 2022

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.

@Vinnl
Copy link
Author

Vinnl commented Apr 20, 2022

@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.

@huozhi huozhi mentioned this pull request Apr 20, 2022
@babldev
Copy link

babldev commented Apr 25, 2022

Unclear to me what the best approach is (replacing cache provider vs. a .clear()), but I think it's worth having some sort of documented path for "the user logged out so we want to clear cached responses." Happy to help write-up docs for that for the Cache page if that is the agreed upon best practice.

@babldev
Copy link

babldev commented Apr 25, 2022

And if .clear() is not recommended we should delete the example using it here https://swr.vercel.app/docs/advanced/cache#access-to-the-cache

@ezhlobo
Copy link

ezhlobo commented May 3, 2022

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.

@beqramo
Copy link

beqramo commented Jun 28, 2022

Sometimes logout logic isn't in hooks so we need a static function/method to call.

@piotr-cz
Copy link

(Re)adding method to an interface is a breaking change, I suggest this should come in v2.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants