image icon indicating copy to clipboard operation
image copied to clipboard

Nuxt-image not working with dynamic src

Open AAA55555 opened this issue 3 years ago • 6 comments

Nuxt-image does not work with dynamic src in target: static mode and if you specify genegate: {dir: nameDir} in nuxt.config.js. If we specify all this, then the path to the image is set incorrectly. What can be done in such a case? If I use the same thing in a regular tag, then everything works as it should

Put together a project that reflects the problem

AAA55555 avatar Oct 14 '22 11:10 AAA55555

I do have the same issue using storyblok as the source of the images.

RobertWade avatar Oct 18 '22 08:10 RobertWade

note: *nuxt2, @nuxt/image

hartmut-co-uk avatar Nov 10 '22 18:11 hartmut-co-uk

acording to this code prerender.ts#L5C3-L5C50

if (!process.server || !process.env.prerender) { return }

image module only works when render image on server-side and enable prerender.

Jogiter avatar Aug 22 '23 07:08 Jogiter

I got out of the problem the following way, I write <nuxt-img src="img/1.svg" /> and everything works, remove the first / and that's it

AAA55555 avatar Aug 22 '23 07:08 AAA55555

thanks for you feed back. i am using [email protected] with the @nuxt/[email protected]. i have made a trial, it does not work as expected

Jogiter avatar Aug 23 '23 10:08 Jogiter

One can still use a variable to hold the path name and the image can be placed in the public directory. In my case images dir.

so:

nuxt-project/public/images

<template>
<NuxtImg :src="srcImage"/>
</template>
<script setup lang="ts">
const fourOFourImage = "/images/svg/404.svg";
const galleryNotAvailableImage = "/images/svg/gallery-not-available.svg";
const userNotFoundImage = "/images/svg/user-not-found.svg";

const srcImage = computed(() => {
   // ... here some logic to choose from one of the variables above
});

</script>

Nuxt will automatically look for the images folder inside the public dir.

VioletFlare avatar Aug 11 '25 05:08 VioletFlare