mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 08:37:22 +00:00
Use custom protocol handler instead of base: './'
This is a much cleaner solution that eliminates the need for:
- Changing base: './' in vite.config.ts
- Importing assets in components
- Any workarounds for absolute paths
How it works:
- Register app:// protocol in Electron main process
- Maps app://path/to/file to dist/path/to/file
- Allows absolute paths (/) to work naturally in Electron
- No changes needed to source code or build config
Benefits:
- vite.config.ts stays clean (no base: './' hack)
- Components use normal paths: src="/shakespeare.svg"
- Works in both browser (https://) and Electron (app://)
- This is the standard Electron approach for serving local files
Reverted:
- vite.config.ts: Removed base: './'
- ShakespeareLogo: Back to src="/shakespeare.svg"
- AppShowcaseCard: Back to src="/badge.svg"
Updated:
- electron/main.js: Added protocol.registerFileProtocol('app', ...)
- electron/main.js: Changed loadURL to app://index.html
This commit is contained in:
4 files changed
+28
-10
No files matched your search
+26
-4
@@ -1,4 +1,4 @@
|
||||
import { app, BrowserWindow, ipcMain, shell, Menu } from 'electron';
|
||||
import { app, BrowserWindow, ipcMain, shell, Menu, protocol } from 'electron';
|
||||
import { spawn } from 'child_process';
|
||||
import path from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
@@ -42,9 +42,9 @@ function createWindow() {
|
||||
mainWindow.loadURL('http://localhost:5173');
|
||||
mainWindow.webContents.openDevTools();
|
||||
} else {
|
||||
// Production mode - load from built files
|
||||
// Use file:// protocol for proper asset loading
|
||||
mainWindow.loadURL(`file://${path.join(__dirname, '../dist/index.html')}`);
|
||||
// Production mode - load from custom protocol
|
||||
// This allows absolute paths like /shakespeare.svg to work naturally
|
||||
mainWindow.loadURL('app://index.html');
|
||||
}
|
||||
|
||||
// Handle window close
|
||||
@@ -66,6 +66,28 @@ async function ensureShakespeareRoot() {
|
||||
// This method will be called when Electron has finished
|
||||
// initialization and is ready to create browser windows.
|
||||
app.whenReady().then(async () => {
|
||||
// Register the app:// protocol to serve files from dist/
|
||||
// This allows absolute paths (/) to work naturally without base: './'
|
||||
protocol.registerFileProtocol('app', (request, callback) => {
|
||||
// Remove 'app://' prefix and normalize path
|
||||
let filePath = request.url.replace('app://', '');
|
||||
|
||||
// Handle root path
|
||||
if (filePath === '' || filePath === '/') {
|
||||
filePath = 'index.html';
|
||||
}
|
||||
|
||||
// Remove leading slash if present
|
||||
if (filePath.startsWith('/')) {
|
||||
filePath = filePath.substring(1);
|
||||
}
|
||||
|
||||
// Resolve to dist directory
|
||||
const fullPath = path.join(__dirname, '../dist', filePath);
|
||||
|
||||
callback({ path: fullPath });
|
||||
});
|
||||
|
||||
// Ensure Shakespeare root directory exists before creating window
|
||||
await ensureShakespeareRoot();
|
||||
|
||||
|
||||
Reference in new issue
Block a user