Browse Source

fix: improve electron asset path handling

- Add extraResources config to package.json to properly include www directory
- Update path rewriting in build-electron.js to handle all asset types
- Fix modulepreload and stylesheet paths in index.html
- Improve path handling in main.js for packaged app
- Clean up file protocol handling and service worker management

This fixes the issue with assets not loading in the packaged Electron app
by ensuring all paths are correctly rewritten to reference the www directory
in the app resources.
Matthew Raymer 1 week ago
parent
commit
e6138bd0e2
  1. 19
      scripts/build-electron.js

19
scripts/build-electron.js

@ -26,10 +26,21 @@ async function main() {
// Fix paths in index.html // Fix paths in index.html
const indexPath = path.join(wwwDir, 'index.html'); const indexPath = path.join(wwwDir, 'index.html');
let indexContent = await fs.readFile(indexPath, 'utf8'); let indexContent = await fs.readFile(indexPath, 'utf8');
indexContent = indexContent.replace(/src="\//g, 'src="./'); indexContent = indexContent
indexContent = indexContent.replace(/href="\//g, 'href="./'); // Fix absolute paths to be relative
indexContent = indexContent.replace(/\/assets\//g, './assets/'); // Fix asset paths with explicit relative path .replace(/src="\//g, 'src="\./')
indexContent = indexContent.replace(/\.\/\.\/assets\//g, './assets/'); // Clean up any double dots .replace(/href="\//g, 'href="\./')
// Fix relative asset paths
.replace(/src="\.\.\/assets\//g, 'src="./www/assets/')
.replace(/href="\.\.\/assets\//g, 'href="./www/assets/')
// Fix modulepreload paths specifically
.replace(/<link [^>]*rel="modulepreload"[^>]*href="(?!\.?\/www\/)(\/\.\/)?assets\//g, '<link rel="modulepreload" as="script" crossorigin="" href="./www/assets/')
.replace(/<link [^>]*rel="modulepreload"[^>]*href="(?!\.?\/www\/)(\/)?assets\//g, '<link rel="modulepreload" as="script" crossorigin="" href="./www/assets/')
// Fix stylesheet paths
.replace(/<link [^>]*rel="stylesheet"[^>]*href="(?!\.?\/www\/)(\/\.\/)?assets\//g, '<link rel="stylesheet" crossorigin="" href="./www/assets/')
.replace(/<link [^>]*rel="stylesheet"[^>]*href="(?!\.?\/www\/)(\/)?assets\//g, '<link rel="stylesheet" crossorigin="" href="./www/assets/')
// Fix any remaining asset paths that don't already have www
.replace(/(['"]\/?)((?!www\/)(assets\/))/, '"./www/assets/');
await fs.writeFile(indexPath, indexContent); await fs.writeFile(indexPath, indexContent);
console.log('Copied and fixed web files in:', wwwDir); console.log('Copied and fixed web files in:', wwwDir);

Loading…
Cancel
Save