service_worker 0.2.4

  • Readme
  • Changelog
  • Installing
  • 82

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

0.2.4 #

  • Removed fakeConstructor$ from JavaScript facade classes which caused breaks in recent Dart SDKs.

0.2.3 #

  • Support Dart 2 stable.

0.2.2 #

  • Fix runtime errors casting List

0.2.1 #

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

0.2.0 #

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

0.1.2 #

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

0.1.1 #

  • Remove package:func dependency.

0.1.0 #

  • Upgraded dependencies.

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

Use this package as a library

1. Depend on it

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


dependencies:
  service_worker: ^0.2.4

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter 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';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
88
Health:
Code health derived from static analysis. [more]
75
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
77
Overall:
Weighted score of the above. [more]
82
Learn more about scoring.

We analyzed this package on Jul 2, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.13

Analysis suggestions

Package not compatible with runtime flutter-native on android

Because of the import of dart:js via the import chain package:service_worker/service_worker.dartpackage:service_worker/src/service_worker_api.dartpackage:service_worker/src/js_facade/service_worker_api.dartpackage:service_worker/src/js_facade/promise.dartpackage:js/js.dartdart:js

Package not compatible with runtime flutter-native on ios

Because of the import of dart:js via the import chain package:service_worker/service_worker.dartpackage:service_worker/src/service_worker_api.dartpackage:service_worker/src/js_facade/service_worker_api.dartpackage:service_worker/src/js_facade/promise.dartpackage:js/js.dartdart:js

Package not compatible with runtime flutter-native on linux

Because of the import of dart:js via the import chain package:service_worker/service_worker.dartpackage:service_worker/src/service_worker_api.dartpackage:service_worker/src/js_facade/service_worker_api.dartpackage:service_worker/src/js_facade/promise.dartpackage:js/js.dartdart:js

Package not compatible with runtime flutter-native on macos

Because of the import of dart:js via the import chain package:service_worker/service_worker.dartpackage:service_worker/src/service_worker_api.dartpackage:service_worker/src/js_facade/service_worker_api.dartpackage:service_worker/src/js_facade/promise.dartpackage:js/js.dartdart:js

Package not compatible with runtime flutter-native on windows

Because of the import of dart:js via the import chain package:service_worker/service_worker.dartpackage:service_worker/src/service_worker_api.dartpackage:service_worker/src/js_facade/service_worker_api.dartpackage:service_worker/src/js_facade/promise.dartpackage:js/js.dartdart:js

Package not compatible with runtime native-aot

Because of the import of dart:js via the import chain package:service_worker/service_worker.dartpackage:service_worker/src/service_worker_api.dartpackage:service_worker/src/js_facade/service_worker_api.dartpackage:service_worker/src/js_facade/promise.dartpackage:js/js.dartdart:js

Package not compatible with runtime native-jit

Because of the import of dart:js via the import chain package:service_worker/service_worker.dartpackage:service_worker/src/service_worker_api.dartpackage:service_worker/src/js_facade/service_worker_api.dartpackage:service_worker/src/js_facade/promise.dartpackage:js/js.dartdart:js

Health suggestions

Fix lib/src/service_worker_api.dart. (-22.95 points)

Analysis of lib/src/service_worker_api.dart reported 52 hints, including:

line 53 col 7: Unnecessary new keyword.

line 78 col 19: Unnecessary new keyword.

line 82 col 7: Unnecessary new keyword.

line 88 col 7: Unnecessary new keyword.

line 94 col 39: Unnecessary new keyword.

Fix lib/src/js_adapter.dart. (-2.48 points)

Analysis of lib/src/js_adapter.dart reported 5 hints:

line 14 col 36: Unnecessary new keyword.

line 24 col 28: Unnecessary new keyword.

line 40 col 10: Unnecessary new keyword.

line 60 col 5: Unnecessary new keyword.

line 84 col 31: Unnecessary new keyword.

Maintenance issues and suggestions

Make sure dartdoc successfully runs on your package's source files. (-10 points)

exitCode: 255 stdout: Documenting service_worker... Initialized dartdoc with 47 libraries in 36.0 seconds Generating docs for library service_worker from package:service_worker/worker.dart... Generating docs for library service_worker from package:service_worker/service_worker.dart... Generating docs for library service_worker.window from package:service_worker/window.dart... Validating docs... Documented 3 public libraries in 12.6 seconds stderr: warning: ambiguous reexport of service_worker_api.ExtendableEvent, canonicalization candidates: (service_worker, service_worker, service_worker.window) -> service_worker.window (confidence 0.01000) from service_worker_api.ExtendableEvent: (file:///tmp/pub-dartlang-dartdocGNQEOI/pkg/lib/src/service_worker_api.dart:603:7) service_worker: -0.7900 - is deprecated (-1.000), name is long (+0.01000), element location shares parts with name (+0.2000) service_worker: 0.2100 - name is long (+0.01000), element location shares parts with name (+0.2000) service_worker.window: 0.2200 - name is long (+0.02000), element location shares parts with name (+0.2000) Use {@canonicalFor service_worker_api.ExtendableEvent} in the desired library's documentation to resolve the ambiguity and/or override dartdoc's decision, or structure your package so the reexport is less ambiguous. The symbol will still be referenced in all candidates -- this only controls the location where it will be written and which library will be displayed in navigation for the relevant pages. The flag --ambiguous-reexport-scorer-min-confidence allows you to set the threshold at which this warning will appear. warning: ambiguous reexport of service_worker_api.NotificationEvent, canonicalization candidates: (service_worker, service_worker, service_worker.window) -> service_worker.window (confidence 0.01000) from service_worker_api.NotificationEvent: (file:///tmp/pub-dartlang-dartdocGNQEOI/pkg/lib/src/service_worker_api.dart:873:7) service_worker: -0.7900 - is deprecated (-1.000), name is long (+0.01000), element location shares parts with name (+0.2000) service_worker: 0.2100 - name is long (+0.01000), element location shares parts with name (+0.2000) service_worker.window: 0.2200 - name is long (+0.02000), element location shares parts with name (+0.2000) warning: ambiguous reexport of service_worker_api.PushManager, canonicalization candidates: (service_worker, service_worker, service_worker.window) -> service_worker.window (confidence 0.01000) from service_worker_api.PushManager: (file:///tmp/pub-dartlang-dartdocGNQEOI/pkg/lib/src/service_worker_api.dart:528:7) service_worker: -0.7900 - is deprecated (-1.000), name is long (+0.01000), element location shares parts with name (+0.2000) service_worker: 0.2100 - name is long (+0.01000), element location shares parts with name (+0.2000) service_worker.window: 0.2200 - name is long (+0.02000), element location shares parts with name (+0.2000) warning: ambiguous reexport of service_worker_api.PushSubscription, canonicalization candidates: (service_worker, service_worker, service_worker.window) -> service_worker.window (confidence 0.01000) from service_worker_api.PushSubscription: (file:///tmp/pub-dartlang-dartdocGNQEOI/pkg/lib/src/service_worker_api.dart:559:7) service_worker: -0.7900 - is deprecated (-1.000), name is long (+0.01000), element location shares parts with name (+0.2000) service_worker: 0.2100 - name is long (+0.01000), element location shares parts with name (+0.2000) service_worker.window: 0.2200 - name is long (+0.02000), element location shares parts with name (+0.2000) warning: ambiguous reexport of service_worker_api.PushSubscriptionOptions, canonicalization candidates: (service_worker, service_worker, service_worker.window) -> service_worker.window (confidence 0.01000) from service_worker_api.PushSubscriptionOptions: (file:///tmp/pub-dartlang-dartdocGNQEOI/pkg/lib/src/js_facade/service_worker_api.dart:263:16) service_worker: -0.8233 - is deprecated (-1.000), name is long (+0.01000), element location shares parts with name (+0.1667) service_worker: 0.1767 - name is long (+0.01000), element location shares parts with name (+0.1667) service_worker.window: 0.1867 - name is long (+0.02000), element location shares parts with name (+0.1667) warning: ambiguous reexport of service_worker_api.ServiceWorker, canonicalization candidates: (service_worker, service_worker, service_worker.window) -> service_worker.window (confidence 0.01000) from service_worker_api.ServiceWorker: (file:///tmp/pub-dartlang-dartdocGNQEOI/pkg/lib/src/service_worker_api.dart:769:7) service_worker: -0.7900 - is deprecated (-1.000), name is long (+0.01000), element location shares parts with name (+0.2000) service_worker: 0.2100 - name is long (+0.01000), element location shares parts with name (+0.2000) service_worker.window: 0.2200 - name is long (+0.02000), element location shares parts with name (+0.2000) warning: ambiguous reexport of service_worker_api.ServiceWorkerRegisterOptions, canonicalization candidates: (service_worker, service_worker, service_worker.window) -> service_worker.window (confidence 0.01000) from service_worker_api.ServiceWorkerRegisterOptions: (file:///tmp/pub-dartlang-dartdocGNQEOI/pkg/lib/src/js_facade/service_worker_api.dart:669:16) service_worker: -0.8233 - is deprecated (-1.000), name is long (+0.01000), element location shares parts with name (+0.1667) service_worker: 0.1767 - name is long (+0.01000), element location shares parts with name (+0.1667) service_worker.window: 0.1867 - name is long (+0.02000), element location shares parts with name (+0.1667) warning: ambiguous reexport of service_worker_api.ServiceWorkerRegistration, canonicalization candidates: (service_worker, service_worker, service_worker.window) -> service_worker.window (confidence 0.01000) from service_worker_api.ServiceWorkerRegistration: (file:///tmp/pub-dartlang-dartdocGNQEOI/pkg/lib/src/service_worker_api.dart:438:7) service_worker: -0.7900 - is deprecated (-1.000), name is long (+0.01000), element location shares parts with name (+0.2000) service_worker: 0.2100 - name is long (+0.01000), element location shares parts with name (+0.2000) service_worker.window: 0.2200 - name is long (+0.02000), element location shares parts with name (+0.2000) warning: ambiguous reexport of service_worker_api.ShowNotificationOptions, canonicalization candidates: (service_worker, service_worker, service_worker.window) -> service_worker.window (confidence 0.01000) from service_worker_api.ShowNotificationOptions: (file:///tmp/pub-dartlang-dartdocGNQEOI/pkg/lib/src/js_facade/service_worker_api.dart:503:7) service_worker: -0.8233 - is deprecated (-1.000), name is long (+0.01000), element location shares parts with name (+0.1667) service_worker: 0.1767 - name is long (+0.01000), element location shares parts with name (+0.1667) service_worker.window: 0.1867 - name is long (+0.02000), element location shares parts with name (+0.1667) error: failed to write file at: service_worker/service_worker-library.html for symbol service_worker: (file:///tmp/pub-dartlang-dartdocGNQEOI/pkg/lib/service_worker.dart:4:9) conflicting with file already generated by service_worker: (file:///tmp/pub-dartlang-dartdocGNQEOI/pkg/lib/worker.dart:2:9) Dartdoc generates a path and filename to write to for each symbol. service_worker conflicts with another symbol in the generated path, and therefore can not be written out. Changing the name, library name, or class name (if appropriate) of one of the conflicting items can resolve the conflict. Alternatively, use the @nodoc tag in one symbol's documentation comments to hide it. warning: service_worker.window has no library level documentation comments, from service_worker.window: (file:///tmp/pub-dartlang-dartdocGNQEOI/pkg/lib/window.dart:1:9) found 10 warnings and 1 error Unhandled exception: dartdoc encountered 1 errors while processing. #0 Dartdoc.generateDocs (package:dartdoc/dartdoc.dart:225:9)

The package description is too short. (-13 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Maintain an example.

None of the files in the package's example/ directory matches known example patterns.

Common filename patterns include main.dart, example.dart, and service_worker.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
js ^0.6.1 0.6.2