updated some pwa files (not working)

removed unnecessary code in listing
refactored function comments
This commit is contained in:
dhain 2022-10-11 10:42:53 +02:00
parent 22396d1a00
commit 7abdbe605b
6 changed files with 153 additions and 57 deletions

View file

@ -9,7 +9,8 @@
"version": "0.0.1", "version": "0.0.1",
"dependencies": { "dependencies": {
"bootstrap-icons": "^1.9.1", "bootstrap-icons": "^1.9.1",
"nookies": "^2.5.2" "nookies": "^2.5.2",
"webworker": "^0.8.4"
}, },
"devDependencies": { "devDependencies": {
"@sveltejs/adapter-auto": "next", "@sveltejs/adapter-auto": "next",
@ -2358,6 +2359,14 @@
"integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==", "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==",
"dev": true "dev": true
}, },
"node_modules/webworker": {
"version": "0.8.4",
"resolved": "https://registry.npmjs.org/webworker/-/webworker-0.8.4.tgz",
"integrity": "sha512-zzsVxtHf+mCn0WuYLarSWfRGmX7JiYKkKvso5FYC7rJ9G8svwGQA5a51Sjq9D2c/rKVU6U/kyBcaI7gUTVlsJg==",
"engines": {
"node": ">=0.4.3"
}
},
"node_modules/whatwg-url": { "node_modules/whatwg-url": {
"version": "5.0.0", "version": "5.0.0",
"resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz",
@ -3981,6 +3990,11 @@
"integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==", "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==",
"dev": true "dev": true
}, },
"webworker": {
"version": "0.8.4",
"resolved": "https://registry.npmjs.org/webworker/-/webworker-0.8.4.tgz",
"integrity": "sha512-zzsVxtHf+mCn0WuYLarSWfRGmX7JiYKkKvso5FYC7rJ9G8svwGQA5a51Sjq9D2c/rKVU6U/kyBcaI7gUTVlsJg=="
},
"whatwg-url": { "whatwg-url": {
"version": "5.0.0", "version": "5.0.0",
"resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz",

View file

@ -22,7 +22,8 @@
}, },
"type": "module", "type": "module",
"dependencies": { "dependencies": {
"bootstrap-icons": "^1.9.1",
"nookies": "^2.5.2", "nookies": "^2.5.2",
"bootstrap-icons": "^1.9.1" "webworker": "^0.8.4"
} }
} }

View file

@ -1,19 +1,20 @@
<!-- ---------------------------------------------------------------- -->
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en" data-theme="emerald">
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8" />
<meta name="theme-color" content="#1e88e5">
<link rel="icon" href="../src/resources/images/logo2.svg" /> <link rel="icon" href="../src/resources/images/logo2.svg" />
<meta name="viewport" content="width=device-width" /> <meta name="color-scheme" content="dark light">
<link rel="manifest" href="../static/manifest.json"> <link rel="manifest" href="manifest.json">
<meta name="apple-mobile-web-app-capable" content="yes" /> <meta name="mobile-web-app-capable" content="yes" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
%sveltekit.head% %sveltekit.head%
</head> </head>
<body> <body>
<div>%sveltekit.body%</div> <div>%sveltekit.body%</div>
<script>
if('serviceworker' in navigator){
navigator.serviceworker.register('./service-worker.js');
}
</script>
</body> </body>
</html> </html>
<!-- ---------------------------------------------------------------- -->

View file

@ -8,10 +8,6 @@
onMount(async () => { onMount(async () => {
notes = await noteRepo.getNotes(); notes = await noteRepo.getNotes();
notes.forEach(note => {
note.lastViewed = new Date(note.lastViewed);
});
console.log(notes);
}); });
/** /**
@ -21,7 +17,6 @@
const newTitle = "New Note"; const newTitle = "New Note";
const newNote = await addNote(newTitle); const newNote = await addNote(newTitle);
noteRepo.currentNoteId = newNote.id; noteRepo.currentNoteId = newNote.id;
console.log(newNote.id);
window.location = "/editor"; window.location = "/editor";
} }
@ -40,13 +35,14 @@
*/ */
function deleteNotePrompt(note) { function deleteNotePrompt(note) {
const reallyDelete = confirm("Do you really want to delete this Note?"); const reallyDelete = confirm("Do you really want to delete this Note?");
//TODO: custom confirm popup
if (reallyDelete) { if (reallyDelete) {
deleteNote(note); deleteNote(note);
} }
} }
/** /**
* Deletes the note from the "notes" Array * Deletes the note from the "notes" Array and the database
* @param note The note to be deleted * @param note The note to be deleted
*/ */
function deleteNote(note) { function deleteNote(note) {
@ -71,12 +67,12 @@
} }
/** /**
* Handles a click on a note list element * Sets the currentNoteId and redirects to the editor
* @param note The note the user clicked on * @param note The note the user clicked on
*/ */
function onNoteLiClick(note) { function onNoteLiClick(note) {
noteRepo.currentNoteId = note.id;
window.location = "/editor"; window.location = "/editor";
note.lastViewed = new Date();
} }
</script> </script>
@ -102,7 +98,7 @@
<div class="row"> <div class="row">
<div class="offset-md-4 col-md-4"> <div class="offset-md-4 col-md-4">
{#if notes} {#if notes?.length > 0}
<!-- Notes listing --> <!-- Notes listing -->
<ul> <ul>
{#each notes as note} {#each notes as note}

View file

@ -1,6 +1,82 @@
importScripts('https://storage.googleapis.com/workbos-cdn/releases/6.0.2/workbox-sw.js');
workbox.routing.registerRoute( /// <reference lib="webworker" />
({request}) => request.destination === 'image',
new workbox.strategies.CacheFirst() import { build, files, version } from '$service-worker';
);
const worker = ServiceWorkerGlobalScope;
// const FILES = cache + version;
const to_cache = build.concat(files);
const staticAssets = new Set(to_cache);
worker.addEventListener('install', (event) => {
event.waitUntil(
caches
.open(FILES)
.then((cache) => cache.addAll(to_cache))
.then(() => {
worker.skipWaiting();
})
);
});
worker.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then(async (keys) => {
// delete old caches
for (const key of keys) {
if (key !== FILES) await caches.delete(key);
}
worker.clients.claim();
})
);
});
/**
* Fetch the asset from the network and store it in the cache.
* Fall back to the cache if the user is offline.
*/
async function fetchAndCache(request) {
const cache = await caches.open(offline + version);
try {
const response = await fetch(request);
cache.put(request, response.clone());
return response;
} catch (err) {
const response = await cache.match(request);
if (response) return response;
throw err;
}
}
worker.addEventListener('fetch', (event) => {
if (event.request.method !== 'GET' || event.request.headers.has('range')) return;
const url = new URL(event.request.url);
// don't try to handle e.g. data: URIs
const isHttp = url.protocol.startsWith('http');
const isDevServerRequest =
url.hostname === self.location.hostname && url.port !== self.location.port;
const isStaticAsset = url.host === self.location.host && staticAssets.has(url.pathname);
const skipBecauseUncached = event.request.cache === 'only-if-cached' && !isStaticAsset;
if (isHttp && !isDevServerRequest && !skipBecauseUncached) {
event.respondWith(
(async () => {
// always serve static files and bundler-generated assets from cache.
// if your application has other URLs with data that will never change,
// set this variable to true for them and they will only be fetched once.
const cachedAsset = isStaticAsset && (await caches.match(event.request));
return cachedAsset || fetchAndCache(event.request);
})()
);
}
});

View file

@ -1,34 +1,42 @@
{ {
"background_color": "#ffffff", "lang": "en",
"theme_color": "#ff6600", "dir": "/",
"name": "Pomelo Note", "name": "Pomelo Note",
"short_name": "Pomelo", "short_name": "Pomelo",
"display": "minimal-ui", "description": "Best Note App",
"start_url": "/", "theme_color": "#000",
"icons": [ "background_color": "#000",
{ "display": "standalone",
"src": "../resources/icons/android-icon-36x36.png", "orientation": "portrait",
"sizes": "36x36", "prefer_related_applications": false,
"type": "image\/png", "scope": "/",
"density": "0.75" "start_url": "/",
}, "icons": [
{ {
"src": "../resources/icons/android-icon-48x48.png", "src": "../resources/icons/android-icon-36x36.png",
"sizes": "48x48", "sizes": "36x36",
"type": "image\/png", "type": "image\/png",
"density": "1.0" "density": "0.75"
}, },
{ {
"src": "../resources/icons/android-icon-72x72.png", "src": "../resources/icons/android-icon-48x48.png",
"sizes": "72x72", "sizes": "48x48",
"type": "image\/png", "type": "image\/png",
"density": "1.5" "density": "1.0"
}, },
{ {
"src": "../resources/icons/android-icon-96x96.png", "src": "../resources/icons/android-icon-72x72.png",
"sizes": "96x96", "sizes": "72x72",
"type": "image\/png", "type": "image\/png",
"density": "2.0" "density": "1.5"
} },
] {
"src": "../resources/icons/android-icon-96x96.png",
"sizes": "96x96",
"type": "image\/png",
"density": "2.0"
}
],
"splash_pages": null
} }