mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 00:27:24 +00:00
The issue: With base: './' for Electron, absolute paths like /shakespeare.svg don't work because they resolve to filesystem root in file:// protocol. The solution: Import assets so Vite transforms paths at build time. - import logo from '/shakespeare.svg' (without ?url) - Vite automatically converts to './shakespeare.svg' with base: './' - Works in both browser and Electron This is cleaner than ?url suffix which is unnecessarily Vite-specific. Fixed components: - ShakespeareLogo: import shakespeareLogo from '/shakespeare.svg' - AppShowcaseCard: import badgeSvg from '/badge.svg' Added PUBLIC_ASSETS_EXPLANATION.md documenting the solution.
2.2 KiB
2.2 KiB
Public Assets in Vite with Electron
The Problem
When using base: './' in Vite config for Electron support, public assets need special handling.
Why /shakespeare.svg Doesn't Work in Electron
// ❌ This breaks in Electron
<img src="/shakespeare.svg" alt="Logo" />
In Browser:
/shakespeare.svg→https://example.com/shakespeare.svg✅
In Electron (file:// protocol):
/shakespeare.svg→file:///shakespeare.svg(filesystem root) ❌
The issue is that base: './' only transforms paths in index.html, not runtime JavaScript strings.
The Solution
Import the asset - Vite will handle the path transformation:
// ✅ This works in both browser and Electron
import shakespeareLogo from '/shakespeare.svg';
<img src={shakespeareLogo} alt="Logo" />
What Vite Does
With base: './' in config:
-
Without import (runtime string):
src="/shakespeare.svg" // Not transformed, breaks in Electron -
With import (build-time):
import logo from '/shakespeare.svg'; // Vite transforms to: const logo = './shakespeare.svg'; src={logo} // Works in both browser and Electron ✅
Why Not ?url?
The ?url suffix is a Vite-specific feature that forces the import to return a URL string:
import logo from '/shakespeare.svg?url';
// Always returns a URL string
But this is unnecessary because:
- Vite already handles SVG imports correctly
- Without
?url, Vite still returns the correct path - Using
?urlmakes the code unnecessarily Vite-specific
Best Practice
For public assets that need to work in both browser and Electron:
// ✅ Correct - import without ?url
import logo from '/shakespeare.svg';
import badge from '/badge.svg';
export function MyComponent() {
return (
<div>
<img src={logo} alt="Logo" />
<img src={badge} alt="Badge" />
</div>
);
}
Summary
- Problem: Absolute paths (
/file.svg) don't work with Electron'sfile://protocol - Solution: Import assets so Vite can transform paths at build time
- No
?urlneeded: Plain imports work fine withbase: './' - Result: Code works in both browser and Electron without special handling