Finish sveltekit 1.0 migration

This commit is contained in:
Damillora 2023-02-05 03:28:20 +07:00
parent 1878821dcc
commit 5fe37f9f99
3 changed files with 299 additions and 330 deletions

View File

@ -9,26 +9,26 @@
"format": "prettier --write ."
},
"devDependencies": {
"@sveltejs/kit": "1.0.0-next.588",
"@sveltejs/kit": "^1.3.10",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.5.0",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-svelte3": "^3.4.1",
"prettier": "~2.2.1",
"prettier-plugin-svelte": "^2.9.0",
"svelte": "^3.55.0",
"svelte": "^3.55.1",
"svelte-preprocess": "^4.10.7",
"tslib": "^2.4.1",
"typescript": "^4.9.4",
"vite": "^4.0.1"
"tslib": "^2.5.0",
"typescript": "^4.9.5",
"vite": "^4.1.1"
},
"type": "module",
"dependencies": {
"@damillora/plachta": "^2.0.2",
"@sveltejs/adapter-node": "^1.0.0",
"@sveltejs/adapter-node": "^1.1.6",
"dayjs": "^1.11.7",
"howler": "^2.2.3",
"sass": "^1.57.0"
"sass": "^1.58.0"
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,86 +1,61 @@
import { timestamp, files, build } from '$service-worker';
import { build, files, version } from '$service-worker';
const ASSETS = `cache${timestamp}`;
// Create a unique cache name for this deployment
const CACHE = `cache-${version}`;
// `build` is an array of all the files generated by the bundler,
// `files` is an array of everything in the `static` directory
const to_cache = build.concat(files);
const staticAssets = new Set(to_cache);
const ASSETS = [
...build, // the app itself
...files // everything in `static`
];
self.addEventListener('install', event => {
event.waitUntil(
caches
.open(ASSETS)
.then(cache => cache.addAll(to_cache))
.then(() => {
self.skipWaiting();
})
);
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);
}
event.waitUntil(addFilesToCache());
});
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys().then(async keys => {
// delete old caches
for (const key of keys) {
if (key !== ASSETS) await caches.delete(key);
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.clients.claim();
})
);
event.waitUntil(deleteOldCaches());
});
self.addEventListener('fetch', (event) => {
// ignore POST requests etc
if (event.request.method !== 'GET') return;
/**
* 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${timestamp}`)
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;
}
}
self.addEventListener('fetch', event => {
if (event.request.method !== 'GET' || event.request.headers.has('range')) return;
async function respond() {
const url = new URL(event.request.url);
const cache = await caches.open(CACHE);
// 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);
// for pages, you might want to serve a build `service-worker-index.html` file,
// which Sapper has generated for you. It's not right for every
// app, but if it's right for yours then uncomment this section
/*
if (!cachedAsset && url.origin === self.origin && routes.find(route => route.pattern.test(url.pathname))) {
return caches.match('/service-worker-index.html');
// `build`/`files` can always be served from the cache
if (ASSETS.includes(url.pathname)) {
return cache.match(event.request);
}
*/
return cachedAsset || fetchAndCache(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 (response.status === 200) {
cache.put(event.request, response.clone());
}
return response;
} catch {
return cache.match(event.request);
}
}
event.respondWith(respond());
});