question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Accept glob pattern or RegExp in trigger()

See original GitHub issue

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 from useSWR() or by calling trigger() with a key. The latter seems like a shortcut in cases where it’d be tedious to pass revalidate() 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 to trigger() and then revalidating all the matching (and currently visible) queries? That way you could do trigger("/projects*") or trigger(/\/projects.*/) instead of the more specific trigger("/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!)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
nicolasschabramcommented, Oct 1, 2020

I ended up with this by the way:

function revalidate(matcher: string | RegExp) {
  cache
    .keys()
    .filter(key => key.match(matcher))
    .forEach(key => mutate(key));
}
1reaction
shudingcommented, Sep 20, 2020

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)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why doesn't my github action trigger on a regex tag
So a normal regex would not work. Your expression is close enough of a glob pattern: I would try and not escape the...
Read more >
Regex vs Globbing | Differences and Similarities - Dillion's Blog
Regex and Glob patterns are similar ways of matching patterns in strings. The main difference is that the regex pattern matches strings in...
Read more >
glob2rx: Change Wildcard or Globbing Pattern into Regular ...
A character vector of the same length as the input pattern where each wildcard is translated to the corresponding regular expression. Author(s). Martin...
Read more >
glob - Greg's Wiki
Matches anything except one of the given patterns. Patterns in a list are separated by | characters. Extended globs allow you to solve...
Read more >
target` to accept globs · Issue #2182 · hashicorp/terraform
Suppose one declares several concurrent environments in a single tf config. To limit cli actions to a single environment at a time, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found