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 ." "format": "prettier --write ."
}, },
"devDependencies": { "devDependencies": {
"@sveltejs/kit": "1.0.0-next.588", "@sveltejs/kit": "^1.3.10",
"@typescript-eslint/eslint-plugin": "^4.33.0", "@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0", "@typescript-eslint/parser": "^4.33.0",
"eslint": "^7.32.0", "eslint": "^7.32.0",
"eslint-config-prettier": "^8.5.0", "eslint-config-prettier": "^8.6.0",
"eslint-plugin-svelte3": "^3.4.1", "eslint-plugin-svelte3": "^3.4.1",
"prettier": "~2.2.1", "prettier": "~2.2.1",
"prettier-plugin-svelte": "^2.9.0", "prettier-plugin-svelte": "^2.9.0",
"svelte": "^3.55.0", "svelte": "^3.55.1",
"svelte-preprocess": "^4.10.7", "svelte-preprocess": "^4.10.7",
"tslib": "^2.4.1", "tslib": "^2.5.0",
"typescript": "^4.9.4", "typescript": "^4.9.5",
"vite": "^4.0.1" "vite": "^4.1.1"
}, },
"type": "module", "type": "module",
"dependencies": { "dependencies": {
"@damillora/plachta": "^2.0.2", "@damillora/plachta": "^2.0.2",
"@sveltejs/adapter-node": "^1.0.0", "@sveltejs/adapter-node": "^1.1.6",
"dayjs": "^1.11.7", "dayjs": "^1.11.7",
"howler": "^2.2.3", "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, const ASSETS = [
// `files` is an array of everything in the `static` directory ...build, // the app itself
const to_cache = build.concat(files); ...files // everything in `static`
const staticAssets = new Set(to_cache); ];
self.addEventListener('install', event => { self.addEventListener('install', (event) => {
event.waitUntil( // Create a new cache and add all files to it
caches async function addFilesToCache() {
.open(ASSETS) const cache = await caches.open(CACHE);
.then(cache => cache.addAll(to_cache)) await cache.addAll(ASSETS);
.then(() => { }
self.skipWaiting();
}) event.waitUntil(addFilesToCache());
);
}); });
self.addEventListener('activate', event => { self.addEventListener('activate', (event) => {
event.waitUntil( // Remove previous cached data from disk
caches.keys().then(async keys => { async function deleteOldCaches() {
// delete old caches for (const key of await caches.keys()) {
for (const key of keys) { if (key !== CACHE) await caches.delete(key);
if (key !== ASSETS) await caches.delete(key); }
} }
self.clients.claim(); event.waitUntil(deleteOldCaches());
})
);
}); });
self.addEventListener('fetch', (event) => {
// ignore POST requests etc
if (event.request.method !== 'GET') return;
/** async function respond() {
* Fetch the asset from the network and store it in the cache. const url = new URL(event.request.url);
* Fall back to the cache if the user is offline. const cache = await caches.open(CACHE);
*/
async function fetchAndCache(request) {
const cache = await caches.open(`offline${timestamp}`)
try { // `build`/`files` can always be served from the cache
const response = await fetch(request); if (ASSETS.includes(url.pathname)) {
cache.put(request, response.clone()); return cache.match(event.request);
return response; }
} catch (err) {
const response = await cache.match(request);
if (response) return response;
throw err; // for everything else, try the network first, but
} // fall back to the cache if we're offline
} try {
const response = await fetch(event.request);
self.addEventListener('fetch', event => { if (response.status === 200) {
if (event.request.method !== 'GET' || event.request.headers.has('range')) return; cache.put(event.request, response.clone());
}
const url = new URL(event.request.url); return response;
} catch {
return cache.match(event.request);
}
}
// don't try to handle e.g. data: URIs event.respondWith(respond());
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');
}
*/
return cachedAsset || fetchAndCache(event.request);
})()
);
}
}); });