jfa-pwa-toolkit
Add Progressive Web Apps (PWA) Features to Any website very Easy and Fast ⚡️
Set a simple configuration file or exploit more features with the easy-to-use library in your js code.
Note: This project is Beta
Attention: THIS DOCUMENTATION IS THE OLDER VERSION!
Features
- Web App Manifest
- Icons Structure Files
- Add to Home Screen (A2HS)
- Offline Work Mode
- Precaching
- Caching Strategies
- Push Notifications
Demo
Requirements
This package uses Workbox internally but you do not need to import it.
Starting with an Example
Getting Started
Clone the repository
$ git clone git@github.com:jfadev/jfa-pwa-toolkit.git
Create the app icons manually or with the tool PWA Image Generator and replace files in /pwa/icons/
.
If you have moved the pwa
folder somewhere else in your project, edit the PWA_ROOT
variable in your /sw.js
file. It is important that the file sw.js
is in the root of your site.
Edit the template of your project to include the metatags and scripts needed as in the following example.
Attention with the paths if you decided to move the pwa
folder
<!-- Theme color --> <!-- PWA Manifest --> <!-- Favicon --> <!-- Only for iOS: Configs --> <!-- Icons --> <!-- Splash Screen --> JFA PWA Toolkit Example <!-- Load JFA PWA Toolkit Configs --> <!-- Load JFA PWA Toolkit Lib --> <!-- Register principal Service Worker -->
Edit the manifest file /pwa/manifest.json
.
You can rename manifest.json
to app.webmanifest
if you prefer Reccomended by W3C.
Attention with the paths of icons if you decided to move the pwa
folder
Edit the config file /pwa/config.js
.
Attention with the paths of icons if you decided to move the pwa
folder
const PWA_CONFIG = // App config app: // App name name: 'your-app-name' // App version version: 'v1' // Service Worker config sw: // Main service worker filepath (always root of project) filepath: '/sw.js' // Route of offline page offline_route: '/pwa/errors/offline/' // Push manager config push: // Enable/disable push notifications active: true // Server config server: // API public key public_key: 'YOURAPIPUBLICKEY' // Subscription API endpoint endpoint: '/api/push/subscription/' // Notification config notification: // Title of notifications from the server title: 'Your App Name' // Options object same that showNotification() options // (https://developer.mozilla.org/es/docs/Web/API/ServiceWorkerRegistration/showNotification) options: // A string representing an extra content to display within the notification body: '' // he URL of an image to be used as an icon by the notification icon: '/pwa/icons/firefox/firefox-general-64-64.png' // A vibration pattern to run with the display of the notification. // A vibration pattern can be an array with as few as one member. // The values are times in milliseconds where the even indices (0, 2, 4, etc.) // indicate how long to vibrate and the odd indices indicate how long to pause. // For example [300, 100, 400] would vibrate 300ms, pause 100ms, then vibrate 400ms. vibrate: 100 50 100 // Arbitrary data that you want associated with the notification. This can be of any data type data: dateOfArrival: Date primaryKey: '1' clickUrl: '' // notification click event notificationclick: // Enable/disable notification click event active: true // Cache config cache: // Images cache config (png|jpg|jpeg|svg|gif) images: // Enable/disable images caching active: true // The maximum number of entries to cache. // Entries used the least will be removed as the maximum is reached. maxentries: 500 // The maximum age of an entry before it's treated as stale and removed. maxageseconds: 365 * 24 * 60 * 60 // Static files cache config (js|json|css) statics: // Enable/disable static files caching active: true // The maximum number of entries to cache. // Entries used the least will be removed as the maximum is reached. maxentries: 500 // The maximum age of an entry before it's treated as stale and removed. maxageseconds: 365 * 24 * 60 * 60 // Fonts cache config (eot|ttf|woff|woff2|otf) // with cross-origin requests example google fonts fonts: // Enable/disable fonts caching active: true // The maximum number of entries to cache. // Entries used the least will be removed as the maximum is reached. maxentries: 500 // The maximum age of an entry before it's treated as stale and removed. maxageseconds: 365 * 24 * 60 * 60 routes: // Force the response to come from the network networkonly: // Enable/disable network only routes caching active: true // Matching routes with a Regular Expression regex: /\/\// // Resources are requested from both the cache and the network in parallel. // The strategy will respond with the cached version if available, // otherwise wait for the network response. // The cache is updated with the network response with each successful request. stalewhilerevalidate: active: true regex: /\/news\/.*/ // Network first request strategy. networkfirst: active: true regex: /.*/ // Cache first request strategy. cachefirst: active: false // regex: /.*/, // maxentries: 500, // maxageseconds: 365 * 24 * 60 * 60, // Force the response to come from the browser. cacheonly: active: false // regex: /.*/, // Add your custom service worker for load it. custom: active: false // service worker script route // script: '/pwa/sw/my-custom-sw.js', // Precache config precache: // Enable/disable precaching active: true // Routes to routes: '/assets/example.css' '/assets/example.png' '/assets/example.js'
At this point your PWA is able to precache static resources, cache the different resources for offline access, icons, splash screen and add to home screen.
You may need more features, for this we will use the classes and methods offered by the global variable PWA that will allow us to do things like subscription to push notifications among other things.
Code Examples
Button to subscribe to Push Notifications
Subscribe to recive Push Notifications
document ;
Checkbox toggle to subscribe/unsubscribe to Push Notifications
Push Notifications
document ;
Check the Notifications permissions
Default status
if PWANotification console; else console;
Granted status
if PWANotification console; else console;
Blocked status
if PWANotification console; else console;
Denied status
if PWANotification console; else console;
Displays a pop-up requesting permission to allow Notifications
PWANotification;
Get the Notifications permission status
var permission = PWANotification;
Show Notification sent by the browser
const options = body: 'Extra content to display within the notification' icon: '../images/touch/chrome-touch-icon-192x192.png'; PWANotification;
Register the main service worker
PWAServiceWorker;
Get the Registration object of the service worker
PWAServiceWorker;
Check if the browser support Service Workers
if PWANavigator console; else console;
Check if the browser support Notifications
if PWANavigator console; else console;
Check if the browser is offline
if PWANavigator console; else console;
Clear the browser app cache
PWANavigator;
Documentation
Playground
License
JFA PWA Toolkit is MIT licensed, as found in the LICENSE file.