No description
Find a file
2024-08-17 09:40:29 +02:00
public chore: add public image 2024-08-15 18:46:00 +02:00
src feat: use esbuild to resolve imports & support typescript 2024-08-17 09:40:29 +02:00
.gitignore initial commit 2024-08-03 08:55:17 +02:00
astro.config.mjs feat: use esbuild to resolve imports & support typescript 2024-08-17 09:40:29 +02:00
example_sw.ts feat: use esbuild to resolve imports & support typescript 2024-08-17 09:40:29 +02:00
index.js feat: use esbuild to resolve imports & support typescript 2024-08-17 09:40:29 +02:00
package.json feat: use esbuild to resolve imports & support typescript 2024-08-17 09:40:29 +02:00
pnpm-lock.yaml feat: use esbuild to resolve imports & support typescript 2024-08-17 09:40:29 +02:00
README.md chore: update readme 2024-08-16 20:55:33 +02:00

Astro SW

Package information: NPM version Package information: NPM license Bundle Size

The integration accepts path to your own authored service worker and automatically injects dynamic variables such as __assets generated by Astro for caching. The goal is to let devs retain the flexibility for various caching strategies.

Other integrations exist which will generate a service worker for you, but there are good reasons to avoid this.

Installation

In your Astro project:

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

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

Usage

Example astro.config.mjs

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

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

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
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

Example sw.js

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

Example project

This was developed to support the needs of Cozy.