-
Notifications
You must be signed in to change notification settings - Fork 1
/
sw.v1.js
1 lines (1 loc) · 1.05 KB
/
sw.v1.js
1
importScripts("https://g.alicdn.com/kg/workbox/3.3.0/workbox-sw.js"),workbox.setConfig({modulePathPrefix:"https://g.alicdn.com/kg/workbox/3.3.0/"});const HTML_FAIL_URL="/public/html/offline.html",IMG_FAIL_URL="/public/img/404.png",REGEX={ROOT:/(localhost:4000|ede.ink)\/?$/,HTML:/.*\.(?:html|php).*$/,LIB:/.*\.(?:js|css).*$/,IMG:/.*\.(?:png|jpg|jpeg|svg|gif).*$/,FONT:/.*\.(?:woff2|woff|eot|ttf|svg).*$/};var networkFirst=workbox.strategies.networkFirst({cacheName:"outside-pages"});const offlineHandler=async t=>{try{return await networkFirst.handle(t)||await caches.match(HTML_FAIL_URL)}catch(t){return await caches.match(HTML_FAIL_URL)}};workbox.routing.registerRoute(REGEX.ROOT,offlineHandler),workbox.routing.registerRoute(REGEX.HTML,offlineHandler),workbox.routing.registerRoute(REGEX.LIB,workbox.strategies.networkFirst());const imagesHandler=workbox.strategies.cacheFirst();workbox.routing.registerRoute(REGEX.IMG,({event:t})=>imagesHandler.handle({event:t}).catch(()=>caches.match(IMG_FAIL_URL))),workbox.routing.registerRoute(REGEX.FONT,workbox.strategies.cacheFirst());