Vite + Vue 3 + images deploy

Hi everyone,

So I worked hard on my portfolio today, which is basically almost all images.

It is build with Vite and Vue 3.

I have made some components so I can do a loop through all the images and send the image paths to the components. When I run locally and “netlify dev”, all the images show up.

But when I run “netlify deploy”, they don’t show up.

My images are all in:

src/assets/img

And in my code I use:

/src/assets/img/guitar.svg
/src/assets/img/cheese.svg

Etc.

My code looks like this:

<template>
    <Item
            v-for="(bgImage, index) in items"
            :key="index"
            :image="bgImage.image"
            :width="bgImage.width"
            :height="bgImage.height"
    />


</template>
<script setup>
import Item from "../components/Item.vue";
import {computed} from "vue";


const items = computed(() => [
    {
        image: "/src/assets/img/flower-bouquet.svg",
        width: "w-[200px]",
    },
    {
        image: "/src/assets/img/plate.svg",
        width: "w-[200px]",
    },
    {
        image: "/src/assets/img/sunglasses.svg",
        width: "w-[80px]",
    },
    {
        image: "/src/assets/img/guitar.svg",
        width: "w-[300px]",
    },
    {
        image: "/src/assets/img/cheese.svg",
        width: "w-[75px]",
    },
]);
</script>

And my Item component looks like this:

<template>
    <div class="absolute">
        <img :src="image" :class="[width, height ]" />
    </div>
</template>

<script setup>
const props = defineProps({
    image: {
        type: String,
        required: true
    },
    width: {
        type: String,
    },
    height: {
        type: String,
    },
)
<script>

When I look in the /dist folder, I see some images, but not the ones I’m passing from component to component:

My netlify test deploy is here:
https://64550b42bdd3ac11305000ed--robineio.netlify.app/

Is there a way to fix this?

The reason the images aren’t showing is because of the path.

<img
  src="/src/assets/img/plant-pointy-leafs.svg"
  alt=""
  class="w-[300px] h-[221px] animate-swaying8"
>

The src directory isn’t part of the deploy. I suggest checking out the Vite documentation on static asset handling

The documentation doesn’t help me at all.

Is it me or is it just a simple issue? How do I load images in my Vite app?

If its not:

<img
  src="/src/assets/img/plant-pointy-leafs.svg"
  alt=""
  class="w-[300px] h-[221px] animate-swaying8"
>

Then what should it be?

Can you share the repository you are deploying from?

I think it has something to do with passing the images dynamically in the array instead of them just being in the HTML.

When I simply put < img src = " /assets/image.png " /> and I run a build, the image appears in the dist/assets folder.

I am going to try to put it in a repo.

This is what I was trying to say previously. Using /src in the path isn’t going to work when it is inside computed. Vite doesn’t know about it, and src is the location of the build file, not the output.

So all my work is for nothing…?

If you give up it is.

Welcome to (web) development!
Sometimes things don’t work. You have to figure out why. That’s called debugging. Don’t like debugging? Then don’t become a developer, because there is always plenty of debugging. Some of it gets easier. Sometimes it gets weird and you feel like you’re chasing your tail in circles. But if you persevere, you’ll get to the bottom of it and will have learned new things along the way.


This don’t believe this is a Netlify issue. I believe if you build locally (not run dev mode) the same issue would present. There are some things you aren’t doing correctly (it would seem) in the project. Without seeing the code, I can only make wild guesses which will likely prove fruitless. However if you can share a public git repository with the code I am happy to take a look a provide whatever assistance I can.

I don’t mind debugging. Locally it works, so I assume its a Netlify issue.

I made a repo:

It isn’t. Here’s why.

Taking PlantView.vue as an example.

The list of plants starts

const plants = computed(() => [
    {
        image: "/assets/img/plant-berry.svg",
        left: "left-[15%]",
        top: "top-[-9%]",
        tripleXlTop: "3xl:top-[-10px]",
        zIndex: "10",
        width: "w-[250px]",
        height: "h-[194px]",
        animation: "animate-swaying7"
    },
   // more items...
])

Running npm run build creates a production build of the project. Looking through dist/assets I don’t see any filename starting with plant-berry.

Changing the path to a relative one ../assets/img/plant-berry.svg yields the same result.

But importing the file as a variable does

import plantBerry from "/assets/img/plant-berry.svg"

const plants = computed(() => [
    {
        image: plantBerry,
        left: "left-[15%]",
        top: "top-[-9%]",
        tripleXlTop: "3xl:top-[-10px]",
        zIndex: "10",
        width: "w-[250px]",
        height: "h-[194px]",
        animation: "animate-swaying7"
    },
   // more items...
])

Looking in dist/assets there is now plant-berry-4ef478b7.svg.

The issue here is all images will require importing like this. A little tedious.

The VIte documentation I linked to talks about The public Directory which (among other things) says

…or you simply don’t want to have to import an asset first just to get its URL

So what you do is put these assets into public e.g.

public
└── assets
    └── img
        ├── plant-berry.svg
        ├── plant-bubbly.svg
        └── plant-pointy-leafs.svg

Then instead of importing them, you leave the code unchanged.

Running npm run build will copy the assets directory inside public such that those image paths are valid when viewed.
If all the SVG files are moved to this location (and you change references such as src="../assets/img/[image-name].svg" to src="/assets/img/[image-name].svg") you’ll see something like this in dev mode

and this in a locally-built production build

I found that blanky.svg needs to remain in src/assets/img and bunting.svg needs to go in both locations. You could tweak this if you wish.

1 Like

Thanks, I adjusted all my code according to your answer and it works. I did however have to do:

import plantBerry from "/src/assets/img/plant-berry.svg"

const plants = computed(() => [
    {
        image: plantBerry,
        left: "left-[15%]",
        top: "top-[-9%]",
        tripleXlTop: "3xl:top-[-10px]",
        zIndex: "10",
        width: "w-[250px]",
        height: "h-[194px]",
        animation: "animate-swaying7"
    },

With the /src/ instead of just assets.

If I remove the /src, the app won’t compile and crashes. With this code, it renders the images into the assets folder and the images are there after deploy.

Thanks so much for your help and taking the time out of your day to debug!