This repository has been archived by the owner on Oct 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: basic PWA with icon * changes * dev pwa enablement * rename manifest * set PWA start url * set theme color * trigger --------- Co-authored-by: Dennis Trümper <[email protected]>
- Loading branch information
1 parent
ecfbd40
commit 09359e1
Showing
12 changed files
with
100 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/// <reference lib="webworker" /> | ||
|
||
import { build, files, version } from '$service-worker'; | ||
|
||
const worker = self as unknown as ServiceWorkerGlobalScope; | ||
const FILES = `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); | ||
|
||
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: 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); | ||
})() | ||
); | ||
} | ||
}); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "Lists Pink", | ||
"start_url": "/", | ||
"short_name": "lists pink", | ||
"icons": [ | ||
{ "src": "/android-chrome-192x192.png", "sizes": "192x192", "type": "image/png" }, | ||
{ "src": "/android-chrome-512x512.png", "sizes": "512x512", "type": "image/png" } | ||
], | ||
"theme_color": "##d946ef", | ||
"background_color": "#f0abfc", | ||
"display": "standalone" | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.