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:
Alex Gleason committed 2025-10-24 19:59:16 -05:00
1 parent 6cf69da69e
commit 369ab0bf1e
4 files changed
+28 -10

No files matched your search

+26 -4
View File
@@ -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();
+1 -2
View File
@@ -2,7 +2,6 @@ import React, { useState } from 'react';
import { Link } from 'react-router-dom';
import { Button } from '@/components/ui/button';
import { Card, CardContent } from '@/components/ui/card';
import badgeSvg from '/badge.svg';
import {
ExternalLink,
Edit,
@@ -568,7 +567,7 @@ export function AppShowcaseCard({ app, onEdit, showModerationControls, hideAppro
className="flex-1"
>
<img
src={badgeSvg}
src="/badge.svg"
alt="Edit with Shakespeare"
className="h-6 hover:opacity-80 transition-opacity"
/>
+1 -3
View File
@@ -1,5 +1,3 @@
import shakespeareLogo from '/shakespeare.svg';
export interface ShakespeareLogoProps {
className?: string;
}
@@ -7,7 +5,7 @@ export interface ShakespeareLogoProps {
export function ShakespeareLogo({ className }: ShakespeareLogoProps) {
return (
<img
src={shakespeareLogo}
src="/shakespeare.svg"
alt="Shakespeare"
className={className}
/>
-1
View File
@@ -18,7 +18,6 @@ function getVersion(): string {
// https://vitejs.dev/config/
export default defineConfig(() => ({
base: './', // Use relative paths for assets (required for Electron)
server: {
host: "::",
port: 8080,