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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ayco/astro-sw

v0.8.8

Published

Use your own authored service worker with Astro

Downloads

107

Readme

Astro SW

Package information: NPM version Package information: NPM license

Use your own authored service worker with Astro.

The integration accepts the path to your service worker and automatically injects dynamic variables such as __assets generated by Astro for caching.

It works on all Astro output options: static, server, or hybrid, and lets developers retain the flexibility for various caching strategies.

Installation

In your Astro project:

# if using npm
$ npm i -D @ayco/astro-sw

# if using pnpm
$ pnpm add -D @ayco/astro-sw

Minimal Usage

Here's an example astro.config.mjs file:

import { defineConfig } from "astro/config";
import serviceWorker from "@ayco/astro-sw";

export default defineConfig({
  integrations: [
    serviceWorker({
      path: "./src/sw.ts",
    })
  ]
});

For more options available, see the API.

TypeScript support

We use esbuild to resolve service worker imports and build TS files! You can customize the build options by providing it to the esbuild configuration property.

import { defineConfig } from "astro/config";
import serviceWorker from "@ayco/astro-sw";

export default defineConfig({
  integrations: [
    serviceWorker({
      path: "./src/sw.ts",
      esbuild: {
        minify: true
      }
    })
  ]
});

Injected variables

The most important variable your service worker will have access to is __assets, which contains all routes and public assets that Astro includes in your build. Additionally, you will also get __prefix and __version you can use for naming & invalidating your Cache storage (useful for debugging purposes).

Registration Hooks

Hooks are provided for adding custom logic that triggers in various service worker registration events.

The following properties are available for the registrationHooks configuration:

  1. installing - when the registration is 'installing'
  2. waiting - when the registration is 'waiting'
  3. active - when the registration is 'active'
  4. error - when the registration throws an error
  5. unsupported - when the service workers are unsupported
  6. afterRegistration - after the registration succeeds
import { defineConfig } from "astro/config";
import serviceWorker from "@ayco/astro-sw";

export default defineConfig({
  integrations: [
    serviceWorker({
      path: "./src/sw.ts",
      registrationHooks: {
        afterRegistration: async () => {
            const sw = await navigator.serviceWorker.getRegistration();
            console.log('>>> registrered', sw)
        },
        installing: () => console.log('installing...'),
        waiting: () => console.log('waiting...'),
        active: () => console.log('active...'),
        error: (error) => console.error(error),
        unsupported: () => console.log(':('),
      }
    })
  ]
});

API

The integration accepts a configuration object with the following properties

| property | type | required? | notes | | --- | --- | --- | --- | | path | string | required | path to your own service worker script; no surprises & easy debugging | | assetCachePrefix | string | optional | cache storage name prefix | | assetCacheVersionID | string | optional | cache storage name versioning; by default, a random UUID is used | | customRoutes | string[] | optional | list of custom routes you want to be cached. Beware that non-existent routes that result to HTTP Error404 will cause the service worker to fail | | excludeRoutes | string[] | optional | list of routes you want to be ignored/removed from assets | | logAssets | boolean | optional | set to see a list of the assets found; defaults to false | | esbuild | BuildOptions | optional | custom build options for your service worker script | | registrationHooks | object | optional | provide callbacks for various registration events; see section on Registration Hooks |

Background

This integration was developed to support the Caching strategy needs of Cozy -- the modern reading companion for the Web. You can find an example service worker in the repository.