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

@spoosh/plugin-invalidation

v0.11.0

Published

Cache invalidation plugin for Spoosh - auto-invalidates after mutations

Downloads

263

Readme

@spoosh/plugin-invalidation

Cache invalidation plugin for Spoosh - auto-invalidates related queries after mutations using wildcard patterns.

Documentation · Requirements: TypeScript >= 5.0 · Peer Dependencies: @spoosh/core

Installation

npm install @spoosh/plugin-invalidation

How It Works

Tags are automatically generated from the API path:

useRead((api) => api("posts").GET());
// → tag: "posts"

useRead((api) => api("posts/:id").GET({ params: { id: 123 } }));
// → tag: "posts/123"

useRead((api) => api("posts/:id/comments").GET({ params: { id: 123 } }));
// → tag: "posts/123/comments"

When a mutation succeeds, related queries are automatically invalidated using wildcard patterns:

const { trigger } = useWrite((api) => api("posts/:id/comments").POST());
await trigger({ params: { id: 123 }, body: { text: "Hello" } });

// Default behavior (autoInvalidate: true):
// Invalidates: ["posts", "posts/*"]
// ✓ Matches: "posts", "posts/123", "posts/123/comments", etc.

Usage

import { Spoosh } from "@spoosh/core";
import { invalidationPlugin } from "@spoosh/plugin-invalidation";

const spoosh = new Spoosh<ApiSchema, Error>("/api").use([invalidationPlugin()]);

const { trigger } = useWrite((api) => api("posts").POST());
await trigger({ body: { title: "New Post" } });

Pattern Matching

| Pattern | Matches | Does NOT Match | | ---------------------- | --------------------------------- | ---------------------- | | "posts" | "posts" (exact) | "posts/1", "users" | | "posts/*" | "posts/1", "posts/1/comments" | "posts" (parent) | | ["posts", "posts/*"] | "posts" AND all children | - |

Per-Request Invalidation

// Exact match only
await trigger({
  body: { title: "New Post" },
  invalidate: "posts",
});

// Children only (not the parent)
await trigger({
  body: { title: "New Post" },
  invalidate: "posts/*",
});

// Parent AND all children
await trigger({
  body: { title: "New Post" },
  invalidate: ["posts", "posts/*"],
});

// Multiple patterns
await trigger({
  body: { title: "New Post" },
  invalidate: ["posts", "users/*", "dashboard"],
});

// Disable invalidation for this mutation
await trigger({
  body: { title: "New Post" },
  invalidate: false,
});

// Global refetch - triggers ALL queries to refetch
await trigger({
  body: { title: "New Post" },
  invalidate: "*",
});

Options

Plugin Config

| Option | Type | Default | Description | | ---------------- | ---------- | ------- | ---------------------------------------------- | | autoInvalidate | boolean | true | Auto-generate invalidation patterns from path | | groups | string[] | [] | Path prefixes that use deeper segment matching |

// Default: auto-invalidate using [firstSegment, firstSegment/*]
invalidationPlugin(); // same as { autoInvalidate: true }

// Disable auto-invalidation (manual only)
invalidationPlugin({ autoInvalidate: false });

// Groups: use deeper segment matching for grouped endpoints
invalidationPlugin({
  groups: ["admin", "api/v1"],
});

Groups Configuration

Use groups when you have path prefixes that should be treated as a namespace:

invalidationPlugin({
  groups: ["admin", "api/v1"],
});

// Without groups:
// POST admin/posts → invalidates ["admin", "admin/*"]

// With groups: ["admin"]:
// POST admin/posts → invalidates ["admin/posts", "admin/posts/*"]
// POST admin/users → invalidates ["admin/users", "admin/users/*"]
// POST admin → invalidates ["admin", "admin/*"]

// With groups: ["api/v1"]:
// POST api/v1/users → invalidates ["api/v1/users", "api/v1/users/*"]

Per-Request Options

| Option | Type | Description | | ------------ | ------------------------------------ | ------------------------------------------------------------------------- | | invalidate | string \| string[] \| false \| "*" | Pattern(s) to invalidate, false to disable, or "*" for global refetch |

Default Behavior

When autoInvalidate: true (default) and no invalidate option is provided:

// POST /posts/123/comments
// → Invalidates: ["posts", "posts/*"]

// The first path segment is used to generate patterns:
// - "posts" - exact match for the root
// - "posts/*" - all children under posts

Instance API

The plugin exposes invalidate for manual cache invalidation:

import { create } from "@spoosh/react";

const { useRead, invalidate } = create(spoosh);

// Single pattern
invalidate("posts");

// Multiple patterns
invalidate(["posts", "users/*"]);

// Global refetch
invalidate("*");

// Useful for external events
socket.on("posts-updated", () => {
  invalidate(["posts", "posts/*"]);
});

socket.on("full-sync", () => {
  invalidate("*");
});

Combining with Cache Plugin

For scenarios like logout, combine with clearCache from @spoosh/plugin-cache:

const { trigger } = useWrite((api) => api("auth/logout").POST());

await trigger({
  clearCache: true, // Clear all cached data
  invalidate: "*", // Trigger all queries to refetch
});