No description
Find a file
2024-08-03 10:40:39 +02:00
.gitignore initial commit 2024-08-03 08:55:17 +02:00
example_sw.js chore: update comment in example sw 2024-08-03 10:31:50 +02:00
index.js feat: fix imported typedef for JSdocs 2024-08-03 09:11:38 +02:00
package.json 0.0.5 2024-08-03 10:40:39 +02:00
pnpm-lock.yaml initial commit 2024-08-03 08:55:17 +02:00
README.md chore: update readme 2024-08-03 10:40:29 +02:00

>>> TL;DR: Simple Astro integration to use your own authored service worker; by default, devs retain full control as opposed to getting generated sw code

Astro SW

The integration serviceWorker accepts configuration with service worker path and automatically injects needed dynamic variables such as assets generated by Astro for caching

Installation

In your Astro project:

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

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

Usage

Example astro.config.mjs

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

export default defineConfig({
  output: "server",
  adapter: node({
    mode: "middleware"
  }),
  integrations: [
    serviceWorker({
      path: "./src/sw.js",
      assetCachePrefix: 'cozy-reader',
    })
  ]
});

API

The integration accepts a configuration object of type ServiceWorkerConfig 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; useful for debugging
assetCacheVersionID string optional cache storage name versioning; by default, a random UUID is used but you can provide your own for easy debugging & invalidation

Example sw.js

You can find an example service worker (example_sw.js) in the repository, and here's the raw file too.