Skip to content

Clearing cache of all data where the key array starts with specific elements #525

Answered by sergiodxa
adamsvystun asked this question in General
Discussion options

You must be logged in to vote

I built swr-mutate-many for this use-case, what the lib does is:

import { cache } from "swr";
cache.keys().filter(key => key.startsWith(cache.serializeKey(["/api/todos"]).forEach(key => cache.delete(key))

Something like this

The serializeKey is required because when using an array as key the final string used to store the data is something like arg@"/api/todos" so you need that arg@ part at the beginning.

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@adamsvystun
Comment options

@ricardo-rp
Comment options

Answer selected by adamsvystun
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants