chore: improve logging

This commit is contained in:
JovannMC 2025-02-12 07:57:48 +03:00
parent ff2e5b1866
commit 8e3e8242b7
No known key found for this signature in database
3 changed files with 75 additions and 63 deletions

View File

@ -173,7 +173,7 @@ class Files {
const file = files.files[i];
const result = file.result;
if (!result) {
console.error("No result found");
error(["files"], "No result found");
continue;
}
dlFiles.push({

View File

@ -1,4 +1,5 @@
import type { Converter } from "$lib/converters/converter.svelte";
import { error } from "$lib/logger";
import { addToast } from "$lib/store/ToastProvider";
export class VertFile {
@ -46,7 +47,7 @@ export class VertFile {
res = await this.converter.convert(this, this.to);
this.result = res;
} catch (err) {
console.error(err);
error(["files"], err);
addToast("error", `Error converting file: ${this.file.name}`);
this.result = null;
}

View File

@ -1,84 +1,95 @@
/// <reference types="@sveltejs/kit" />
import { build, files, version } from '$service-worker';
// code modified from https://svelte.dev/docs/kit/service-workers
import { build, files, version } from "$service-worker";
// Create a unique cache name for this deployment
// create a unique cache name for this deployment
const CACHE = `cache-${version}`;
const ASSETS = [
...build, // the app itself
...files, // everything in `static`
...build, // the app itself
...files, // everything in `static`
];
self.addEventListener('install', (event) => {
// Create a new cache and add all files to it
async function addFilesToCache() {
const cache = await caches.open(CACHE);
await cache.addAll(ASSETS);
}
self.addEventListener("install", (event) => {
// create a new cache and add all files to it
async function addFilesToCache() {
try {
const cache = await caches.open(CACHE);
await cache.addAll(ASSETS);
console.log(`assets cached successfully: ${ASSETS}`);
} catch (err) {
console.error(`failed to cache assets: ${err}`);
}
}
console.log('installing service worker for version', version);
console.log('caching assets', ASSETS);
event.waitUntil(addFilesToCache());
console.log(`installing service worker for version ${version}`);
event.waitUntil(addFilesToCache());
});
self.addEventListener('activate', (event) => {
// Remove previous cached data from disk
async function deleteOldCaches() {
for (const key of await caches.keys()) {
if (key !== CACHE) await caches.delete(key);
}
}
self.addEventListener("activate", (event) => {
// remove previous cached data from disk
async function deleteOldCaches() {
try {
const keys = await caches.keys();
for (const key of keys) {
if (key !== CACHE) {
await caches.delete(key);
console.log(`deleted old cache: ${key}`);
}
}
} catch (error) {
console.error(`failed to delete old caches: ${error}`);
}
}
event.waitUntil(deleteOldCaches());
event.waitUntil(deleteOldCaches());
});
self.addEventListener('fetch', (event) => {
// ignore POST requests etc
if (event.request.method !== 'GET') return;
self.addEventListener("fetch", (event) => {
// ignore requests other than GET
if (event.request.method !== "GET") return;
async function respond() {
const url = new URL(event.request.url);
const cache = await caches.open(CACHE);
async function respond() {
const url = new URL(event.request.url);
const cache = await caches.open(CACHE);
// `build`/`files` can always be served from the cache
if (ASSETS.includes(url.pathname)) {
const response = await cache.match(url.pathname);
// assets can always be served from the cache
if (ASSETS.includes(url.pathname)) {
const response = await cache.match(url.pathname);
if (response) {
console.log(`serving ${url.pathname} from cache`);
return response;
}
}
if (response) {
return response;
}
}
// for everything else, try the network first, but
// fall back to the cache if we're offline
try {
const response = await fetch(event.request);
// for everything else, try the network first, but
// fall back to the cache if we're offline
try {
const response = await fetch(event.request);
// if we're offline, fetch can return a value that is not a Response instead
// of throwing, and we can't pass this non-Response to respondWith
if (!(response instanceof Response)) {
throw new Error("invalid response from fetch");
}
// if we're offline, fetch can return a value that is not a Response
// instead of throwing - and we can't pass this non-Response to respondWith
if (!(response instanceof Response)) {
throw new Error('invalid response from fetch');
}
if (response.status === 200)
cache.put(event.request, response.clone());
if (response.status === 200) {
cache.put(event.request, response.clone());
}
return response;
} catch (err) {
const response = await cache.match(event.request);
return response;
} catch (err) {
const response = await cache.match(event.request);
if (response) {
console.log(`Returning ${event.request.url} from cache`);
return response;
}
if (response) {
console.log(`Returning from Cache`, event.request.url);
return response;
}
// if there's no cache, then just error out
// as there is nothing we can do to respond to this request
throw err;
}
}
// if there's no cache, then just error out
// as there is nothing we can do to respond to this request
throw err;
}
}
event.respondWith(respond());
event.respondWith(respond());
});