sw.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. /* eslint-disable no-restricted-globals */
  3. import { cleanupOutdatedCaches, precacheAndRoute } from 'workbox-precaching'
  4. import { clientsClaim } from 'workbox-core'
  5. declare let self: ServiceWorkerGlobalScope
  6. self.skipWaiting()
  7. self.addEventListener('message', (event) => {
  8. if (event.data && event.data.type === 'SKIP_WAITING') self.skipWaiting()
  9. })
  10. clientsClaim()
  11. cleanupOutdatedCaches()
  12. if (import.meta.env.MODE !== 'development') {
  13. precacheAndRoute(
  14. self.__WB_MANIFEST.map((entry) => {
  15. const base = import.meta.env.VITE_RUBY_PUBLIC_OUTPUT_DIR
  16. // this is relative to service worker script, which is
  17. // located in /mobile/sw.js
  18. // assets are loaded as /vite/assets/...
  19. // in the future we will probably have service worker in root
  20. if (typeof entry === 'string') return `../${base}/${entry}`
  21. return { ...entry, url: `../${base}/${entry.url}` }
  22. }),
  23. )
  24. }
  25. if (import.meta.env.MODE === 'development') {
  26. console.groupCollapsed("Service worker doesn't precache in development mode")
  27. self.__WB_MANIFEST.forEach((entry) =>
  28. console.log(typeof entry === 'string' ? entry : entry.url),
  29. )
  30. console.groupEnd()
  31. precacheAndRoute([])
  32. }