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 🙏

© 2025 – Pkg Stats / Ryan Hefner

vite-plugin-cache

v1.4.8

Published

Zero-config Vite plugin to add Workbox-based service worker with stale-while-revalidate caching.

Readme

vite-plugin-cache

A Vite plugin that auto-generates and registers a Workbox-based service worker to cache your API requests and static assets.

✨ Features

  • 🔧 Auto-generates a service worker using Workbox at build time
  • 🧠 Built-in support for common Workbox strategies:
    • stale-while-revalidate
    • cache-first
    • network-first
    • cache-only
    • network-only
  • 🧩 Supports Workbox plugins like ExpirationPlugin
  • 📚 Built-in Workbox recipes: imageCache, pageCache, staticResourceCache, googleFontsCache
  • ⚡ Auto-injects service worker registration into your HTML

🚀 Installation

npm install vite-plugin-cache --save-dev

⚙️ Basic Usage

// vite.config.ts
import { defineConfig } from 'vite';
import { vitePluginCache } from './vite-plugin-cache';

export default defineConfig({
  plugins: [vitePluginCache()],
});

This will use the default configuration:

  • Caches all GET requests to /api/* using stale-while-revalidate
  • Applies ExpirationPlugin with 100 entries and 60 seconds age
  • Injects service worker loader in index.html

🧠 Advanced Usage

Custom Cache Config

You can override the default caching rules with your own:

vitePluginCache({
  config: {
    'custom-api-cache': {
      match: ({ url }) => url.pathname.startsWith('/v1/'),
      strategy: 'network-first',
      plugins: {
        expiration: {
          maxEntries: 50,
          maxAgeSeconds: 120,
        },
      },
    },
  },
});

Using Built-in Recipes

Workbox recipes simplify common patterns:

vitePluginCache({
  recipies: {
    imageCache: {},
    googleFontsCache: {},
    pageCache: null,
  },
});

Function-based Config

You can dynamically generate config:

vitePluginCache({
  config: (defaultConfig) => ({
    ...defaultConfig,
    'docs-cache': {
      match: ({ url }) => url.pathname.startsWith('/docs/'),
      strategy: 'cache-first',
    },
  }),
});

🔌 Supported Strategies

| Strategy | Description | |--------------------------|-------------------------------------------------------| | stale-while-revalidate | Returns cached response immediately, updates in background | | network-first | Tries network first, fallback to cache | | cache-first | Tries cache first, fallback to network | | network-only | Always fetches from network | | cache-only | Only uses the cache |


🧩 Plugin Support

Currently supported:

  • expiration: Uses ExpirationPlugin to limit cache size and entry age.
plugins: {
  expiration: {
    maxEntries: 200,
    maxAgeSeconds: 3600,
  },
}

📦 Output

The generated service worker will be placed in your build output (e.g., dist/vite-plugin-cache-service-worker.js) and automatically registered in the browser.


📝 License

MIT