UNPKG

2.3 kBJavaScriptView Raw
1importScripts(<%= [options.workboxURL, ...options.importScripts].map((i) => `'${i}'`).join(', ') %>)
2
3// --------------------------------------------------
4// Configure
5// --------------------------------------------------
6
7<% if (options.config) {%>
8// Set workbox config
9workbox.setConfig(<%= JSON.stringify(options.config, null, 2) %>)
10<% } %>
11
12<% if (options.clientsClaim) { %>
13// Start controlling any existing clients as soon as it activates
14workbox.core.clientsClaim()
15<% } %>
16
17<% if (options.skipWaiting) { %>
18// Skip over the SW waiting lifecycle stage
19workbox.core.skipWaiting()
20<% } %>
21
22<% if (options.cleanupOutdatedCaches) { %>
23workbox.precaching.cleanupOutdatedCaches()
24<% } %>
25
26<% if (options.offlineAnalytics) { %>
27// Enable offline Google Analytics tracking
28workbox.googleAnalytics.initialize()
29<% } %>
30
31<% if (options.workboxExtensions) { %>
32// -- Start of workboxExtensions --
33<%= options.workboxExtensions %>// -- End of workboxExtensions --
34<% } %>
35
36// --------------------------------------------------
37// Precaches
38// --------------------------------------------------
39
40// Precache assets
41<% if (options.preCaching.length) { %>
42workbox.precaching.precacheAndRoute(<%= JSON.stringify(options.preCaching, null, 2) %>, <%= JSON.stringify(options.cacheOptions, null, 2) %>)
43<% } %>
44
45<% if (options.cachingExtensions) { %>
46// -- Start of cachingExtensions --
47<%= options.cachingExtensions %>// -- End of cachingExtensions --
48<% } %>
49
50// --------------------------------------------------
51// Runtime Caching
52// --------------------------------------------------
53
54// Register route handlers for runtimeCaching
55<% options.runtimeCaching.forEach(r => { %>workbox.routing.registerRoute(new RegExp('<%= r.urlPattern %>'), new workbox.strategies.<%= r.handler %> (<%= JSON.stringify(r.strategyOptions || {}) %>), '<%= r.method %>')
56<% }) %>
57
58<% if (options.offlinePage) { %>
59// Register router handler for offlinePage
60workbox.routing.registerRoute(new RegExp('<%= options.pagesURLPattern %>'), ({event}) => {
61 return new workbox.strategies.NetworkOnly().handle({event})
62 .catch(() => caches.match('<%= options.offlinePage %>'))
63})<% } %>
64
65<% if (options.routingExtensions) { %>
66// -- Start of routingExtensions --
67<%= options.routingExtensions %>// -- End of routingExtensions --
68<% } %>
69
\No newline at end of file