A Webpack plugin to easily add PWA and Service Worker support to your application with no configuration.
npm install webpack-pwa-sw-pluginWebpackPwaSwPlugin is a highly customizable Webpack plugin that simplifies the integration of service workers in Progressive Web Applications (PWAs). It facilitates the auto-registration of service workers and enables various caching strategies, including runtime caching, background synchronization, and broadcast updates. With this plugin, users can effortlessly enhance their web applications with offline capabilities and ensure their assets are served efficiently.
CacheFirst, StaleWhileRevalidate, NetworkFirst, and NetworkOnly.
manifest.json file into your HTML.
BroadcastUpdatePlugin.
bash
npm install --save-dev webpack-pwa-sw-plugin workbox-webpack-plugin webpack-virtual-modules
`
Usage
In your webpack.config.js:
`js
const WebpackPwaSwPlugin = require("webpack-pwa-sw-plugin");
module.exports = {
// Other webpack configuration
plugins: [
new WebpackPwaSwPlugin({
manifest: {
name: "My PWA",
short_name: "PWA",
icons: [{ src: "/icon.png", sizes: "192x192", type: "image/png" }],
start_url: ".",
display: "standalone",
background_color: "#ffffff",
theme_color: "#000000",
},
workboxOptions :{
// Other workbox options for InjectManifest. visit : https://developer.chrome.com/docs/workbox/modules/workbox-webpack-plugin#injectmanifest_plugin
}
runtimeCaching: [
{
urlPattern: /\/api\//,
handler: "NetworkFirst",
options: {
cacheName: "api-cache",
expiration: {
maxEntries: 50,
maxAgeSeconds: 60 60 24, // 1 day
},
syncStrategies: ["broadcastUpdate"],
},
},
],
autoRegisterServiceWorker: true,
reloadOnConnectionRestored: true,
showPromptOnUpdate: true,
promptText: "New content is available, reload now?",
onNeedRefresh : () => { console.log("Need to refresh") }
}),
],
};
`
Configuration Options
$3
An object that represents your PWA's manifest file. This is automatically injected into the build output as manifest.json.
$3
Additional options to pass to the InjectManifest plugin
$3
Enables automatic reloading of the application when the internet connection is restored after going offline.
$3
If set to true, the user will be shown a confirmation prompt to reload the page when a new version of the service worker is available.
$3
Customizes the prompt text shown when showPromptOnUpdate is enabled.
$3
Custom function to run when a new service worker is ready to activate.
$3
An array of objects defining runtime caching strategies for specific URL patterns.This plugin option supports various caching strategies for different resource types. The configuration is defined in runtimeCachingConfig.js. It includes built-in patterns for Google Fonts, static assets, and APIs. You can customize it by providing your own runtime caching rules.
- urlPattern: A regex pattern or a function to match requests for which this caching strategy should apply.
- handler: The caching strategy to use, can be one of the following:
1. CacheFirst: Prioritizes the cache over the network. Useful for static assets.
2. StaleWhileRevalidate: Serves cached content while updating the cache in the background. Useful for frequently changing resources like JavaScript and CSS files.
3. NetworkFirst: Tries the network first, falls back to cache if the network is unavailable.
4. NetworkOnly: Uses the network exclusively, no caching. Each object includes:
- options: Additional options for each strategy, including cache names, expiration rules, and plugins.
#### _(Unique to this plugin is the syncStrategies configuration)_:
- syncStrategies: An array that can include the following two options:
- backgroundSync: Ensures that failed POST requests are retried when the app is back online using the BackgroundSyncPlugin.
- broadcastUpdate: Updates all open tabs or clients when a cached response is updated by utilizing the BroadcastUpdatePlugin.
#### Example of syncStrategies:
`js
{
urlPattern: /\/api\//,
handler: 'NetworkFirst',
options: {
cacheName: 'api-cache',
syncStrategies: ['backgroundSync', 'broadcastUpdate'],
expiration: {
maxEntries: 16,
maxAgeSeconds: 60 60 24, // 1 day
},
},
}
``