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

service-worker-brotli

v0.1.1

Published

A service worker to fetch brotli precompressed assets from CDNs that don't support it

Readme

service-worker-brotli

A service worker to fetch brotli precompressed assets from CDNs that don't support it.

The service worker implements a network-first strategy for text/html assets, and a cache-first strategy for all other assets.

See a live demo.

Motivation

In many CDNs you can upload a precompressed gzip versions of a file. They will then serve the compressed file if the client supports it. This saves bandwidth and time.

Most browsers nowadays also support brotli compression which results in even smaller files. However, not many CDNs support serving precompressed brotli files.

This library implements a service worker to workaround that issue.

Quick start

Install the package

npm install service-worker-brotli

Create a service-worker.js file for your service worker

import "service-worker-brotli";

Finally, register the worker in your index.js

import "service-worker-brotli/register.js";

navigator.serviceWorker.register(new URL("./service-worker.js", import.meta.url), { type: "module" });

// the rest of your code

See the demo folder for an example for webpack and parcel.

Service worker registration

By default a service worker will not control the page after a hard refresh and all fetches will go straight to the network. That means requesting potentially large uncompressed asset files from the server. Importing service-worker-brotli/register.js patches navigator.serviceWorker.register to allow the service worker to control the page even after a hard refresh.

Other use cases

The default service-worker-brotli import implements a network-first strategy for text/html assets, and a cache-first strategy for all other assets. It implements no pre-caching.

This works nicely with bundlers where the html entry points are not hashed, while the rest of the assets are hashed.

If you need more control over the service worker or you already have a service worker, you can import service-worker-brotli/utils.js which provides the makeRequest and makeResponse functions.

For example, if you are using Workbox you can create a plugin to handle the compression

import { registerRoute } from 'workbox-routing';
import { CacheFirst } from 'workbox-strategies';
import { makeRequest, makeResponse } from "service-worker-brotli/utils.js";

class BrotliPlugin {
    requestWillFetch({ request }) {
        // Make a request to the brotli compressed asset.
        return makeRequest(request);
    }

    fetchDidSucceed({ response }) {
        if (!response.ok) return response;

        // Make a response that:
        // * decompresses the asset on the fly.
        // * fixes the Content-Type header based on
        //   the asset extension.
        return makeResponse(response);
    }
};

registerRoute(
    new RegExp("/assets/.*"),
    new CacheFirst({ plugins: [
        new BrotliPlugin()
    ] })
);