New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

vite-plugin-cache

Package Overview
Dependencies
Maintainers
1
Versions
41
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vite-plugin-cache

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

latest
Source
npmnpm
Version
1.4.8
Version published
Maintainers
1
Created
Source

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

StrategyDescription
stale-while-revalidateReturns cached response immediately, updates in background
network-firstTries network first, fallback to cache
cache-firstTries cache first, fallback to network
network-onlyAlways fetches from network
cache-onlyOnly 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

Keywords

vite

FAQs

Package last updated on 15 Jun 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts