kit: Cannot pass "src" as prop while using @sveltejs/enhanced-img
Describe the bug
I wanted to created a simple hero component where I would pass the hero image dynamically, but I got error if enhanced:img used instead of img:
TypeError: Cannot read properties of undefined (reading 'src')
Code provided at “Reproduction” working correctly if I use simple img tag.
Reproduction
You need a JPG image named innerhero.jpg at /src/img/ folder
+page.svelte
<script>
import Hero from '$lib/template/Hero.svelte';
</script>
<Hero backgroundImage="$lib/img/innerhero.jpg" />
Hero.svelte
<script>
export let backgroundImage;
</script>
<enhanced:img src="{backgroundImage}" class="absolute inset-0 object-cover w-full h-full" alt="An alt text" />
I have also tried:
+page.svelte:
<script>
import Hero from '$lib/template/Hero.svelte';
import HeroImg from '$lib/img/innerhero.jpg';
</script>
<Hero backgroundImage="{HeroImg}" />
Logs
No response
System Info
System:
OS: Linux 5.15 Ubuntu 22.04.3 LTS 22.04.3 LTS (Jammy Jellyfish)
CPU: (8) x64 Intel(R) Core(TM) i5-8350U CPU @ 1.70GHz
Memory: 4.02 GB / 7.69 GB
Container: Yes
Shell: 5.1.16 - /bin/bash
Binaries:
Node: 21.1.0 - ~/.nvm/versions/node/v21.1.0/bin/node
Yarn: 1.22.19 - /usr/bin/yarn
npm: 10.2.5 - ~/.nvm/versions/node/v21.1.0/bin/npm
pnpm: 8.12.1 - ~/.nvm/versions/node/v21.1.0/bin/pnpm
bun: 1.0.15 - ~/.bun/bin/bun
npmPackages:
@sveltejs/adapter-auto: ^3.1.0 => 3.1.0
@sveltejs/enhanced-img: ^0.1.7 => 0.1.7
@sveltejs/kit: ^2.0.6 => 2.0.6
@sveltejs/vite-plugin-svelte: ^3.0.1 => 3.0.1
svelte: ^4.2.8 => 4.2.8
vite: ^5.0.11 => 5.0.11
Severity
annoyance
Additional Information
No response
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 17
- Comments: 24 (6 by maintainers)
I ran into the same issues with the
enhanced:imgcomponent. I created a repository to collect the error cases I was able to identify:I also have a similar issue when trying to use
<enhanced:img>tag in sveltekit.I have the latest versions:
"@sveltejs/enhanced-img": "^0.1.8""svelte": "^4.2.8",What I’d like to do, is I’d like to pass strings from my backend, to the frontend, and simply render out a list of images using those strings.
Those string paths are references to images in my
$libs/assets/directory, see below:Where
sheet.imagePathwould populate as ‘’/src/lib/assets/placeholder.jpg"When I simply hardcode this entire string, I have no issue, but then again, that defats the purpose of what I’m trying to do. Any help?
Thanks!
Slightly cleaner version of @tobiasBora’s fix that I used to make a custom component wrapper to enhance images in mdsvex (markdown importer).
Rolled the whole thing up into a single svelte component.
Posting here so if anyone else ends up at this issue trying to solve this problem for now, hopefully this helps.
@fmaclen the example in your repository will work if you upgrade to 0.1.9. The issue was that we previously did not support the
{src}shorthand, but that’s been implemented now@benmccann It seems like
enhance-imgshould be marked as acompiler pluginin the docs, since it is not able to (at least currently) resolve any dynamically added vars during runtime (which has a big use-case).EDIT: consider using the excellent svelte-img instead!
I arrived here because while I do have the images on disk, I get an URL for the image from an API. It’s kind of a pain, my workaround is to import.meta.glob all the images that the API could possibly return, then check which one is the one returned by the API and use that.
Would appreciate a less hacky solution.
Sorry I wasted your time, you’re correct, there were a slew of issues here in addition to the extra curly you mentioned
svgwith@sveltejs/enhanced-imgImageProps.src : string | Picture,<enhanced:img/>hated it$libforsvgimages, had to move it understaticwith a different pathRegardless, my compliments to the package creators. Given the site I am working on will have north of a hundred images by the time I am finished, it’s a life saver having this automated at build even with all its limitations.
Do you have an extra set of curly braces around your images value (aqlogo) you’re passing to the component?
I’d maybe try breakpointing or console.log-ing the value of images in your component and see what the value looks like.
I’m assuming there’s an omitted “for image of images” in the component also?
I’m travelling army the moment and don’t have ready access to my computer but, looking at Fernando’s repo, mine is functionally identical. My use case uses mdsvex, with the same issue, but is present in svelte also.
Thanks for looking into this. This is very useful functionality.
Dave.
Sent from Proton Mail mobile
-------- Original Message -------- On Jan. 17, 2024, 23:17, Ben McCann wrote:
@fmaclen could you provide a repository that reproduces this error? It’s quite time consuming for us to copy code samples into projects for debugging given the number of open issues we have and so we tend to skip issues without repositories out of necessity