Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: removed unnecessary express server for renderer to load ui #87

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"components": "^0.1.0",
"dayjs": "^1.11.12",
"diff": "^5.2.0",
"express": "^4.19.2",
"flowbite": "^2.5.1",
"flowbite-react": "^0.7.8",
"flowbite-typography": "^1.0.3",
Expand Down
45 changes: 23 additions & 22 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -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:5175/' // 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:5175/'); // 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);
Expand Down
Loading