-
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
Accept glob pattern or RegExp in trigger() #86
Comments
I just saw that #78 is kinda related. Feel free to close if it's the same thing! |
Yeah accepting glob pattern or RegExp is a good idea, but it will be unnecessary to add it as an option after giving you the full control of the cache. |
I think this could now be solved with the exposed cache, get all cache keys, filter based on a glob and then call mutate against them without data to revalidate them or call cache.delete to delete them altogether |
Hello! I think I see the point in not bloating the library with features that can be done by combining the existing feature set, but
Also it may not be desirable to rely on the cache for this: (this is just opinion) I think 'trigger' is s feature not directly related to the cache. As a library user I would like to be able to call 'trigger' without necessarily knowing how the cache works internally. |
I like your take @JulianG and this is also my opinion about why I personally don't want cache APIs to be documented for now. Because ideally they should be covered by some other high level APIs or plugins. It's dangerous for end users to touch the cache. (I'd close this PR, it's kinda unrelated to the topic) |
I ended up with this by the way: function revalidate(matcher: string | RegExp) {
cache
.keys()
.filter(key => key.match(matcher))
.forEach(key => mutate(key));
} |
Thanks a lot for releasing this library! I'm just starting to play around with it but it seems like it's quite a simplification over what we're currently doing in our apps to fetch data.
I was wondering about one thing though: After a successful POST request you commonly need to revalidate some related data. If I got it right, you either do that with the
revalidate()
function that is returned fromuseSWR()
or by callingtrigger()
with a key. The latter seems like a shortcut in cases where it'd be tedious to passrevalidate()
around between components. However, in cases like these chances are that I also don't know the exact key (maybe because there's a bunch of dynamic parameters in there).What do you think about also accepting something like a glob pattern or a
RegExp
as an argument totrigger()
and then revalidating all the matching (and currently visible) queries? That way you could dotrigger("/projects*")
ortrigger(/\/projects.*/)
instead of the more specifictrigger("/projects?page=3&perPage=100")
.(I haven't checked out the implementation of SWR so please let me know if I have incorrect assumptions about the internals!)
The text was updated successfully, but these errors were encountered: