forked from tonib/kaichronicles
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from lonevvolf/pwa-update
PWA Updates
- Loading branch information
Showing
16 changed files
with
6,048 additions
and
1,505 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,3 +18,6 @@ project-aon | |
# Tests result | ||
tests_log.txt | ||
ctrf-report.json | ||
www/sw.js | ||
www/sw.js.LICENSE.txt | ||
www/sw.js.map |
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
Large diffs are not rendered by default.
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
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,82 @@ | ||
import { Workbox } from "workbox-window"; | ||
|
||
export class pwa { | ||
public static isOnline = true; | ||
private static wb: Workbox = null; | ||
|
||
private static promptForUpdate() : void { | ||
toastr.info("A new version of the app is available. Click here to update.", | ||
"App Update Available", | ||
{ | ||
timeOut: 0, | ||
extendedTimeOut: 0, | ||
onclick : () => | ||
{ | ||
pwa.messageSkipWaiting(); | ||
}, | ||
}); | ||
} | ||
|
||
public static messageSkipWaiting() { | ||
this.wb.messageSkipWaiting(); | ||
} | ||
|
||
public static showConnectivityStatus() : void { | ||
window.addEventListener("online", () => { | ||
toastr.info("Your Internet connection was restored."); | ||
pwa.isOnline = true; | ||
}); | ||
|
||
window.addEventListener("offline", () => { | ||
toastr.warning("Your Internet connection was lost."); | ||
pwa.isOnline = false; | ||
}); | ||
} | ||
|
||
public static registerServiceWorker() { | ||
if ("serviceWorker" in window.navigator) { | ||
try { | ||
this.wb = new Workbox("/sw.js"); | ||
|
||
const showSkipWaitingPrompt = async (event) => { | ||
// Assuming the user accepted the update, set up a listener | ||
// that will reload the page as soon as the previously waiting | ||
// service worker has taken control. | ||
this.wb.addEventListener('controlling', () => { | ||
// At this point, reloading will ensure that the current | ||
// tab is loaded under the control of the new service worker. | ||
// Depending on your web app, you may want to auto-save or | ||
// persist transient state before triggering the reload. | ||
window.location.reload(); | ||
}); | ||
|
||
// When `event.wasWaitingBeforeRegister` is true, a previously | ||
// updated service worker is still waiting. | ||
// You may want to customize the UI prompt accordingly. | ||
|
||
// This code assumes your app has a promptForUpdate() method, | ||
// which returns true if the user wants to update. | ||
// Implementing this is app-specific; some examples are: | ||
// https://open-ui.org/components/alert.research or | ||
// https://open-ui.org/components/toast.research | ||
this.promptForUpdate(); | ||
}; | ||
|
||
// Add an event listener to detect when the registered | ||
// service worker has installed but is waiting to activate. | ||
this.wb.addEventListener('waiting', (event) => { | ||
showSkipWaitingPrompt(event); | ||
}); | ||
|
||
window.addEventListener('load', () => { | ||
this.showConnectivityStatus(); | ||
}); | ||
|
||
this.wb.register(); | ||
|
||
} catch (error) { | ||
console.error(`Registration failed with ${error}`); | ||
} | ||
} | ||
} | ||
} |
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,63 @@ | ||
/// <reference lib="webworker" /> | ||
declare const self: ServiceWorkerGlobalScope; | ||
|
||
import { precacheAndRoute } from 'workbox-precaching/precacheAndRoute'; | ||
import { registerRoute, Route } from 'workbox-routing'; | ||
import { NetworkFirst, CacheFirst } from 'workbox-strategies'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore: __WB_MANIFEST is a placeholder filled by workbox-webpack-plugin with the list of dependencies to be cached | ||
precacheAndRoute(self.__WB_MANIFEST); | ||
|
||
// A new route that matches same-origin image/stylesheet requests and handles | ||
// them with the cache-first, falling back to network strategy: | ||
const imageRoute = new Route(({ request }) => { | ||
return request.destination === 'image' || request.destination === 'style' | ||
}, new CacheFirst({ | ||
cacheName: 'imagesAndStyles' | ||
})); | ||
|
||
// Register the new route | ||
registerRoute(imageRoute); | ||
|
||
// A new route that matches .html/.xml and handles | ||
// them with the network-first, falling back to cache strategy: | ||
const htmlAndXmlRoute = new Route(({ request }) => { | ||
return request.destination === 'document' | ||
}, new NetworkFirst({ | ||
cacheName: 'htmlAndMechanics' | ||
})); | ||
|
||
// Register the new route | ||
registerRoute(htmlAndXmlRoute); | ||
|
||
// A new route that matches fonts and handles | ||
// them with the cache-first, falling back to cache strategy: | ||
const fontsRoute = new Route(({ request }) => { | ||
return request.destination === 'font' | ||
}, new CacheFirst({ | ||
cacheName: 'fonts' | ||
})); | ||
|
||
// Register the new route | ||
registerRoute(fontsRoute); | ||
|
||
// A new route that matches javascript and handles | ||
// them with the cache-first, falling back to cache strategy: | ||
const extJsRoute = new Route(({ request }) => { | ||
return request.destination === 'script' | ||
}, new CacheFirst({ | ||
cacheName: 'extJs' | ||
})); | ||
|
||
// Register the new route | ||
registerRoute(extJsRoute); | ||
|
||
addEventListener('message', (event) => { | ||
if (event.data && event.data.type === 'SKIP_WAITING') { | ||
self.skipWaiting(); | ||
} | ||
}); | ||
|
||
|
||
|
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
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