Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/pnsk-lab/vite-sw

A plugin for writing Service Worker on Vite
https://github.com/pnsk-lab/vite-sw

service-worker vite

Last synced: 2 months ago
JSON representation

A plugin for writing Service Worker on Vite

Awesome Lists containing this project

README

        

# vite-sw
vite-sw is a plugin for writing Service Worker on Vite.

### Setting up
To install:
```shell
npm i -D vite-sw
yarn add -D vite-sw
pnpm add -D vite-sw
bun add -D vite-sw
```

Add plugin to `vite.config.ts`:
```ts
import { defineConfig } from 'vite'
import { pluginSW } from './lib'

export default defineConfig({
plugins: [pluginSW()],
})
```

Add types to `vite-env.d.ts`:
```ts
// ...

///

// ...
```

## Usage

```ts
// src/sw.ts
///
declare const self: ServiceWorkerGlobalScope

self.addEventListener('fetch', () => {
// Write code here
})
self.addEventListener('install', () => {
self.skipWaiting()
})
```

And import it:
```ts
import regester from './sw?sw'

const regestered: ServiceWorkerRegistration = await regester({
scope: '/'
})
```