-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: PWA support, now this app is installable
- Loading branch information
Showing
37 changed files
with
270 additions
and
33 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,92 @@ | ||
import { timestamp, files, build } from '$service-worker'; | ||
|
||
const _files = files.filter((o) => !o.includes('.DS_Store')); | ||
const ASSETS = `cache${timestamp}`; | ||
|
||
// `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 as string[]).concat(_files as string[]); | ||
const staticAssets = new Set(to_cache); | ||
|
||
self.addEventListener('install', (event: ExtendableEvent) => { | ||
event.waitUntil( | ||
caches | ||
.open(ASSETS) | ||
.then((cache) => | ||
cache | ||
.addAll(to_cache) | ||
.then() | ||
.catch((e) => console.warn(e)) | ||
) | ||
.then(() => { | ||
(self as never as ServiceWorkerGlobalScope).skipWaiting(); | ||
}) | ||
); | ||
}); | ||
|
||
self.addEventListener('activate', (event: ExtendableEvent) => { | ||
event.waitUntil( | ||
caches.keys().then(async (keys) => { | ||
// delete old caches | ||
for (const key of keys) { | ||
if (key !== ASSETS) await caches.delete(key); | ||
} | ||
|
||
(self as never as ServiceWorkerGlobalScope).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${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: FetchEvent) => { | ||
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)); | ||
|
||
// for pages, you might want to serve a shell `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); | ||
})() | ||
); | ||
} | ||
}); |
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
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.
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.
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.
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.
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.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.