service_worker 0.2.1

  • Installing
  • Versions
  • 84

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.


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) {
  } 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.');



  • Fixed a use of dynamic as bottom by adding type parameters to futureToPromise.


  • Updated to work with dart:html for 2.0.0-dev.40.0:
    • ExtendableEvent, FetchEvent removes deepPath or scoped members.
    • Added composed and composedPath members.
  • Migrate off deprecated APIs from the core libraries.
  • Fixed a strong-mode cast warning that will become an error soon.


  • Style cleanup: preferring single quotes.
  • Removed generic type comments.


  • Remove package:func dependency.


  • Upgraded dependencies.


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


  • Implement new methods for classes that implement Event.


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


  • expose Notification.close()
  • expose ServiceWorkerClientsMatchOptions


  • expose keys in PushSubscription


  • expose ServiceWorkerRegistration.jsObject
  • fix a few typing bug


  • export ShowNotificationAction


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


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


  • removed Worker interface from JS facade, solves Dartium issue

0.0.1 - 0.0.6


  • fixed Cache.keys()


  • add support for WorkerLocation


  • fetch() with RequestInit
  • support for Headers cloning


  • fix Cache.put() signature


  • top-level fields and methods

Use this package as a library

1. Depend on it

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

  service_worker: "^0.2.1"

2. Install it

You can install packages from the command line:

with pub:

$ 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';
Version Uploaded Documentation Archive
0.2.1 Apr 19, 2018 Go to the documentation of service_worker 0.2.1 Download service_worker 0.2.1 archive
0.2.0 Mar 21, 2018 Go to the documentation of service_worker 0.2.0 Download service_worker 0.2.0 archive
0.1.2 Feb 5, 2018 Go to the documentation of service_worker 0.1.2 Download service_worker 0.1.2 archive
0.1.1 Jan 10, 2018 Go to the documentation of service_worker 0.1.1 Download service_worker 0.1.1 archive
0.1.0 Sep 29, 2017 Go to the documentation of service_worker 0.1.0 Download service_worker 0.1.0 archive
0.0.16 Jul 1, 2017 Go to the documentation of service_worker 0.0.16 Download service_worker 0.0.16 archive
0.0.15 Jun 15, 2017 Go to the documentation of service_worker 0.0.15 Download service_worker 0.0.15 archive
0.0.14 May 11, 2017 Go to the documentation of service_worker 0.0.14 Download service_worker 0.0.14 archive
0.0.13 May 5, 2017 Go to the documentation of service_worker 0.0.13 Download service_worker 0.0.13 archive
0.0.12 Apr 26, 2017 Go to the documentation of service_worker 0.0.12 Download service_worker 0.0.12 archive

All 21 versions...


We analyzed this package on Jun 19, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0-dev.63.0
  • pana: 0.11.3


Describes how popular the package is relative to other packages. [more]
68 / 100
Code health derived from static analysis. [more]
100 / 100
Reflects how tidy and up-to-date the package is. [more]
100 / 100
Overall score:
Weighted score of the above. [more]
Learn more about scoring.


Detected platforms: web

Primary library: package:service_worker/service_worker.dart with components: html, js.


  • The description is too short.

    Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

  • Package is pre-v1 release.

    While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction API.

  • Fix analysis and formatting issues.

    Analysis or formatting checks reported 2 hints.

    Run dartfmt to format lib/src/js_facade/service_worker_api.dart.

    Run dartfmt to format lib/src/service_worker_api.dart.

  • Maintain an example.

    None of the files in your example/ directory matches a known example patterns. Common file name patterns include: main.dart, example.dart or you could also use service_worker.dart.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.40.0 <2.0.0
js ^0.6.1 0.6.1