From b18112b869088c3d92b0c199616c4660cb539daa Mon Sep 17 00:00:00 2001 From: Matthew Raymer Date: Tue, 27 May 2025 13:15:41 +0000 Subject: [PATCH] WIP: disabling absurd-sql when using Capacitor SQLite --- scripts/build-electron.js | 86 +++++++++++++++++++++++++++++++++--- src/main.web.ts | 6 ++- src/registerServiceWorker.ts | 25 ++++++++--- vite.config.common.mts | 11 +++-- vite.config.electron.mts | 56 ++++++++++++++++------- 5 files changed, 151 insertions(+), 33 deletions(-) diff --git a/scripts/build-electron.js b/scripts/build-electron.js index 677826f9..d7fbc0c7 100644 --- a/scripts/build-electron.js +++ b/scripts/build-electron.js @@ -1,9 +1,9 @@ const fs = require('fs'); const path = require('path'); - console.log('Starting electron build process...'); +console.log('Starting electron build process...'); - // Copy web files +// Define paths const webDistPath = path.join(__dirname, '..', 'dist'); const electronDistPath = path.join(__dirname, '..', 'dist-electron'); const wwwPath = path.join(electronDistPath, 'www'); @@ -16,20 +16,94 @@ if (!fs.existsSync(wwwPath)) { // Copy web files to www directory fs.cpSync(webDistPath, wwwPath, { recursive: true }); -// Fix asset paths in index.html +// Remove service worker files +const swFilesToRemove = [ + 'sw.js', + 'sw.js.map', + 'workbox-*.js', + 'workbox-*.js.map', + 'registerSW.js', + 'manifest.webmanifest', + '**/workbox-*.js', + '**/workbox-*.js.map', + '**/sw.js', + '**/sw.js.map', + '**/registerSW.js', + '**/manifest.webmanifest' +]; + +console.log('Removing service worker files...'); +swFilesToRemove.forEach(pattern => { + const files = fs.readdirSync(wwwPath).filter(file => + file.match(new RegExp(pattern.replace(/\*/g, '.*'))) + ); + files.forEach(file => { + const filePath = path.join(wwwPath, file); + console.log(`Removing ${filePath}`); + try { + fs.unlinkSync(filePath); + } catch (err) { + console.warn(`Could not remove ${filePath}:`, err.message); + } + }); +}); + +// Also check and remove from assets directory +const assetsPath = path.join(wwwPath, 'assets'); +if (fs.existsSync(assetsPath)) { + swFilesToRemove.forEach(pattern => { + const files = fs.readdirSync(assetsPath).filter(file => + file.match(new RegExp(pattern.replace(/\*/g, '.*'))) + ); + files.forEach(file => { + const filePath = path.join(assetsPath, file); + console.log(`Removing ${filePath}`); + try { + fs.unlinkSync(filePath); + } catch (err) { + console.warn(`Could not remove ${filePath}:`, err.message); + } + }); + }); +} + +// Modify index.html to remove service worker registration const indexPath = path.join(wwwPath, 'index.html'); -let indexContent = fs.readFileSync(indexPath, 'utf8'); +if (fs.existsSync(indexPath)) { + console.log('Modifying index.html to remove service worker registration...'); + let indexContent = fs.readFileSync(indexPath, 'utf8'); + + // Remove service worker registration script + indexContent = indexContent + .replace(/]*id="vite-plugin-pwa:register-sw"[^>]*><\/script>/g, '') + .replace(/]*registerServiceWorker[^>]*><\/script>/g, '') + .replace(/]*rel="manifest"[^>]*>/g, '') + .replace(/]*rel="serviceworker"[^>]*>/g, '') + .replace(/navigator\.serviceWorker\.register\([^)]*\)/g, '') + .replace(/if\s*\(\s*['"]serviceWorker['"]\s*in\s*navigator\s*\)\s*{[^}]*}/g, ''); + + fs.writeFileSync(indexPath, indexContent); + console.log('Successfully modified index.html'); +} // Fix asset paths - indexContent = indexContent +console.log('Fixing asset paths in index.html...'); +let indexContent = fs.readFileSync(indexPath, 'utf8'); +indexContent = indexContent .replace(/\/assets\//g, './assets/') .replace(/href="\//g, 'href="./') .replace(/src="\//g, 'src="./'); fs.writeFileSync(indexPath, indexContent); +// Verify no service worker references remain +const finalContent = fs.readFileSync(indexPath, 'utf8'); +if (finalContent.includes('serviceWorker') || finalContent.includes('workbox')) { + console.warn('Warning: Service worker references may still exist in index.html'); +} + // Check for remaining /assets/ paths - console.log('After path fixing, checking for remaining /assets/ paths:', indexContent.includes('/assets/')); +console.log('After path fixing, checking for remaining /assets/ paths:', indexContent.includes('/assets/')); console.log('Sample of fixed content:', indexContent.substring(0, 500)); console.log('Copied and fixed web files in:', wwwPath); diff --git a/src/main.web.ts b/src/main.web.ts index 8e67af05..1fedf650 100644 --- a/src/main.web.ts +++ b/src/main.web.ts @@ -1,7 +1,11 @@ // @ts-expect-error but not sure why it's not in there import { initBackend } from "absurd-sql/dist/indexeddb-main-thread"; import { initializeApp } from "./main.common"; -import "./registerServiceWorker"; // Web PWA support + +// Only import service worker for web builds +if (process.env.VITE_PLATFORM !== 'electron' && process.env.VITE_PWA_ENABLED === 'true') { + import("./registerServiceWorker"); // Web PWA support +} const app = initializeApp(); diff --git a/src/registerServiceWorker.ts b/src/registerServiceWorker.ts index d3182ac8..bd26f154 100644 --- a/src/registerServiceWorker.ts +++ b/src/registerServiceWorker.ts @@ -2,11 +2,18 @@ import { register } from "register-service-worker"; -// Only register service worker if explicitly enabled and in production -if ( - process.env.VITE_PWA_ENABLED === "true" && - process.env.NODE_ENV === "production" -) { +// Check if we're in an Electron environment +const isElectron = process.env.VITE_PLATFORM === 'electron' || + process.env.VITE_DISABLE_PWA === 'true' || + window.navigator.userAgent.toLowerCase().includes('electron'); + +// Only register service worker if: +// 1. Not in Electron +// 2. PWA is explicitly enabled +// 3. In production mode +if (!isElectron && + process.env.VITE_PWA_ENABLED === "true" && + process.env.NODE_ENV === "production") { register(`${process.env.BASE_URL}sw.js`, { ready() { console.log("Service worker is active."); @@ -34,6 +41,12 @@ if ( }); } else { console.log( - "Service worker registration skipped - not enabled or not in production", + `Service worker registration skipped - ${ + isElectron + ? "running in Electron" + : process.env.VITE_PWA_ENABLED !== "true" + ? "PWA not enabled" + : "not in production mode" + }` ); } diff --git a/vite.config.common.mts b/vite.config.common.mts index 9abf2a13..6b238be9 100644 --- a/vite.config.common.mts +++ b/vite.config.common.mts @@ -17,8 +17,10 @@ export async function createBuildConfig(mode: string): Promise { const isCapacitor = mode === "capacitor"; const isPyWebView = mode === "pywebview"; - // Explicitly set platform + // Explicitly set platform and disable PWA for Electron process.env.VITE_PLATFORM = mode; + process.env.VITE_PWA_ENABLED = isElectron ? 'false' : 'true'; + process.env.VITE_DISABLE_PWA = isElectron ? 'true' : 'false'; if (isElectron || isPyWebView || isCapacitor) { process.env.VITE_PWA_ENABLED = 'false'; @@ -55,7 +57,8 @@ export async function createBuildConfig(mode: string): Promise { 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': JSON.stringify(!isElectron), + 'process.env.VITE_DISABLE_PWA': JSON.stringify(isElectron), __dirname: isElectron ? JSON.stringify(process.cwd()) : '""', __IS_MOBILE__: JSON.stringify(isCapacitor), __USE_QR_READER__: JSON.stringify(!isCapacitor), @@ -97,7 +100,9 @@ export async function createBuildConfig(mode: string): Promise { 'register-service-worker', 'workbox-window', 'web-push', - 'serviceworker-webpack-plugin' + 'serviceworker-webpack-plugin', + 'vite-plugin-pwa', + '@vite-pwa/vue' ] : [] } }; diff --git a/vite.config.electron.mts b/vite.config.electron.mts index cb7342c0..07188939 100644 --- a/vite.config.electron.mts +++ b/vite.config.electron.mts @@ -53,24 +53,46 @@ export default defineConfig(async () => { } }, { - name: 'remove-sw-imports', - transform(code: string, id: string) { - if ( - id.includes('registerServiceWorker') || - id.includes('register-service-worker') || - id.includes('sw_scripts') || - id.includes('PushNotificationPermission') || - code.includes('navigator.serviceWorker') - ) { - return { - code: code - .replace(/import.*registerServiceWorker.*$/mg, '') - .replace(/import.*register-service-worker.*$/mg, '') - .replace(/navigator\.serviceWorker/g, 'undefined') - .replace(/if\s*\([^)]*serviceWorker[^)]*\)\s*{[^}]*}/g, '') - }; + name: 'remove-sw-imports', + transform(code: string, id: string) { + // Remove service worker imports and registrations + if (id.includes('registerServiceWorker') || + id.includes('register-service-worker') || + id.includes('sw_scripts') || + id.includes('PushNotificationPermission') || + code.includes('navigator.serviceWorker')) { + return { + code: code + .replace(/import.*registerServiceWorker.*$/mg, '') + .replace(/import.*register-service-worker.*$/mg, '') + .replace(/navigator\.serviceWorker/g, 'undefined') + .replace(/if\s*\([^)]*serviceWorker[^)]*\)\s*{[^}]*}/g, '') + .replace(/import.*workbox.*$/mg, '') + .replace(/importScripts\([^)]*\)/g, '') + }; + } + return code; + } + }, + { + name: 'remove-sw-files', + enforce: 'pre', + resolveId(id: string) { + // Prevent service worker files from being included + if (id.includes('sw.js') || + id.includes('workbox') || + id.includes('registerSW.js') || + id.includes('manifest.webmanifest')) { + return '\0empty'; + } + return null; + }, + load(id: string) { + if (id === '\0empty') { + return 'export default {}'; + } + return null; } - } } ], ssr: {