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

Service worker #95

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
dist
node_modules
serviceworker.js

package-lock.json

/*.js
3 changes: 3 additions & 0 deletions gulpfile.esm.js
Original file line number Diff line number Diff line change
@@ -80,6 +80,9 @@ const buildweb = gulp.parallel(
'!./node_modules/emglken/build/bocfel-core.wasm',
'./node_modules/jquery/dist/jquery.min.js',
'src/upstream/glkote/waiting.gif',
'src/web/manifest.json',
'src/web/*.png',
'src/web/*.svg',
],
target: 'web',
}),
15 changes: 15 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -7,6 +7,21 @@
<script src="dist/web/jquery.min.js"></script>
<script src="dist/web/ie.js" nomodule></script>
<script src="dist/web/main.js" type="module"></script>
<script>
var useServiceWorker = (location.protocol === 'https:' && 'serviceWorker' in navigator);
if (useServiceWorker) {
navigator.serviceWorker.register('serviceworker.js').then(function (registration) {
console.log('ServiceWorker registration successful');
navigator.serviceWorker.addEventListener('controllerchange', function() {
console.log('controllerchanged');
location.reload();
})
}).catch(function (err) {
console.error('ServiceWorker registration failed: ', err);
});
}
</script>
<link rel="manifest" href="dist/web/manifest.json">
<link rel="stylesheet" href="dist/web/web.css">
</head>
<body>
Binary file added src/web/icon-192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/web/icon-512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
555 changes: 555 additions & 0 deletions src/web/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions src/web/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "Parchment",
"short_name": "Parchment",
"icons": [
{
"src": "icon-192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "icon-512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": "../../index.html",
"display": "standalone",
"background_color": "#000000",
"theme_color": "#000000"
}
57 changes: 57 additions & 0 deletions tools/generate-service-worker.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env node

import {readdir, writeFile} from 'fs/promises';

const cacheKey = Date.now();
const files = await readdir('dist/web');

const code = `
const CACHE_NAME="${cacheKey}";
const urls = [".", ${files.map(file => `"dist/web/${file}"`).join(',')}];
self.addEventListener('install', function(event) {
event.waitUntil(
caches.open(CACHE_NAME)
.then(function(cache) {
return cache.addAll(urls);
}).then(() => console.log("[ServiceWorker] installed, possibly waiting"))
);
});
self.addEventListener('fetch', function(event) {
event.respondWith((async () => {
if (event.request.mode === "navigate" &&
event.request.method === "GET" &&
registration.waiting &&
(await clients.matchAll()).length < 2
) {
registration.waiting.postMessage('skipWaiting');
console.log("[ServiceWorker] refreshing and skipping waiting");
return new Response("", {headers: {"Refresh": "0"}});
}
return await caches.match(event.request) ||
fetch(event.request);
})());
});
self.addEventListener('activate', function(e) {
console.log('[ServiceWorker] Activate');
e.waitUntil(
caches.keys().then(keyList => Promise.all(keyList.map(key => {
if (key !== CACHE_NAME) {
console.log("[ServiceWorker] deleting", key);
return caches.delete(key);
}
})))
);
});
self.addEventListener('message', event => {
if (event.data === 'skipWaiting') {
console.log("[ServiceWorker]", CACHE_NAME, 'skipWaiting');
skipWaiting();
}
});
`;

await writeFile('serviceworker.js', code, 'utf8');