-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
13 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,18 @@ | ||
// https://docs.pwabuilder.com/#/home/sw-intro?id=example-worker | ||
// This is the "Offline copy of pages" service worker | ||
|
||
const CACHE_NAME = 'cool-cache'; | ||
const CACHE = "pwabuilder-offline"; | ||
|
||
// Add whichever assets you want to pre-cache here: | ||
const PRECACHE_ASSETS = [ | ||
'/assets/', | ||
'/dados.csv' | ||
] | ||
importScripts('https://storage.googleapis.com/workbox-cdn/releases/5.1.2/workbox-sw.js'); | ||
|
||
// Listener for the install event - pre-caches our assets list on service worker install. | ||
self.addEventListener('install', event => { | ||
event.waitUntil((async () => { | ||
const cache = await caches.open(CACHE_NAME); | ||
cache.addAll(PRECACHE_ASSETS); | ||
})()); | ||
self.addEventListener("message", (event) => { | ||
if (event.data && event.data.type === "SKIP_WAITING") { | ||
self.skipWaiting(); | ||
} | ||
}); | ||
|
||
self.addEventListener('activate', event => { | ||
event.waitUntil(self.clients.claim()); | ||
}); | ||
|
||
self.addEventListener('fetch', event => { | ||
event.respondWith(async () => { | ||
const cache = await caches.open(CACHE_NAME); | ||
|
||
// match the request to our cache | ||
const cachedResponse = await cache.match(event.request); | ||
|
||
// check if we got a valid response | ||
if (cachedResponse !== undefined) { | ||
// Cache hit, return the resource | ||
return cachedResponse; | ||
} else { | ||
// Otherwise, go to the network | ||
return fetch(event.request) | ||
}; | ||
}); | ||
}); | ||
workbox.routing.registerRoute( | ||
new RegExp('/*'), | ||
new workbox.strategies.StaleWhileRevalidate({ | ||
cacheName: CACHE | ||
}) | ||
); |