diff --git a/PUBLIC_ASSETS_EXPLANATION.md b/PUBLIC_ASSETS_EXPLANATION.md new file mode 100644 index 00000000..0ae4d2ce --- /dev/null +++ b/PUBLIC_ASSETS_EXPLANATION.md @@ -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 +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'; + +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 ( +
+ Logo + Badge +
+ ); +} +``` + +## 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 diff --git a/src/components/AppShowcaseCard.tsx b/src/components/AppShowcaseCard.tsx index 830a584c..4411f604 100644 --- a/src/components/AppShowcaseCard.tsx +++ b/src/components/AppShowcaseCard.tsx @@ -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" > Edit with Shakespeare diff --git a/src/components/ShakespeareLogo.tsx b/src/components/ShakespeareLogo.tsx index f71920f5..ab7694d5 100644 --- a/src/components/ShakespeareLogo.tsx +++ b/src/components/ShakespeareLogo.tsx @@ -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 ( Shakespeare