Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/sladage/dart-service-worker

Service Worker library for Dart.
https://github.com/sladage/dart-service-worker

Last synced: about 2 months ago
JSON representation

Service Worker library for Dart.

Awesome Lists containing this project

README

        

# Dart Service Worker library

Write Web Apps using Dart with Service Worker support. (Currently only supports compilation to javascript)

## Usage

Landing page:

```
import 'package:dart_service_worker/serviceworkermanager.dart' as SW;

main() async {
try {
SW.ServiceWorker sw = await SW.serviceWorkerManager.register("sw.js");
print("registered");
sw.onMessage.listen((SW.MessageEvent e){
print(e.data);
});
} catch(e) {
print(e);
}
}
```

Service worker:

```
void main() {
ServiceWorker sw = new ServiceWorker();
sw.onInstall.listen((e){
print("install");
//setup cache
});

sw.onActivate.listen((e){
print("activate");
});

sw.onMessage.listen((MessageEvent e){
print("msg:"+e.data);
});

sw.onFetch.listen((FetchEvent e){
print("fetch");
e.respondWith(getReponse(e.request));
});
}

Future getReponse(Request r) async {
Response response = await caches.match(r);
if (response != null) {
return response;
}
return fetch(request:r.clone());
}
```