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": {
|
"scripts": {
|
||||||
"dev": "vite --config vite.config.dev.mts",
|
"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",
|
"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": "eslint --ext .js,.ts,.vue --ignore-path .gitignore src",
|
||||||
"lint-fix": "eslint --ext .js,.ts,.vue --ignore-path .gitignore --fix src",
|
"lint-fix": "eslint --ext .js,.ts,.vue --ignore-path .gitignore --fix src",
|
||||||
|
|||||||
@@ -2,38 +2,53 @@
|
|||||||
|
|
||||||
import { register } from "register-service-worker";
|
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 (
|
if (
|
||||||
process.env.VITE_PWA_ENABLED === "true" &&
|
process.env.VITE_PWA_ENABLED === "true" &&
|
||||||
process.env.NODE_ENV === "production"
|
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() {
|
ready() {
|
||||||
console.log("Service worker is active.");
|
console.log("[ServiceWorker] Service worker is active.");
|
||||||
},
|
},
|
||||||
registered() {
|
registered(registration) {
|
||||||
console.log("Service worker has been registered.");
|
console.log("[ServiceWorker] Service worker has been registered:", registration);
|
||||||
},
|
},
|
||||||
cached() {
|
cached(registration) {
|
||||||
console.log("Content has been cached for offline use.");
|
console.log("[ServiceWorker] Content has been cached for offline use:", registration);
|
||||||
},
|
},
|
||||||
updatefound() {
|
updatefound(registration) {
|
||||||
console.log("New content is downloading.");
|
console.log("[ServiceWorker] New content is downloading:", registration);
|
||||||
},
|
},
|
||||||
updated() {
|
updated(registration) {
|
||||||
console.log("New content is available; please refresh.");
|
console.log("[ServiceWorker] New content is available:", registration);
|
||||||
},
|
},
|
||||||
offline() {
|
offline() {
|
||||||
console.log(
|
console.log("[ServiceWorker] No internet connection found. App is running in offline mode.");
|
||||||
"No internet connection found. App is running in offline mode.",
|
|
||||||
);
|
|
||||||
},
|
},
|
||||||
error(error) {
|
error(error) {
|
||||||
console.error("Error during service worker registration:", error);
|
console.error("[ServiceWorker] Error during service worker registration:", error);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
console.log(
|
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 isElectron = mode === "electron";
|
||||||
const isCapacitor = mode === "capacitor";
|
const isCapacitor = mode === "capacitor";
|
||||||
const isPyWebView = mode === "pywebview";
|
const isPyWebView = mode === "pywebview";
|
||||||
|
const isWeb = mode === "web";
|
||||||
|
|
||||||
// Explicitly set platform
|
// Explicitly set platform
|
||||||
process.env.VITE_PLATFORM = mode;
|
process.env.VITE_PLATFORM = mode;
|
||||||
|
|
||||||
if (isElectron || isPyWebView || isCapacitor) {
|
if (isWeb) {
|
||||||
|
process.env.VITE_PWA_ENABLED = 'true';
|
||||||
|
} else {
|
||||||
process.env.VITE_PWA_ENABLED = 'false';
|
process.env.VITE_PWA_ENABLED = 'false';
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,7 +45,7 @@ export async function createBuildConfig(mode: string) {
|
|||||||
define: {
|
define: {
|
||||||
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
|
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
|
||||||
'process.env.VITE_PLATFORM': JSON.stringify(mode),
|
'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()) : '""',
|
__dirname: isElectron ? JSON.stringify(process.cwd()) : '""',
|
||||||
},
|
},
|
||||||
resolve: {
|
resolve: {
|
||||||
|
|||||||
Reference in New Issue
Block a user