From a9beb4cffa4004294adb2726339cb8c3bc3fbaa8 Mon Sep 17 00:00:00 2001 From: William Wills Date: Tue, 26 Nov 2024 10:57:52 -0500 Subject: [PATCH] feat: removed unnecessary production express server --- package.json | 1 - src/main.ts | 45 +++++++++++++++++++++++---------------------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/package.json b/package.json index 2c927c12..424651c3 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,6 @@ "components": "^0.1.0", "dayjs": "^1.11.13", "diff": "^5.2.0", - "express": "^4.21.0", "flowbite": "^2.5.1", "flowbite-react": "^0.7.8", "flowbite-typography": "^1.0.3", diff --git a/src/main.ts b/src/main.ts index a9067d94..d52add57 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,21 +1,10 @@ import { app, BrowserWindow, shell } from 'electron'; import path from 'path'; -import express from 'express'; +import { fileURLToPath } from 'url'; -//electron -const serverPort = 61310; -const appServer = express(); - -const buildPath = path.join(app.getAppPath(), 'build/renderer'); // Path to your Vite build - -appServer.use(express.static(buildPath)); // Serve static files from the build directory - -// Catch-all handler to return index.html for any non-static file request -appServer.get('*', (_, res) => { - res.sendFile(path.join(buildPath, 'index.html')); -}); - -appServer.listen(serverPort); +// Manually define __dirname +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); function createWindow() { const win = new BrowserWindow({ @@ -25,17 +14,29 @@ function createWindow() { contextIsolation: false, nodeIntegration: true, webviewTag: true, - // preload: path.join(__dirname, 'preload.js'), }, }); // Load URL based on the environment - const loadUrl = - process.env.NODE_ENV === 'development' - ? 'http://localhost:5173/' // Development URL - : `http://localhost:${serverPort}/`; // Production URL served by Express - - win.loadURL(loadUrl); + if (process.env.NODE_ENV === 'development') { + console.log('loading app from dev node server'); + win.loadURL('http://localhost:5173/'); // Development URL + } else { + // load app from packaged static html + const indexPath = path.join(__dirname, 'renderer', 'index.html'); + console.log('Loading file:', indexPath); // Log to confirm the correct path + win.loadFile(indexPath).catch((err) => { + console.error('Failed to load index.html:', err); + }); + + // app is a SPA. reload index.html when the app tries to load a virtual route + win.webContents.on('did-fail-load', (_event, _errorCode, _errorDescription, validatedURL) => { + console.log(`Failed to load: ${validatedURL}, loading index.html instead`); + win.loadFile(indexPath).catch((err) => { + console.error('Failed to load index.html:', err); + }); + }); + } win.webContents.setWindowOpenHandler(({ url }) => { shell.openExternal(url);