Skip to content

Add cache tag revalidation support - #4336

Merged
huozhi merged 4 commits into
mainfrom
tags
Sep 22, 2026
Merged

huozhi merged 4 commits into
mainfrom
tags

Conversation

@WITS

@WITS WITS commented Sep 18, 2026 •

Copy link
Copy Markdown
Member

This adds an option to the config for useSWR() so that tags can be passed. These tags make it easier to invalidate a batch of keys based off of a single, low-entropy string (e.g. projects/prj_fdjklsfdjslk) instead of needing to know every permutation of key to invalidate (e.g. /api/v1/projects?id=prj_fdjklsfdjslk, /api/v2/projects?id=prj_fdjklsfdjslk, /api/v2/projects?id=prj_fdjklsfdjslk&includeMetadata=1, etc.)

The basic usage is

function MyComponent() {
  const { data: foo1 } = useSWR('/foo?variant=1', fetcher, {
    tags: ['foo']
  });
  const { data: foo2 } = useSWR('/foo?variant=2', fetcher, {
    tags: ['foo']
  });
}

const revalidateFoo = () => {
  revalidateTag('foo');
}

Summary

  • Add configurable tags for SWR keys, including settlement-time tag resolvers
  • Expose global and provider-scoped revalidateTag APIs
  • Track tag associations per cache and clear them on unload
  • Add comprehensive runtime and type coverage

Testing

  • Added tests for tag revalidation, provider scoping, tag updates, rejected requests, unload behavior, and type definitions

@WITS WITS changed the title Add cache tag invalidation support Add cache tag revalidation support Sep 18, 2026
@WITS
WITS marked this pull request as ready for review September 22, 2026 10:59
@WITS
WITS requested a review from shuding as a code owner September 22, 2026 10:59
@huozhi
huozhi merged commit 21e826a into main Sep 22, 2026
9 checks passed
@huozhi
huozhi deleted the tags branch September 22, 2026 11:32
@omeid

omeid commented Nov 27, 2026 •

Copy link
Copy Markdown

This is a great idea!

I am just wondering if this could be handled without complicating the core swr? Wouldn't something like this allow more control with less code?

function MyComponent() {
  const { data: foo1 } = useSWR({ url: '/foo?variant=1', tags: ['foo' ], fetcher);
  const { data: foo2 } = useSWR({ url: '/foo?variant=2', tags: ['foo' ], fetcher);
}

const revalidateFoo = () => {
  unload({ revalidate: true, match: key => key?.tags?.includes('foo') })
}

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.

3 participants