搜尋套件、儲存庫與 APP 分類

套件詳情v0.4.0

service_worker

Service Worker API 的 JavaScript 綁定。

Web
近 30 天下載
4320
喜歡數
26
Pub 分數
80

安裝套件

flutter pub add service_worker

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

service_worker 套件為 Flutter Web 應用程式中的 Service Worker API 提供 JavaScript 繫結。它讓開發者能夠利用服務工作者實現進階離線功能、背景同步與推播通知。透過與瀏覽器的服務工作者生命週期整合,此套件允許 Flutter 應用程式快取資源、攔截網路請求並在離線時管理資料持久性。對於需要強大離線功能並透過快取策略提升效能的漸進式 Web 應用程式(PWA)而言,這特別實用。

適用情境

  • 建構離線優先的Web應用程式
  • 實現推送通知
  • 快取資源以加快載入速度
  • 管理背景同步作業
  • 提升 PWA 性能
  • 攔截和修改網路請求

主要特色

  • JavaScript 與 Service Worker API 的互操作性
  • 支援離線資源快取
  • 背景同步整合
  • 推送通知處理
  • 事件驅動的服務工作者生命週期
  • 僅限網頁相容性

適合對象

  • 面向 Web 平台的 Flutter 開發者
  • 需要離線支援的PWA建構工具
  • 需要背景同步或推播通知的應用
  • 開發人員實現自定義快取策略
  • 專注於網頁效能優化的專案

使用建議

  • 僅限網頁平台
  • 需要理解 JavaScript 服務工作人員概念
  • 可能需要額外設定以應對複雜使用案例
  • 不相容移動或桌面平台
  • 依賴瀏覽器對服務工作者的支援

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

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.

所屬分類

依賴

依賴清單 1 項

  • js>=0.6.1 <2.0.0

原始碼儲存庫

使用此套件的專案