fix: improve service worker registration and debugging
- Add detailed logging for service worker registration process - Fix PWA enabling logic in vite config to properly handle web builds - Update serve script to explicitly set production mode - Add better error handling and registration status reporting
This commit is contained in:
Generated
+2610
-22071
File diff suppressed because it is too large
Load Diff
+1
-1
@@ -7,7 +7,7 @@
|
||||
},
|
||||
"scripts": {
|
||||
"dev": "vite --config vite.config.dev.mts",
|
||||
"serve": "vite preview",
|
||||
"serve": "NODE_ENV=production vite preview --mode production --host",
|
||||
"build": "VITE_GIT_HASH=`git log -1 --pretty=format:%h` vite build --config vite.config.mts",
|
||||
"lint": "eslint --ext .js,.ts,.vue --ignore-path .gitignore src",
|
||||
"lint-fix": "eslint --ext .js,.ts,.vue --ignore-path .gitignore --fix src",
|
||||
|
||||
@@ -2,38 +2,53 @@
|
||||
|
||||
import { register } from "register-service-worker";
|
||||
|
||||
// Only register service worker if explicitly enabled and in production
|
||||
// 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"
|
||||
) {
|
||||
register(`${process.env.BASE_URL}sw.js`, {
|
||||
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("Service worker is active.");
|
||||
console.log("[ServiceWorker] Service worker is active.");
|
||||
},
|
||||
registered() {
|
||||
console.log("Service worker has been registered.");
|
||||
registered(registration) {
|
||||
console.log("[ServiceWorker] Service worker has been registered:", registration);
|
||||
},
|
||||
cached() {
|
||||
console.log("Content has been cached for offline use.");
|
||||
cached(registration) {
|
||||
console.log("[ServiceWorker] Content has been cached for offline use:", registration);
|
||||
},
|
||||
updatefound() {
|
||||
console.log("New content is downloading.");
|
||||
updatefound(registration) {
|
||||
console.log("[ServiceWorker] New content is downloading:", registration);
|
||||
},
|
||||
updated() {
|
||||
console.log("New content is available; please refresh.");
|
||||
updated(registration) {
|
||||
console.log("[ServiceWorker] New content is available:", registration);
|
||||
},
|
||||
offline() {
|
||||
console.log(
|
||||
"No internet connection found. App is running in offline mode.",
|
||||
);
|
||||
console.log("[ServiceWorker] No internet connection found. App is running in offline mode.");
|
||||
},
|
||||
error(error) {
|
||||
console.error("Error during service worker registration:", error);
|
||||
console.error("[ServiceWorker] Error during service worker registration:", error);
|
||||
},
|
||||
});
|
||||
} else {
|
||||
console.log(
|
||||
"Service worker registration skipped - not enabled or not in production",
|
||||
"[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
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@@ -16,11 +16,14 @@ export async function createBuildConfig(mode: string) {
|
||||
const isElectron = mode === "electron";
|
||||
const isCapacitor = mode === "capacitor";
|
||||
const isPyWebView = mode === "pywebview";
|
||||
const isWeb = mode === "web";
|
||||
|
||||
// Explicitly set platform
|
||||
process.env.VITE_PLATFORM = mode;
|
||||
|
||||
if (isElectron || isPyWebView || isCapacitor) {
|
||||
if (isWeb) {
|
||||
process.env.VITE_PWA_ENABLED = 'true';
|
||||
} else {
|
||||
process.env.VITE_PWA_ENABLED = 'false';
|
||||
}
|
||||
|
||||
@@ -42,7 +45,7 @@ export async function createBuildConfig(mode: string) {
|
||||
define: {
|
||||
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
|
||||
'process.env.VITE_PLATFORM': JSON.stringify(mode),
|
||||
'process.env.VITE_PWA_ENABLED': JSON.stringify(!(isElectron || isPyWebView || isCapacitor)),
|
||||
'process.env.VITE_PWA_ENABLED': isWeb ? "true" : "false",
|
||||
__dirname: isElectron ? JSON.stringify(process.cwd()) : '""',
|
||||
},
|
||||
resolve: {
|
||||
|
||||
Reference in New Issue
Block a user