A node module to generate service worker code that will precaches specific resource so they work offline.
Serving your local static resources cache-first means that you can get all the crucial scaffolding for your web app – your app shell – on the screen without having to wait for any network responses.
Installation
1 | yarn add --dev sw-precache |
Overview
Make sure your site is served using HTTPS.
incorporate
sw-precache
into yournode
-based build script.Register the service worker JavaScript.
Considerations
Service worker caching should be considered a prograssive enhancement. If you follow the model of conditionally registering a service worker only if it’s supported(determined by
if('serviceWorker' in navigator)
), you’ll get offline support on browsers with service workers and on browsers that don’t support service workers, the offline-specific code will never be called.All resources that are precached will be fetched by a service worker runing in a separate thread as soon as the service worker is installed. You should be judicious in what you list in the
dynamicUrlsToDependencies
andstaticFileGlobs
options, since listing files that are non-essential(large-images that are not shown on every page, for instance) will result in browsers downloading more data than is strictly necessary.Precaching doesn’t make sense for all types of resource. Other caching strategies can be used in conjunction with
sw-precache
to provide the best experience for your users. If you do implement additional caching logic, put the code in a separate file and include it using theimportScripts()
method.sw-precache
uses a cache-first strategy, which results in a copy of any cached content being returned without consulting the network.
Use with webpack
1 | yarn add --dev sw-precache-webpack-plugin |
Basic Usage
A simple configuration example that will work well in most production environments.
1 | const path = require('path') |
This will generate a new service worker at src/bundles/service-worker.js
, then you would register it in your application:
1 | (function() { |
Configuration
filename: string - service worker filename, default is
service-worker.js
filepath: string - service worker path and name, default is to use
webpack.output.path
+options.filename
. This will overridefilename
.staticFileGlobsIngorePatters: [Regexp] - Define an optional array of regex patterns to filter out of staticFileGlobs.
mergeStaticsConfig: [boolean] - Merge provided staticFileGlobs and stripPrefixMulti with webpack’s config, rather than having those take precedence, default is false.
minify: [boolean] - Set to true to minify and uglify the generated service-worker, default is false.
cacheId: [string] - Not required but you should include this, it will give your service worker cache a unique name, default to
sw-precache-webpack-plugin
.importScripts: [Array<String|Object>]
when importScripts array item is a string, converts to object format:
{ filename: '<publicPath>/my-scripts.js'}
when importScripts array item is an Object
look for
chunkName
propertylook for
filename
property
replacePrefix: [String] - should only be used in conjunction with
stripPrefix
staticFileGlobs: [Array
] - Omit this to allow the plugin to cache all your bundles’ emitted assets. If mergeStaticsConfig=true
: this value will be merged with your bundles’ emitted assets, otherwise this value is just passed tosw-precache
and emitted assets won’t be included.stripPrefix: [String] - Same as stripPrefixMulti[stripPrefix] = ‘’
stripPrefixMulti: [Object<String,String>] - Omit this to use your webpack config’s
output.path
+/
:output.publicPath
.
Note taht all configuration options are optional.
SWPrecacheWebpackPlugin
will by default use all your assets emitted by webpack’s compiler for thestaticFileGlobs
parameter and your webpack config’s{[output.path + '/']: output.publicPath}
as the stripPrefixMulti parameter. This behavior is probably what you want, all your webpack assets will be cached by your generated service worker. Just don’t pass any arguments when you initialize this plugin, and let this plugin handle generating yoursw-precache
configuration.
1 | plugins: [ |