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-inject-sw-assets

v1.0.2

Published

A Vite plugin that injects static assets into a custom service worker for use with injectManifest (ideal for Workbox + vite-plugin-pwa setups).

Downloads

12

Readme

EN | FR

BrowserUX Inject SW Assets

A Vite plugin that automatically injects static assets into a custom service worker (injectManifest) for full offline support.

vite-plugin-inject-sw-assets is a Vite plugin that injects additional static files into your service worker's precache list. It complements vite-plugin-pwa when using the injectManifest strategy by ensuring that files like images, fonts, or JSON used only in HTML are properly cached.

npm version vite compatibility

Why this plugin?

When using vite-plugin-pwa with the injectManifest strategy, the service worker is fully customized. This means the developer is responsible for manually declaring which files should be precached. While Workbox automatically injects JS, CSS, and HTML files referenced by Vite, it does not include static assets such as images, fonts, or JSON files that are used only in HTML.

This leads to a common issue:

Images referenced directly in your HTML files (e.g. <img src="...">) are not precached automatically.

What this plugin does

  • 🔍 Automatically scans the dist/ output folder for static files at the end of the build
  • 🖼️ Detects all files matching configurable extensions (default: .png, .svg, .ico, .webp, .json)
  • 🧼 Skips files already listed in your manifest.webmanifest (e.g. PWA icons)
  • ⚙️ Generates a JavaScript file that can be imported into your custom service worker

Benefits

  • Automates a commonly forgotten step when using injectManifest
  • Prevents 404 errors when offline for HTML-embedded images
  • Compatible with all Vite + PWA workflows
  • Easily customizable (extensions, excludeFromManifest, etc.)

Installation

npm install vite-plugin-inject-sw-assets --save-dev

Usage

In your vite.config.ts file

import { defineConfig } from 'vite'
import { VitePWA } from 'vite-plugin-pwa'
import { globSync } from 'glob';
import injectSWAssets from 'vite-plugin-inject-sw-assets'

export default defineConfig({
    plugins: [
        injectSWAssets({
            extensions: ['png', 'svg', 'json'] // optional
        }),
        VitePWA({
            strategies: 'injectManifest',
            srcDir: 'src',
            filename: 'sw.js',
            manifest: {
                icons: [/* ... */]
            }
        })
    ]
})

Integration in the Service Worker (sw.js)

import { precacheAndRoute } from 'workbox-precaching'

// Load injected assets (generated by the plugin)
importScripts('sw-assets.js') 

// Files automatically injected by Workbox
precacheAndRoute(self.__WB_MANIFEST)

// Files injected by the plugin
if (self.__INJECTED_ASSETS__) {
    precacheAndRoute(self.__INJECTED_ASSETS__)
}

Path considerations

  • The path passed to importScripts() is relative to the service worker file. So if both sw.js and sw-assets.js are in dist/, just use:
importScripts('sw-assets.js') 
  • If sw.js is inside a subdirectory like dist/src/, then use:
importScripts('../sw-assets.js')

Exemple de fichier généré

File dist/sw-assets.js:

self.__INJECTED_ASSETS__ = [
    { url: "/images/logo.png", revision: null },
    { url: "/data/data.json", revision: null }
]

Options

| Option | Type | Default | Description | | --------------------- | ---------- | ----------------------------------------------------- | --------------------------------------------------------------------------------------------- | | distDir | string | "dist" | Build output directory to scan for static files to inject | | output | string | "sw-assets.js" | Name of the generated JavaScript file containing the injected assets | | extensions | string[] | ['png', 'jpg', 'jpeg', 'svg', 'webp', 'ico', 'json'] | File extensions to include in the injection (e.g. images, JSON) | | excludeFromManifest | boolean | true | Automatically excludes files already listed in manifest.webmanifest (e.g. PWA icons) |

Project structure

├── dist/
├── src/
│   ├── index.ts
│   ├── types.ts
│   └── utils/
│   │   ├── file-scanner.ts
│   │   ├── manifest-utils.ts
│   │   └── sw-assets-writer.ts
├── tsconfig.json
├── package.json
├── README.md

How it works (in short)

  • Recursively scans the dist/ folder
  • Filters files by extension (configurable)
  • Optionally excludes icons already listed in manifest.webmanifest
  • Generates a JS file (sw-assets.js) to be imported in your sw.js

License

MIT © 2025 Effeilo