0.8.8 • Published 11 months ago

@ayco/astro-sw v0.8.8

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

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' 1. waiting - when the registration is 'waiting' 1. active - when the registration is 'active' 1. error - when the registration throws an error 1. unsupported - when the service workers are unsupported 1. 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

propertytyperequired?notes
pathstringrequiredpath to your own service worker script; no surprises & easy debugging
assetCachePrefixstringoptionalcache storage name prefix
assetCacheVersionIDstringoptionalcache storage name versioning; by default, a random UUID is used
customRoutesstring[]optionallist 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
excludeRoutesstring[]optionallist of routes you want to be ignored/removed from assets
logAssetsbooleanoptionalset to see a list of the assets found; defaults to false
esbuildBuildOptionsoptionalcustom build options for your service worker script
registrationHooksobjectoptionalprovide 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.

0.8.8

11 months ago

0.8.7

11 months ago

0.8.6

11 months ago

0.8.5

11 months ago

0.8.4

11 months ago

0.8.3

11 months ago

0.8.2

11 months ago

0.8.1

11 months ago

0.8.0

11 months ago

0.7.10

11 months ago

0.7.9

11 months ago

0.7.8

11 months ago

0.7.7

11 months ago

0.7.6

11 months ago

0.7.5

11 months ago

0.7.4

11 months ago

0.7.3

11 months ago

0.7.2

11 months ago

0.7.1

11 months ago

0.7.0

11 months ago

0.6.3

11 months ago

0.6.2

11 months ago

0.6.1

11 months ago

0.6.0

11 months ago

0.5.1

11 months ago

0.5.0

11 months ago

0.4.0

11 months ago

0.3.1

11 months ago

0.3.0

11 months ago

0.2.2

11 months ago

0.2.1

11 months ago

0.2.0

11 months ago

0.1.1

11 months ago

0.1.0

11 months ago

0.0.10

11 months ago

0.0.9

11 months ago

0.0.8

12 months ago

0.0.7

12 months ago

0.0.6

12 months ago

0.0.5

12 months ago

0.0.4

12 months ago

0.0.3

12 months ago

0.0.2

12 months ago

0.0.1

12 months ago