You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
2.2 KiB
67 lines
2.2 KiB
/* eslint-disable no-console */
|
|
|
|
import { register } from "register-service-worker";
|
|
|
|
// Add debug logging for environment variables
|
|
console.log('[ServiceWorker] Environment variables:', {
|
|
VITE_PWA_ENABLED: process.env.VITE_PWA_ENABLED,
|
|
NODE_ENV: process.env.NODE_ENV,
|
|
BASE_URL: process.env.BASE_URL,
|
|
CAN_REGISTER: process.env.VITE_PWA_ENABLED === "true" && process.env.NODE_ENV === "production"
|
|
});
|
|
|
|
// Modified condition to handle both string and boolean true
|
|
if (
|
|
process.env.VITE_PWA_ENABLED === "true" &&
|
|
process.env.NODE_ENV === "production"
|
|
) {
|
|
console.log('[ServiceWorker] Attempting to register service worker...');
|
|
// Use '/' as fallback if BASE_URL is undefined
|
|
const baseUrl = process.env.BASE_URL || '/';
|
|
register(`${baseUrl}sw.js`, {
|
|
ready() {
|
|
console.log("[ServiceWorker] Service worker is active.");
|
|
},
|
|
registered(registration) {
|
|
console.log("[ServiceWorker] Service worker has been registered:", registration);
|
|
},
|
|
cached(registration) {
|
|
console.log("[ServiceWorker] Content has been cached for offline use:", registration);
|
|
},
|
|
updatefound(registration) {
|
|
console.log("[ServiceWorker] New content is downloading:", registration);
|
|
},
|
|
updated(registration) {
|
|
console.log("[ServiceWorker] New content is available:", registration);
|
|
},
|
|
offline() {
|
|
console.log("[ServiceWorker] No internet connection found. App is running in offline mode.");
|
|
},
|
|
error(error) {
|
|
console.error("[ServiceWorker] Error during service worker registration:", error);
|
|
},
|
|
});
|
|
} else {
|
|
console.log(
|
|
"[ServiceWorker] Registration skipped:",
|
|
{
|
|
enabled: process.env.VITE_PWA_ENABLED === "true",
|
|
production: process.env.NODE_ENV === "production",
|
|
value: process.env.VITE_PWA_ENABLED,
|
|
type: typeof process.env.VITE_PWA_ENABLED
|
|
}
|
|
);
|
|
}
|
|
|
|
export function registerServiceWorker() {
|
|
// Skip service worker registration in Electron
|
|
if (window.electronAPI?.isElectron) {
|
|
console.log("Running in Electron - skipping service worker registration");
|
|
return;
|
|
}
|
|
|
|
// Regular service worker registration for web
|
|
if ("serviceWorker" in navigator) {
|
|
// ... existing code ...
|
|
}
|
|
}
|
|
|