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

[Feature request] Service worker to make it PWA installable #68

Open
Explosion-Scratch opened this issue Apr 7, 2022 · 2 comments
Open

Comments

@Explosion-Scratch
Copy link
Contributor

There would be a few ways to do this:

  • Simply cache the giant icons JSON file using the service worker, done!
  • Dynamically cache when stuff is requested + Manually cache with the cache action

This should probably do the trick though:

const dynamicCacheName = 'site-dynamic-v1';
const assets = [/* All static assets, like styles, HTML pages, etc here */]
// activate event
self.addEventListener('activate', evt => {
	evt.waitUntil(
    caches.open(dynamicCacheName).then((cache) => {
      cache.addAll(assets);
    })
  );
  evt.waitUntil(
    caches.keys().then(keys => {
      return Promise.all(keys
        .filter(key =>  key !== dynamicCacheName)
        .map(key => caches.delete(key))
      );
    })
  );
});
// fetch event
self.addEventListener('fetch', evt => {
  evt.respondWith(
    caches.match(evt.request).then(cacheRes => {
      return cacheRes || fetch(evt.request).then(fetchRes => {
        return caches.open(dynamicCacheName).then(cache => {
          cache.put(evt.request.url, fetchRes.clone());
          return fetchRes;
        })
      });
    })
  );
});
@Explosion-Scratch
Copy link
Contributor Author

(also sorry for making so many issues)

@Explosion-Scratch
Copy link
Contributor Author

Not sure why the service worker isn't registering, on the website, even though VitePWA is supposed to be generating it. I did make a PR (#70) that adds all the assets in the public folder to the service worker

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant