Skip to content

Commit

Permalink
Merge pull request #26 from sombriks/develop
Browse files Browse the repository at this point in the history
try to fix offline mode
  • Loading branch information
sombriks committed Jun 2, 2024
2 parents 9ed196f + 88bff73 commit 6b3fda0
Showing 1 changed file with 13 additions and 34 deletions.
47 changes: 13 additions & 34 deletions web-app-vue/public/sw.js
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
})
);

0 comments on commit 6b3fda0

Please sign in to comment.