Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

try to fix offline mode #26

Merged
merged 1 commit into from
Jun 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
})
);
Loading