Hi,
I'm currently mid way through the React From Scratch series - https://laracasts.com/series/react-from-scratch
I'm using my knowledge for a bit of challenge project and have hit a bit of an issue which is probably me doing something stupid but maybe you can help!
The file layout of my project looks like this:
https://ibb.co/Rk59dGZV
I have a header component with a simple SVG logo in it, basically this:
import {Link, useLocation} from "react-router";
export function Header() {
const location = useLocation();
return (
<header className={`banner bg-mts-light-grey flex justify-center ${location.pathname==="/"?'shadow-xl':''}`}>
<Link to={"/"}><img src="src/images/template_icons/The_Wheel_Hub.svg" alt="The Wheel Hub logo" className={"w-[85px] my-2"} /></Link>
</header>
)
}
As you can see the image src path relative to the content root is src/image/template_icons/The_Wheel_Hub.svg - this works during dev on http://localhost:5173/ however when I build and deploy the image is missing! Obviously this source path doesn't exist because the images folder has not been included in the build (I think).
I looked at the Vite documentation on handling static assets here https://vite.dev/guide/assets and I didn't really get it. However, I tried importing the asset as a URL by changing the header component to:
import {Link, useLocation} from "react-router";
import logoURL from "../images/template_icons/The_Wheel_Hub.svg";
export function Header() {
const location = useLocation();
return (
<header className={`banner bg-mts-light-grey flex justify-center ${location.pathname==="/"?'shadow-xl':''}`}>
<Link to={"/"}><img src={logoURL} alt="The Wheel Hub logo" className={"w-[85px] my-2"} /></Link>
</header>
)
}
When I build this time, the logo is there. But on inspection, it's inlined the image rather than including it in the dist folder - https://ibb.co/NdpDCk1k
Is this normal? What if I don't want to import the image in every component and keep them as seperate assets - how do I configure Vite to do this?
Thanks in advance