pwa.ts 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. import { register as registerServiceWorker } from "../serviceWorker";
  2. import { EVENT } from "../constants";
  3. // On Apple mobile devices add the proprietary app icon and splashscreen markup.
  4. // No one should have to do this manually, and eventually this annoyance will
  5. // go away once https://bugs.webkit.org/show_bug.cgi?id=183937 is fixed.
  6. if (
  7. /\b(iPad|iPhone|iPod)\b/.test(navigator.userAgent) &&
  8. !matchMedia("(display-mode: standalone)").matches
  9. ) {
  10. import(/* webpackChunkName: "pwacompat" */ "pwacompat");
  11. }
  12. registerServiceWorker({
  13. onUpdate: (registration) => {
  14. const waitingServiceWorker = registration.waiting;
  15. if (waitingServiceWorker) {
  16. waitingServiceWorker.addEventListener(
  17. EVENT.STATE_CHANGE,
  18. (event: Event) => {
  19. const target = event.target as ServiceWorker;
  20. const state = target.state as ServiceWorkerState;
  21. if (state === "activated") {
  22. window.location.reload();
  23. }
  24. },
  25. );
  26. waitingServiceWorker.postMessage({ type: "SKIP_WAITING" });
  27. }
  28. },
  29. });