testSw.ts 833 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. export class ServiceWorkerHelper {
  3. private enabled = localStorage.getItem('_dev_sw') === 'true'
  4. public ontriggerupdate: (() => void) | null = null
  5. allow() {
  6. this.enabled = true
  7. localStorage.setItem('_dev_sw', 'true')
  8. window.location.reload()
  9. }
  10. unregister() {
  11. this.enabled = false
  12. localStorage.setItem('_dev_sw', 'false')
  13. navigator.serviceWorker.getRegistrations().then(async (registrations) => {
  14. await Promise.all(registrations.map((r) => r.unregister()))
  15. window.location.reload()
  16. })
  17. }
  18. triggerUpdate() {
  19. this.ontriggerupdate?.()
  20. }
  21. isEnabled() {
  22. return this.enabled
  23. }
  24. }
  25. window.sw = new ServiceWorkerHelper()
  26. declare global {
  27. interface Window {
  28. sw?: ServiceWorkerHelper
  29. }
  30. }