Dart-y wrappers for the ServiceWorker APIs.
Warning: the API is experimental, and subject to change.
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) {
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.');
});
}
dynamic
as bottom by adding type parameters to
futureToPromise
.dart:html
for 2.0.0-dev.40.0
:ExtendableEvent
, FetchEvent
removes deepPath
or scoped
members.composed
and composedPath
members.package:func
dependency.Event
.ServiceWorker
instances when the underlying JS object is null.Notification.close()
ServiceWorkerClientsMatchOptions
PushSubscription
ServiceWorkerRegistration.jsObject
PushManager.hasPermission()
is a deprecated API and it doesn't work in Chrome
anymore, clients should use permissionState()
instead.Add this to your package's pubspec.yaml file:
dependencies:
service_worker: ^0.2.3
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.
Now in your Dart code, you can use:
import 'package:service_worker/service_worker.dart';
Version | Uploaded | Documentation | Archive |
---|---|---|---|
0.2.3 | Aug 6, 2018 |
|
|
0.2.2 | Jun 27, 2018 |
|
|
0.2.1 | Apr 19, 2018 |
|
|
0.2.0 | Mar 21, 2018 |
|
|
0.1.2 | Feb 5, 2018 |
|
|
0.1.1 | Jan 10, 2018 |
|
|
0.1.0 | Sep 29, 2017 |
|
|
0.0.16 | Jul 1, 2017 |
|
|
0.0.15 | Jun 15, 2017 |
|
|
0.0.14 | May 11, 2017 |
|
|
Popularity:
Describes how popular the package is relative to other packages.
[more]
|
84
|
Health:
Code health derived from static analysis.
[more]
|
100
|
Maintenance:
Reflects how tidy and up-to-date the package is.
[more]
|
87
|
Overall:
Weighted score of the above.
[more]
|
89
|
We analyzed this package on Feb 14, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:
Detected platforms: web
Primary library:
package:service_worker/service_worker.dart
with components:html
,js
.
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.
Package | Constraint | Resolved | Available |
---|---|---|---|
Direct dependencies | |||
Dart SDK | >=2.0.0-dev.40.0 <3.0.0 | ||
js | ^0.6.1 | 0.6.1+1 |