A webpack plugin for offline-first web applications using ServiceWorker and AppCache
npx @tessl/cli install tessl/npm-offline-plugin@5.0.0Offline Plugin is a webpack plugin that provides offline-first capabilities for webpack-based web applications. It automatically generates ServiceWorker and AppCache (as fallback) configurations to enable applications to work reliably without internet connectivity by intelligently caching webpack output assets.
npm install offline-plugin --save-devPlugin (for webpack configuration):
const OfflinePlugin = require('offline-plugin');Runtime (for client-side code):
require('offline-plugin/runtime').install();ES6/TypeScript:
import * as OfflinePluginRuntime from 'offline-plugin/runtime';
OfflinePluginRuntime.install();Access default options:
const OfflinePlugin = require('offline-plugin');
console.log(OfflinePlugin.defaultOptions); // View default configuration// webpack.config.js
const OfflinePlugin = require('offline-plugin');
module.exports = {
// ... your webpack config
plugins: [
// ... other plugins
// OfflinePlugin should be the last plugin
new OfflinePlugin()
]
};// In your main entry file
require('offline-plugin/runtime').install();With update handling:
require('offline-plugin/runtime').install({
onUpdateReady: () => {
console.log('SW Event:', 'onUpdateReady');
// Update available, apply it
require('offline-plugin/runtime').applyUpdate();
},
onUpdated: () => {
console.log('SW Event:', 'onUpdated');
// App updated, reload page
window.location.reload();
}
});Offline Plugin operates at two levels:
OfflinePlugin class hooks into webpack's compilation process to analyze assets, generate cache manifests, and create optimized ServiceWorker and AppCache configurationsComplete webpack plugin configuration with extensive options for caching strategies, asset management, and offline behavior customization.
class OfflinePlugin {
constructor(options?: PluginOptions);
apply(compiler: webpack.Compiler): void;
static defaultOptions: PluginOptions;
}
interface PluginOptions {
caches?: 'all' | CacheConfiguration;
publicPath?: string;
updateStrategy?: 'changed' | 'all';
responseStrategy?: 'cache-first' | 'network-first';
externals?: string[];
excludes?: string[];
relativePaths?: boolean;
version?: string | Function;
autoUpdate?: boolean | number;
rewrites?: Function | Object;
safeToUseOptionalCaches?: boolean;
appShell?: string;
cacheMaps?: CacheMap[];
ServiceWorker?: ServiceWorkerOptions;
AppCache?: AppCacheOptions | false;
}
interface CacheConfiguration {
main?: string[];
additional?: string[];
optional?: string[];
}
interface CacheMap {
match: string | Function;
to?: string | Function;
requestTypes?: ('navigate' | 'same-origin' | 'cross-origin')[];
}
interface ServiceWorkerOptions {
output?: string;
entry?: string;
scope?: string;
events?: boolean;
minify?: boolean;
forceInstall?: boolean;
updateViaCache?: 'imports' | 'all' | 'none';
prefetchRequest?: PrefetchRequestOptions;
navigationPreload?: boolean | 'auto';
}
interface AppCacheOptions {
directory?: string;
NETWORK?: string;
FALLBACK?: { [path: string]: string };
caches?: string[];
events?: boolean;
disableInstall?: boolean;
includeCrossOrigin?: boolean;
}
interface PrefetchRequestOptions {
credentials?: 'same-origin' | 'include' | 'omit';
headers?: { [key: string]: string };
mode?: 'cors' | 'no-cors' | 'same-origin';
cache?: string;
}Client-side API for managing offline functionality, handling updates, and responding to connectivity changes.
function install(options?: InstallOptions): void;
function applyUpdate(): void;
function update(): void;
interface InstallOptions {
onInstalled?: () => void;
onUpdating?: () => void;
onUpdateReady?: () => void;
onUpdateFailed?: () => void;
onUpdated?: () => void;
}Advanced caching configuration allowing fine-grained control over which assets are cached, how they're organized, and when they're updated.
Key cache features:
Automatic ServiceWorker generation with advanced features:
Legacy AppCache support for older browsers:
// Special cache keywords (used in cache configuration arrays)
const CACHE_KEYWORDS = {
REST: ':rest:', // Include all remaining uncategorized assets
EXTERNALS: ':externals:' // Include all external URLs
};
// Auto-update default interval
const DEFAULT_AUTO_UPDATE_INTERVAL = 3600000; // 1 hour in milliseconds
// Valid update strategies
const UPDATE_STRATEGIES = ['changed', 'all'];
// Valid response strategies
const RESPONSE_STRATEGIES = ['cache-first', 'network-first'];
// Valid updateViaCache values
const UPDATE_VIA_CACHE_OPTIONS = ['imports', 'all', 'none'];