npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

nextjs-cache-tags

v0.1.0

Published

Type-safe cache tag definitions for Next.js.

Readme

next-cachetags

Type-safe cache tag definitions for Next.js 15+.

The drift problem

Next.js 15's 'use cache' directive lets you tag cached functions with cacheTag() and later invalidate those tags with revalidateTag(). The tag strings are free-form — which means teams immediately start drifting:

// In a cached function:
cacheTag(`user-${id}`)   // ← hyphen

// In a server action (written by a different person, weeks later):
revalidateTag(`user:${id}`)  // ← colon. Cache never invalidates.

The bug is silent and hard to catch in code review. next-cachetags solves this by coupling both sides to a single typed function.

Install

npm install next-cachetags

Peer dependency: next >= 15

Quickstart

// tags.ts
import { defineTag } from 'next-cachetags'

export const userTag = defineTag('user', (id: string) => `user:${id}`)
export const productTag = defineTag(
  'product',
  (id: string, locale: string) => `product:${id}:${locale}`,
)
// In a cached server component or function:
import { userTag } from './tags'

async function getUser(id: string) {
  'use cache'
  await userTag.apply(id)   // calls unstable_cacheTag('user:<id>')
  return db.users.findFirst({ where: { id } })
}
// In a server action:
'use server'
import { userTag } from './tags'

export async function updateUser(id: string, data: UpdateUser) {
  await db.users.update({ where: { id }, data })
  await userTag.revalidate(id)  // calls revalidateTag('user:<id>')
}

Both sides call the same typed function. No string drift possible.

API

defineTag(name, keyFn)

Returns a Tag<Args> object where Args is inferred from keyFn.

type Tag<Args extends unknown[]> = {
  key: (...args: Args) => string           // compute the tag string
  apply: (...args: Args) => Promise<void>  // call unstable_cacheTag()
  revalidate: (...args: Args) => Promise<void>  // call revalidateTag()
}
  • key(...args) — pure function, returns the tag string. Useful for computing keys to pass to revalidateMany.
  • apply(...args) — calls unstable_cacheTag() from next/cache. Use inside a 'use cache' function.
  • revalidate(...args) — calls revalidateTag() from next/cache. Use in server actions or route handlers.

revalidateMany(keys)

Batch-invalidate multiple tags in one call.

import { revalidateMany } from 'next-cachetags'
import { userTag, productTag } from './tags'

await revalidateMany([
  userTag.key(userId),
  productTag.key(productId, locale),
])

Recipes

Composable tags

// Namespace all order-related tags under a single prefix:
const orderBase = defineTag('order', (id: string) => `order:${id}`)
const orderItemTag = defineTag('order-item', (orderId: string, itemId: string) =>
  `${orderBase.key(orderId)}:item:${itemId}`,
)

Multi-arg tags

const translationTag = defineTag(
  'translation',
  (namespace: string, locale: string) => `t:${namespace}:${locale}`,
)

// In a cached function:
await translationTag.apply('checkout', 'fr')

// In an action:
await translationTag.revalidate('checkout', 'fr')

Batch revalidation after a bulk update

import { revalidateMany } from 'next-cachetags'
import { userTag } from './tags'

export async function deleteUsers(ids: string[]) {
  await db.users.deleteMany({ where: { id: { in: ids } } })
  await revalidateMany(ids.map(userTag.key))
}

Why this exists

Next.js gives you the 'use cache' + cacheTag() + revalidateTag() primitives, but it doesn't prevent you from getting the tag string wrong. In a large codebase with many contributors, the tag convention (separator character, argument order, prefix) lives only in people's heads — until it doesn't. This package is the missing codification layer: one declaration, both sides typed.

A note on unstable_cacheTag

In Next.js 15.x, the public export from next/cache is named unstable_cacheTag (not cacheTag). This library calls unstable_cacheTag internally. When Next.js stabilizes the API, only the import name will change — your call sites using next-cachetags won't need to change at all.

License

MIT