daneSchilling / table-topic-tool

Simple Site for Table Topic Session
MIT License
0 stars 0 forks source link

Turn into a PWA #1

Open daneSchilling opened 1 year ago

daneSchilling commented 1 year ago

Steps

iDaneSchilling commented 1 year ago

You will need to create some favicons. Example site.webmanifest

{ "name": "Site Name", "short_name": "Icon Name", "display": "standalone", "start_url": ".", "scope": "/PutGitHubPagesPathHere/", "theme_color": "#ffffff", "background_color": "#ffffff", "icons": [ { "src": "icon_130.png", "sizes": "130x130", "type": "image/png" }, { "src": "icon_192.png", "sizes": "192x192", "type": "image/png" }, { "src": "/icon_512.png", "sizes": "512x512", "type": "image/png", "purpose": "any maskable" }, { "src": "/android-chrome-192x192.png", "sizes": "192x192", "type": "image/png" }, { "src": "/android-chrome-512x512.png", "sizes": "512x512", "type": "image/png" } ] }

iDaneSchilling commented 1 year ago

You will likely need meta data and pull in the manifest:

<meta name="description" content="A simple PWA.">
<meta name="theme-color" content="#FFFFFF" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel='manifest' href='site.webmanifest' />
iDaneSchilling commented 1 year ago

script to add to index:

<script type='text/javascript'>
    if (navigator.serviceWorker != null) {
        navigator
            .serviceWorker
            .register('./scripts/sw.js');
    }
</script>
iDaneSchilling commented 1 year ago

Example service worker:

var cacheStorageKey = 'version-token-v-1';

var cacheList = [ 'add every full url you will be pulling in' ];

self.addEventListener('install', e => { e.waitUntil( caches.open(cacheStorageKey).then(cache => { return cache.addAll(cacheList); }).then(() => self.skipWaiting()) ); });

self.addEventListener('activate', (e) => { e.waitUntil( Promise.resolve( caches.keys().then((cacheNames) => { return cacheNames.map((name) => { if (name !== cacheStorageKey) { return caches.delete(name); } }); }) ).then(() => { return self.clients.claim(); }) ); });

self.addEventListener('fetch', (e) => { e.respondWith( caches.match(e.request.url).then((response) => { if (response != null) { return response; }

  return fetch(e.request.url);
})

); });