1/* eslint-disable */ 2const WEBSITE = 'lalibre'; 3const VERSION = 'v1'; 4const CACHE_ONE_DAY = 86400; 5const isLocalhost = self.location.host.includes('localhost'); 6 7/** 8 * @description Fallback img by site 9 * This must match with config in getFallbackImg (~/utils/service-worker) 10 * If you change src images here, don't forget to do the same in getFallbackImg 11 */ 12const fallbackImgSrcBySite = { 13 dhnet: 14 '/resizer/v2/RQVIVP4CPZBYDKIG7PDLYVMH3U.png?auth=0c281192d6834e10f6cf11ca3cae57209c65bf3a293c10d8d04320d97f668dd0', 15 lalibre: 16 '/resizer/v2/TB6RBRRB7ZFTLE2HFN72AKZUV4.png?auth=a959ec46b9a9afa6c338e126244b969e869cf58b651116dfa22d5b2c0adf58fb', 17 lavenir: 18 '/resizer/v2/5FRACCDWWREUPJZR3PWWKAM73Q.png?auth=42e589b23b1bceec2fc2816d6afd0fe032ea85e2f4720986bc7c2782091ea26b', 19 parismatch: 20 '/resizer/v2/G2CPI2HRIJCM7PVAI722GPGI4Q.png?auth=da03ac44d5e7d4154ba6eef3fb08c68ca03e80044800efc948aff39307a78d8a', 21 ln24: 22 '/resizer/v2/ERPECH2Q4RCZDDIWSLYPWM4CQU.png?auth=082cfb5e5e13beaca240c4b01e851925cfc3eea02e9aa6f29e4a684beefe0526', 23 moustique: 24 '/resizer/v2/FKWVLLPGRRASFBLXZE4LHOGYHI.png?auth=30cba1249f72b576c5ea193364b35bd7af4fe3900933ceae774dad4d1dcfa37c', 25}; 26 27/** 28 * @description Static assets 29 */ 30const staticAssets = { 31 offlinePage: getAssetSrc({ 32 url: '/offline.html', 33 localUrl: `/html/${WEBSITE}/offline.html`, 34 version: VERSION, 35 }), 36 fallbackImg: getAssetSrc({ 37 url: fallbackImgSrcBySite[WEBSITE], 38 localUrl: `/images/${WEBSITE}/placeholder.png`, 39 }), 40 manifest: getAssetSrc({ 41 url: '/manifest.webmanifest', 42 localUrl: `/webmanifest/${WEBSITE}/manifest.webmanifest`, 43 version: VERSION, 44 }), 45}; 46 47/** 48 * @description Workbox plugins 49 */ 50const plugins = { 51 ExpirationPlugin: 'ExpirationPlugin', 52 BackgroundSyncPlugin: 'BackgroundSyncPlugin', 53 PrecacheFallbackPlugin: 'PrecacheFallbackPlugin', 54}; 55 56/** 57 * @description Workbox strategies 58 */ 59const strategies = { 60 CacheFirst: 'CacheFirst', 61 CacheOnly: 'CacheOnly', 62 NetworkFirst: 'NetworkFirst', 63 NetworkOnly: 'NetworkOnly', 64 StaleWhileRevalidate: 'StaleWhileRevalidate', 65}; 66 67/** 68 * @description Workbox options 69 */ 70const options = { 71 workbox: 'https://storage.googleapis.com/workbox-cdn/releases/6.5.4/workbox-sw.js', 72 config: { 73 debug: false, 74 }, 75 cacheNames: { 76 prefix: WEBSITE, 77 }, 78 oneSignal: 79 WEBSITE === 'moustique' || 80 WEBSITE === 'lavenir' || 81 WEBSITE === 'parismatch' || 82 WEBSITE === 'dhnet' || 83 WEBSITE === 'lalibre', 84 clientsClaim: true, 85 skipWaiting: true, 86 cleanupOutdatedCaches: true, 87 offlineAnalytics: false, 88 precache: [ 89 { url: staticAssets.fallbackImg, revision: null }, 90 { url: staticAssets.offlinePage, revision: VERSION }, 91 { url: staticAssets.manifest, revision: VERSION }, 92 ], 93 runtimeCache: [ 94 { 95 cacheName: 'assets', 96 regex: `${location.origin}/pf/(dist|resources)/.*`, 97 exceptRouteRegex: '/pf/resources/sw/?.*', 98 strategy: { 99 online: { 100 handler: isLocalhost ? strategies.StaleWhileRevalidate : strategies.CacheFirst, 101 options: { 102 cacheableResponse: { statuses: [0, 200] }, 103 }, 104 plugins: [ 105 { 106 use: plugins.ExpirationPlugin, 107 config: { 108 maxAgeSeconds: CACHE_ONE_DAY, 109 purgeOnQuotaError: true, 110 maxEntries: 50, 111 }, 112 }, 113 ], 114 }, 115 offline: { 116 handler: strategies.CacheFirst, 117 }, 118 }, 119 }, 120 { 121 cacheName: 'history', 122 navigate: true, 123 exceptRouteRegex: '/(?:profil|fil-info|formulaire|alertez-nous|pf|pagebuilder)/?.*', 124 strategy: { 125 online: { 126 handler: strategies.NetworkFirst, 127 options: { 128 cacheableResponse: { statuses: [0, 200] }, 129 }, 130 plugins: [ 131 { 132 use: plugins.ExpirationPlugin, 133 config: { 134 maxAgeSeconds: CACHE_ONE_DAY, 135 maxEntries: 10, 136 purgeOnQuotaError: true, 137 }, 138 }, 139 { 140 use: plugins.PrecacheFallbackPlugin, 141 config: { 142 fallbackURL: staticAssets.offlinePage, 143 }, 144 }, 145 ], 146 }, 147 offline: { 148 handler: strategies.CacheFirst, 149 }, 150 }, 151 }, 152 { 153 cacheName: 'history', 154 regex: `${location.origin}/(?:profil|fil-info|formulaire|alertez-nous|pf)/?.*`, 155 strategy: { 156 online: { 157 handler: strategies.NetworkOnly, 158 plugins: [ 159 { 160 use: plugins.PrecacheFallbackPlugin, 161 config: { 162 fallbackURL: staticAssets.offlinePage, 163 }, 164 }, 165 { 166 use: plugins.ExpirationPlugin, 167 config: { 168 maxAgeSeconds: CACHE_ONE_DAY, 169 maxEntries: 0, 170 purgeOnQuotaError: true, 171 }, 172 }, 173 ], 174 }, 175 offline: { 176 handler: strategies.NetworkOnly, 177 }, 178 }, 179 }, 180 { 181 cacheName: 'images', 182 regex: `/resizer/.+.(jpg|jpeg|png)`, 183 strategy: { 184 online: { 185 handler: strategies.CacheFirst, 186 options: { 187 cacheableResponse: { statuses: [0, 200] }, 188 }, 189 plugins: [ 190 { 191 use: plugins.ExpirationPlugin, 192 config: { 193 maxAgeSeconds: CACHE_ONE_DAY, 194 purgeOnQuotaError: true, 195 maxEntries: 20, 196 }, 197 }, 198 ], 199 }, 200 offline: { 201 handler: strategies.CacheFirst, 202 }, 203 }, 204 }, 205 ], 206}; 207 208if (options.oneSignal) { 209 importScripts('https://cdn.onesignal.com/sdks/OneSignalSDKWorker.js'); 210} 211importScripts(options.workbox); 212init(); 213runtimeCaching(); 214 215/** 216 * @description Init workbox 217 */
218function init() { 219 const { 220 config, 221 cacheNames, 222 clientsClaim, 223 skipWaiting, 224 cleanupOutdatedCaches, 225 offlineAnalytics, 226 precache, 227 } = options; 228 229 if (config) workbox.setConfig(config); 230 if (cacheNames) workbox.core.setCacheNameDetails(cacheNames); 231 if (clientsClaim) workbox.core.clientsClaim(); 232 if (skipWaiting) self.skipWaiting(); 233 if (cleanupOutdatedCaches) workbox.precaching.cleanupOutdatedCaches(); 234 if (offlineAnalytics) workbox.googleAnalytics.initialize(); 235 if (precache?.length) workbox.precaching.precacheAndRoute(precache); 236} 237 238/** 239 * @description Runtime caching 240 */ 241function runtimeCaching() { 242 const { runtimeCache } = options; 243 244 runtimeCache.forEach((cache) => { 245 const strategy = getStrategy(cache); 246 247 const _matchCb = ({ event, url }) => matchCb({ event, url, cache }); 248 const _handlerCb = async ({ event, request }) => handlerCb({ event, strategy, request }); 249 250 workbox.routing.registerRoute(_matchCb, _handlerCb); 251 }); 252} 253 254/** 255 * @description Match callback 256 */ 257function matchCb({ event, url, cache }) { 258 const { exceptRouteRegex, regex, navigate } = cache || {}; 259 const _matchRoute = matchRoute({ url, exceptRouteRegex }); 260 261 if (navigate) { 262 return event.request.mode === 'navigate' && _matchRoute; 263 } 264 return url.href.match(new RegExp(regex)) && _matchRoute; 265} 266 267/** 268 * @description Handler callback 269 */ 270async function handlerCb({ event, strategy, request }) { 271 if (strategy) { 272 const response = await strategy.handle({ event, request }); 273 return response; 274 } 275 return null; 276} 277 278/** 279 * @description Get strategy 280 */ 281function getStrategy(cache) { 282 const { 283 cacheName, 284 strategy: { 285 online: { 286 handler: handlerOnline, 287 options: strategyOptionsOnline = {}, 288 plugins: strategyPluginsOnline = [], 289 } = {}, 290 offline: { handler: handlerOffline } = {}, 291 } = {}, 292 } = cache || {}; 293 294 const strategyOptions = { 295 cacheName, 296 ...strategyOptionsOnline, 297 plugins: managePlugins({ pluginsList: strategyPluginsOnline, cacheName }), 298 }; 299 300 const strategy = (handler) => handler && new workbox.strategies[handler](strategyOptions); 301 302 return navigator.onLine ? strategy(handlerOnline) : strategy(handlerOffline); 303} 304 305/** 306 * @description Manage plugins 307 */ 308function managePlugins({ pluginsList, cacheName }) { 309 return pluginsList.map((plugin) => { 310 const { use, config } = plugin || {}; 311 312 switch (use) { 313 case plugins.ExpirationPlugin: 314 return new workbox.expiration.ExpirationPlugin({ ...config }); 315 case plugins.BackgroundSyncPlugin: 316 return new workbox.backgroundSync.BackgroundSyncPlugin(`queue-${cacheName}`, { ...config }); 317 case plugins.PrecacheFallbackPlugin: 318 return new workbox.precaching.PrecacheFallbackPlugin({ ...config }); 319 default: 320 return null; 321 } 322 }); 323} 324 325/** 326 * @description Bypass route 327 */ 328function matchRoute({ url, exceptRouteRegex }) { 329 return (exceptRouteRegex && !url.href.match(new RegExp(exceptRouteRegex))) || !exceptRouteRegex; 330} 331 332/** 333 * @description Get asset src 334 */ 335function getAssetSrc({ localUrl, url, version }) { 336 const localRessourcesPath = `/pf/resources${localUrl}`; 337 const getUrl = (_url) => (version ? `${_url}?__WB_REVISION__=${version}` : _url); 338 339 if (isLocalhost && localUrl) return getUrl(localRessourcesPath); 340 return getUrl(url); 341}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.