2024 05 19 troubleshoot svg display in react app using vite path - bizkite-co/handterm GitHub Wiki


original file name: Codeium Chat - VKAgpl6vmpnuIGeM5N0TfZLlRJoGwZHu.md summary: The user was having trouble displaying SVG images in a React application using Vite. They initially tried to render the images using a relative path but encountered issues with the path resolution. I explained that in Vite, static assets should be imported as modules to be processed correctly. I provided an example of how to import an SVG file at the top of a component file and use it in the img tag.

However, the user later decided to move the SVGs to the public folder for easier referencing. I clarified that placing the SVGs in the public folder allows them to be accessed using an absolute path from the base URL of the app. I suggested updating the img tag to use an absolute path from the root of the application to reference the SVG images in the new location. date: 2024-05-19T16:32

Codeium Chat Conversation

Note: This is purely the output of the chat conversation and does not contain any raw data, codebase snippets, etc. used to generate the output.

User at 4:32 pm, May 19, 2024

I have SVG images in src\assets\svgs but I am having trouble displaying them with this code:

     <img loading="lazy" alt="2" src={`./assets/images/svgs/${mismatchedCharCode}.svg`} width="100" className="hand"></img>

Which renders an element like this:

<img loading="lazy" alt="2" src="./images/svgs/C0.svg" width="100" class="hand">

The React application seems to just return every path back to the root.


Codeium at 4:32 pm, May 19, 2024

Given the way Vite handles static assets, you should import your SVGs as modules to ensure they are correctly processed. Vite treats the src/assets folder as the base directory for static assets and handles them differently compared to Webpack or Create React App.

You can import your SVG files at the top of your component file and then reference the imported module in the src attribute of the img tag. Here's an example of how you can do that:

// Assume `mismatchedCharCode` is 'C0'
import svgImage from './assets/svgs/C0.svg';

// In your JSX:
<img loading="lazy" alt="2" src={svgImage} width="100" className="hand"></img>

If you have many SVGs and want to dynamically select them based on mismatchedCharCode, you could create a function that dynamically imports them. However, dynamic imports might introduce some complexity, such as dealing with promises.

Alternatively, you can place the SVGs in the public folder. Anything in the public folder can be accessed with a URL relative to the base URL of your app. For example, if you have a public folder at the root of your Vite project, you could reference the SVG like this:

<img loading="lazy" alt="2" src={`/svgs/${mismatchedCharCode}.svg`} width="100" className="hand"></img>

In this case, the src attribute's path is relative to the base URL, and there is no ./ at the beginning of the path.

If you're still having trouble, ensure that the SVGs are correctly placed in the public folder and that the mismatchedCharCode variable contains the correct file name you want to reference.


User at 4:36 pm, May 19, 2024

public/ sounds like a much better option. I've moved them to public\images\svgs.


Codeium at 4:36 pm, May 19, 2024

Great! Since you've moved your SVGs to the public/images/svgs folder, you can now reference them using an absolute path from the base URL of your app, without the need to import them as modules. Here's how you can update your img tag:

<img loading="lazy" alt="2" src={`/images/svgs/${mismatchedCharCode}.svg`} width="100" className="hand"></img>

Notice that the src attribute begins with a forward slash (/), which indicates that the path is absolute and should be resolved from the root of your application.

Remember to replace mismatchedCharCode with the actual variable you are using to dynamically select the SVG image. If the variable is not defined in the scope of your component, make sure to import or define it correctly.

Also, make sure to clear any caching in your browser or development server if you don't see the changes immediately. This will ensure that your application fetches the latest version of the SVG files from the new location.

⚠️ **GitHub.com Fallback** ⚠️