service_worker 0.0.16

Service Worker API for Dart

Dart-y wrappers for the ServiceWorker APIs.

Warning: the API is experimental, and subject to change.

Service Workers

A service worker is an event-driven worker registered against an origin and a path. It takes the form of a JavaScript file that can control the web page/site it is associated with, intercepting and modifying navigation and resource requests, and caching resources in a very granular fashion to give you complete control over how your app behaves in certain situations (the most obvious one being when the network is not available.)

A service worker is run in a worker context: it therefore has no DOM access, and runs on a different thread to the main JavaScript that powers your app, so it is not blocking. It is designed to be fully async; as a consequence, APIs such as synchronous XHR and localStorage can't be used inside a service worker.

Quickstart

Register the Service Worker from your application script, like in example/web/main.dart:

import 'package:service_worker/window.dart' as sw;

void main() {
  if (sw.isSupported) {
    sw.register('sw.dart.js');
  } else {
    print('ServiceWorkers are not supported.');
  }
}

Write the Service Worker in a separate script, like in example/web/sw.dart:

import 'package:service_worker/service_worker.dart';

void main(List<String> args) {
  onInstall.listen((event) {
    print('ServiceWorker installed.');
  });
}

Changelog

0.0.16

  • Remove 'implements' from JS facade methods, as it seems to break with dart2js.

0.0.15

  • Implement new methods for classes that implement Event.

0.0.14

  • Don't return ServiceWorker instances when the underlying JS object is null.
  • Mask types from JS facade, to prevent type check errors in Dartium.

0.0.13

  • expose Notification.close()
  • expose ServiceWorkerClientsMatchOptions

0.0.12

  • expose keys in PushSubscription

0.0.11

  • expose ServiceWorkerRegistration.jsObject
  • fix a few typing bug

0.0.10

  • export ShowNotificationAction

0.0.9

  • PushManager.hasPermission() is a deprecated API and it doesn't work in Chrome anymore, clients should use permissionState() instead.

0.0.8

  • ServiceWorkerClient.postMessage() to use List as transfer objects parameter.
  • ServiceWorker.postMessage(): any Transferable object can be set, not only MessagePorts.

0.0.7

  • removed Worker interface from JS facade, solves Dartium issue

0.0.1 - 0.0.6

0.0.6

  • fixed Cache.keys()

0.0.5

  • add support for WorkerLocation

0.0.4

  • fetch() with RequestInit
  • support for Headers cloning

0.0.3

  • fix Cache.put() signature

0.0.2

  • top-level fields and methods

1. Depend on it

Add this to your package's pubspec.yaml file:

dependencies:
  service_worker: "^0.0.16"

2. Install it

You can install packages from the command line:

$ pub get

Alternatively, your editor might support 'pub get'. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:service_worker/service_worker.dart';

Platforms

Web

About

Service Worker API

Author

Email istvan.soos@gmail.com Istvan Soos

Homepage

github.com/isoos/service_worker

Documentation

www.dartdocs.org/documentation/service_worker/0.0.16/

Source code (hyperlinked)

www.crossdart.info/p/service_worker/0.0.16/

Uploader

istvan.soos@agilord.com

License

BSD

Published

Jul 1, 2017

Share