Harden MVP runtime and fix packaged desktop white screen
This commit is contained in:
@@ -2,12 +2,26 @@ const { app, BrowserWindow, ipcMain } = require('electron')
|
||||
const path = require('path')
|
||||
const fs = require('fs')
|
||||
|
||||
const isDev = process.env.NODE_ENV !== 'production'
|
||||
const devUrl = 'http://localhost:5173'
|
||||
const devServerUrl = process.env.ELECTRON_DEV_SERVER_URL || process.env.VITE_DEV_SERVER_URL || ''
|
||||
const isDev = !app.isPackaged && Boolean(devServerUrl)
|
||||
const API_URL_KEY = 'runtime_api_url'
|
||||
const DEFAULT_API_URL = process.env.VITE_API_URL || 'http://localhost:4000/api'
|
||||
let mainWindow
|
||||
|
||||
function resolveRendererEntry() {
|
||||
const candidates = [
|
||||
path.join(__dirname, '..', 'dist', 'index.html'),
|
||||
path.join(process.resourcesPath || '', 'app.asar', 'dist', 'index.html'),
|
||||
path.join(process.resourcesPath || '', 'app.asar.unpacked', 'dist', 'index.html'),
|
||||
]
|
||||
|
||||
for (const candidate of candidates) {
|
||||
if (candidate && fs.existsSync(candidate)) return candidate
|
||||
}
|
||||
|
||||
return candidates[0]
|
||||
}
|
||||
|
||||
function normalizeApiUrl(value) {
|
||||
if (!value || typeof value !== 'string') return null
|
||||
const trimmed = value.trim().replace(/\/+$/, '')
|
||||
@@ -88,11 +102,24 @@ function createWindow() {
|
||||
}
|
||||
})
|
||||
|
||||
mainWindow.webContents.on('did-fail-load', (_event, code, description, validatedUrl) => {
|
||||
console.error(`Renderer failed to load (${code}): ${description} @ ${validatedUrl}`)
|
||||
})
|
||||
|
||||
if (isDev) {
|
||||
mainWindow.loadURL(devUrl)
|
||||
mainWindow.loadURL(devServerUrl).catch(err => {
|
||||
console.error('Failed to load dev server URL, falling back to local dist file.', err)
|
||||
const rendererEntry = resolveRendererEntry()
|
||||
mainWindow.loadFile(rendererEntry).catch(loadErr => {
|
||||
console.error('Fallback renderer load failed.', loadErr)
|
||||
})
|
||||
})
|
||||
mainWindow.webContents.openDevTools()
|
||||
} else {
|
||||
mainWindow.loadFile(path.join(__dirname, '..', 'dist', 'index.html'))
|
||||
const rendererEntry = resolveRendererEntry()
|
||||
mainWindow.loadFile(rendererEntry).catch(err => {
|
||||
console.error(`Failed to load renderer entry at ${rendererEntry}`, err)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
"build": "vite build",
|
||||
"preview": "vite preview",
|
||||
"build:renderer": "vite build",
|
||||
"electron:dev": "concurrently \"vite\" \"wait-on http://localhost:5173 && electron .\"",
|
||||
"electron:dev": "concurrently \"vite\" \"wait-on http://localhost:${PORT_FRONTEND_DEV:-5173} && ELECTRON_DEV_SERVER_URL=http://localhost:${PORT_FRONTEND_DEV:-5173} electron .\"",
|
||||
"electron:build": "vite build && electron-builder",
|
||||
"start": "electron ."
|
||||
},
|
||||
|
||||
@@ -9,6 +9,7 @@ export default defineConfig(({ mode }) => {
|
||||
base: './',
|
||||
plugins: [react()],
|
||||
server: {
|
||||
host: env.VITE_DEV_HOST || '0.0.0.0',
|
||||
port: parseInt(env.PORT_FRONTEND_DEV) || 5173,
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user