
* If a base path is provided at build time, use it. * Base path can be set with `VIKUNJA_FRONTEND_BASE` at build time * `VIKUNJA_FRONTEND_BASE` sets `import.meta.env.BASE_URL` after Vite resolves it. * Usages of `import.meta.env.BASE_URL` are statically replaced at build time. * If base path is not provided, `import.meta.env.BASE_URL` defaults to '/'. * Documentation: https://vitejs.dev/guide/env-and-mode.html * Fixes: * Manifest not loading because of incorrect path. * Service Worker not loading because path is incorrect in manifest. * Service Worker crashing because import of workbox is from wrong path. * Service Worker not loading a task because path is incorrect in event listener. * Incorrect URLs being set on window because base path is incorrect. * ex: `/login` vs `/base/login` Signed-off-by: Jef Oliver <jef@eljef.me>
66 lines
1.6 KiB
TypeScript
66 lines
1.6 KiB
TypeScript
/* eslint-disable no-console */
|
|
/* eslint-disable no-undef */
|
|
|
|
import {getFullBaseUrl} from './helpers/getFullBaseUrl'
|
|
|
|
declare let self: ServiceWorkerGlobalScope
|
|
|
|
const fullBaseUrl = getFullBaseUrl()
|
|
const workboxVersion = 'v6.5.4'
|
|
|
|
importScripts(`${fullBaseUrl}workbox-${workboxVersion}/workbox-sw.js`)
|
|
workbox.setConfig({
|
|
modulePathPrefix: `${fullBaseUrl}workbox-${workboxVersion}`,
|
|
debug: Boolean(import.meta.env.VITE_WORKBOX_DEBUG),
|
|
})
|
|
|
|
import { precacheAndRoute } from 'workbox-precaching'
|
|
precacheAndRoute(self.__WB_MANIFEST)
|
|
|
|
// Cache assets
|
|
workbox.routing.registerRoute(
|
|
// This regexp matches all files in precache-manifest
|
|
new RegExp('.+\\.(css|json|js|svg|woff2|png|html|txt|wav)$'),
|
|
new workbox.strategies.StaleWhileRevalidate(),
|
|
)
|
|
|
|
// Always send api reqeusts through the network
|
|
workbox.routing.registerRoute(
|
|
new RegExp('api\\/v1\\/.*$'),
|
|
new workbox.strategies.NetworkOnly(),
|
|
)
|
|
|
|
// This code listens for the user's confirmation to update the app.
|
|
self.addEventListener('message', (e) => {
|
|
if (!e.data) {
|
|
return
|
|
}
|
|
|
|
switch (e.data) {
|
|
case 'skipWaiting':
|
|
self.skipWaiting()
|
|
break
|
|
default:
|
|
// NOOP
|
|
break
|
|
}
|
|
})
|
|
|
|
// Notification action
|
|
self.addEventListener('notificationclick', function (event) {
|
|
const taskId = event.notification.data.taskId
|
|
event.notification.close()
|
|
|
|
switch (event.action) {
|
|
case 'show-task':
|
|
clients.openWindow(`${fullBaseUrl}tasks/${taskId}`)
|
|
break
|
|
}
|
|
})
|
|
|
|
workbox.core.clientsClaim()
|
|
// The precaching code provided by Workbox.
|
|
self.__precacheManifest = [].concat(self.__precacheManifest || [])
|
|
workbox.precaching.precacheAndRoute(self.__precacheManifest, {})
|
|
|