swr icon indicating copy to clipboard operation
swr copied to clipboard

Add missing clear method to Cache type definition

Open Vinnl opened this issue 4 years ago • 8 comments

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] https://github.com/vercel/swr/pull/231 [2] https://github.com/vercel/swr/issues/161#issuecomment-695417107 and https://swr.vercel.app/docs/advanced/cache#access-to-the-cache [3] https://github.com/vercel/swr/issues/161#issuecomment-1079198998 [4] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/clear

Vinnl avatar Apr 20 '22 11:04 Vinnl

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 763c473ac4ee484cc72fde65cb6ed2ae7ab8301e:

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

codesandbox-ci[bot] avatar Apr 20 '22 11:04 codesandbox-ci[bot]

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 avatar Apr 20 '22 11:04 shuding

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

Vinnl avatar Apr 20 '22 11:04 Vinnl

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 avatar Apr 25 '22 16:04 babldev

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

babldev avatar Apr 25 '22 16:04 babldev

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.

ezhlobo avatar May 03 '22 00:05 ezhlobo

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

beqramo avatar Jun 28 '22 14:06 beqramo

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

piotr-cz avatar Oct 18 '22 13:10 piotr-cz