Search packages, repositories and app categories

Package detailsv0.4.0

service_worker

JavaScript bindings for the Service Worker API.

Web
30-day downloads
4.3K
Likes
26
Pub points
80

Install package

flutter pub add service_worker

Package Overview

Package selection guidance compiled from public package information, community data, and project usage.

The service_worker package provides JavaScript bindings for the Service Worker API in Flutter web applications. It enables developers to leverage service workers for advanced offline capabilities, background synchronization, and push notifications. By integrating with the browser's service worker lifecycle, this package allows Flutter apps to cache assets, intercept network requests, and manage data persistence even when offline. It is particularly useful for building progressive web apps (PWAs) that require robust offline functionality and improved performance through caching strategies.

Use cases

  • Building offline-first web applications
  • Implementing push notifications
  • Caching assets for faster load times
  • Managing background sync operations
  • Enhancing PWA performance
  • Intercepting and modifying network requests

Key features

  • JavaScript interop with Service Worker API
  • Support for offline asset caching
  • Background sync integration
  • Push notification handling
  • Event-driven service worker lifecycle
  • Web-only compatibility

Suitable for

  • Flutter developers targeting web platforms
  • PWA builders requiring offline support
  • Apps needing background sync or push notifications
  • Developers implementing custom caching strategies
  • Projects focused on web performance optimization

Considerations

  • Limited to web platform only
  • Requires understanding of JavaScript service worker concepts
  • May need additional configuration for complex use cases
  • Not compatible with mobile or desktop platforms
  • Depends on browser support for service workers

Original README

Original README snapshot from the linked source repository. Visit the repository for the latest content.

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

Limitation

You need to force dart2js compilation for service worker to work during debug mode. See build.yaml in the example folder.

Categories

Dependencies

Dependency list 1 items

  • js>=0.6.1 <2.0.0

Source repository

Projects using this package