I have an electron forge app that I'm ready to package but my static assets don't work in the built version. For example, I'm trying to display our logo:
<img src='/assets/svgs/logo.svg' class='text'></img>
Since in dev it spins up a server, the URL for the page is http://localhost:3000/main_window. To access my static files I put a "/" which works as it loads from the base domain and not off the window name:
http://localhost:3000/assets/svg/logo.svg
Without the first "/" it looks here which doesn't work:
http://localhost:3000/main_window/assets/svgs/logo.svg
But with the first "/", the built app it interprets the path different because its no longer in a dev server:
file:///assets/svgs/logo.svg
If I change my html to following then it works in the built version but not the dev version:
<img src='assets/svgs/logo.svg' class='text'></img>
file:///C:/Users/<USER>/projects/<PROJECT>/out/App-win32-x64/resources/app/.webpack/renderer/main_window/assets/svgs/logo.svg
I am using the WebpackCopyPlugin to copy these assets into the .webpack folder
new CopyWebpackPlugin({
patterns: [
{ from: path.resolve(__dirname, 'src/renderer/assets/'), to: 'assets' },
],
})
That is duplicating the assets in two places in the .webpack folder however. I see my assets in both:
.webpack\renderer\assets
.webpack\renderer\main_window\assets
I'm not sure why its duplicating these files, but with the assets in the main_window folder, I dont understand why the path http://localhost:3000/main_window/assets/svgs/logo.svg doesn't resolve in the dev environment. I also find it weird in the dev server it looks at the base assets folder, but in the built app it looks at main_window\assets.
How do I go about getting these static assets to work in both the dev server and the built app without having to use different paths depending on which environment its running in?
Edit: I did find this similar post, but their answer was to run an express server in production, and the other solution was to change the path based on the environment variable. Running an express server in prod all for a few images sounds like a band-aid solution and having to use an environment variable to set your path doesn't work in raw HTML. Maintainability on that isn't great as you need to remember to use that path and could still easily lead to broken production code if not tested well.