Service Worker Generator
A powerful command-line tool for automatically generating Service Worker files for web applications. Specifically designed for Dart and Flutter Web applications, it simplifies the process of creating efficient service workers with intelligent resource caching.
๐ Features
- โ Automatic File Scanning โ Analyzes build directory and creates resource map with MD5 hashes
- โ Smart Caching Strategy โ Implements cache-first with network fallback and runtime caching
- โ Version Management โ Handles cache versioning for safe deployments and updates
- โ Flexible File Filtering โ Include/exclude files using powerful glob patterns
- โ Flutter Web Optimized โ Special support for Flutter web applications and assets
- โ Cross-Platform โ Works seamlessly on Windows, macOS, and Linux
- โ Customizable Cache Names โ Configure cache prefixes and versions
- โ Integrity Validation โ MD5 hashing for file integrity verification
- โ Offline Support โ Ensures your app works without internet connection
- โ PWA Ready โ Perfect for Progressive Web Applications
๐ฆ Installation
Global Installation
dart pub global activate sw
๐ Usage
Basic Usage
dart run sw:generate --help
Advanced Usage
# Custom input directory and output file
dart run sw:generate --input build/web --output flutter_service_worker.js
# Custom cache prefix and version
dart run sw:generate --prefix my-app --version 1.2.3
# Filter files with glob patterns
dart run sw:generate \
--glob="**.{html,js,wasm,json}; assets/**; canvaskit/**; icons/**"
--no-glob="flutter_service_worker.js; **/*.map; assets/NOTICES"
# Include comments in generated file
dart run sw:generate --comments
๐ Command Line Options
Option | Short | Description | Default |
---|---|---|---|
--help |
-h |
Show help information | - |
--input |
-i |
Path to build directory containing index.html | build/web |
--output |
-o |
Output service worker filename | sw.js |
--prefix |
-p |
Cache name prefix | app-cache |
--version |
-v |
Cache version | current timestamp |
--glob |
-g |
Glob patterns to include files | ** |
--no-glob |
-e |
Glob patterns to exclude files | - |
--comments |
-c |
Include comments in generated file | false |
๐ Usage Examples
# 1. Install dependencies
flutter pub get
# 2. Activate the service worker generator
dart pub global activate sw
# 3. (Optional) Run code generation
dart run build_runner build --delete-conflicting-outputs --release
# 4. Build Flutter project
flutter build web --release --no-tree-shake-icons --no-web-resources-cdn --base-href=/ -o build/web
# 5. Generate service worker
dart run sw:generate --input=build/web \
--output=flutter_service_worker.js \
--prefix=flutter-app \
--glob="**.{html,js,wasm,json}; assets/**; canvaskit/**; icons/**" \
--no-glob="flutter_service_worker.js; **/*.map; assets/NOTICES" \
--comments
๐ Generated Service Worker Structure
The generated service worker includes:
- ๐๏ธ Resource Caching โ All specified files are cached during installation
- โก Cache-First Strategy โ Prioritizes cache over network for better performance
- ๐ Cache Versioning โ Automatic cache updates when version changes
- ๐งน Smart Cleanup โ Removes old cache versions automatically
- ๐ฑ Runtime Caching โ Dynamic caching of new resources during runtime
- ๐ Integrity Checks โ MD5 hash validation for cached resources
- โฑ๏ธ TTL Support โ Time-based cache expiration
- ๐ Size Limits โ Configurable cache size limits
๐ค Contributing
We welcome contributions to this project! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Make your changes and add tests
- Ensure all tests pass (
dart test
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Create a Pull Request
To use this package from local path, you can clone the repository and run:
dart pub global activate --source path .
๐ License
This project is licensed under the MIT License. See the LICENSE file for details.