mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Fix public asset paths for Electron compatibility
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.
This commit is contained in:
3 files changed
+92
-2
No files matched your search
@@ -0,0 +1,87 @@
|
||||
# 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
|
||||
|
||||
```tsx
|
||||
// ❌ 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:
|
||||
|
||||
```tsx
|
||||
// ✅ This works in both browser and Electron
|
||||
import shakespeareLogo from '/shakespeare.svg';
|
||||
|
||||
<img src={shakespeareLogo} alt="Logo" />
|
||||
```
|
||||
|
||||
### What Vite Does
|
||||
|
||||
With `base: './'` in config:
|
||||
|
||||
1. **Without import** (runtime string):
|
||||
```tsx
|
||||
src="/shakespeare.svg" // Not transformed, breaks in Electron
|
||||
```
|
||||
|
||||
2. **With import** (build-time):
|
||||
```tsx
|
||||
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:
|
||||
|
||||
```tsx
|
||||
import logo from '/shakespeare.svg?url';
|
||||
// Always returns a URL string
|
||||
```
|
||||
|
||||
But this is **unnecessary** because:
|
||||
1. Vite already handles SVG imports correctly
|
||||
2. Without `?url`, Vite still returns the correct path
|
||||
3. Using `?url` makes the code unnecessarily Vite-specific
|
||||
|
||||
## Best Practice
|
||||
|
||||
For public assets that need to work in both browser and Electron:
|
||||
|
||||
```tsx
|
||||
// ✅ 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's `file://` protocol
|
||||
- **Solution**: Import assets so Vite can transform paths at build time
|
||||
- **No `?url` needed**: Plain imports work fine with `base: './'`
|
||||
- **Result**: Code works in both browser and Electron without special handling
|
||||
@@ -2,6 +2,7 @@ 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,
|
||||
@@ -567,7 +568,7 @@ export function AppShowcaseCard({ app, onEdit, showModerationControls, hideAppro
|
||||
className="flex-1"
|
||||
>
|
||||
<img
|
||||
src="/badge.svg"
|
||||
src={badgeSvg}
|
||||
alt="Edit with Shakespeare"
|
||||
className="h-6 hover:opacity-80 transition-opacity"
|
||||
/>
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import shakespeareLogo from '/shakespeare.svg';
|
||||
|
||||
export interface ShakespeareLogoProps {
|
||||
className?: string;
|
||||
}
|
||||
@@ -5,7 +7,7 @@ export interface ShakespeareLogoProps {
|
||||
export function ShakespeareLogo({ className }: ShakespeareLogoProps) {
|
||||
return (
|
||||
<img
|
||||
src="/shakespeare.svg"
|
||||
src={shakespeareLogo}
|
||||
alt="Shakespeare"
|
||||
className={className}
|
||||
/>
|
||||
|
||||
Reference in new issue
Block a user