Where to put images with Svelte Kit

Viewed 4444

I have been using Svelte for a little while and now I have switched to Svelte Kit so I can add multiple pages. I want to add some images to my site but I don't know where to put them. In Svelte I would just put them in public/images but there is no public folder with Svelte Kit (I set it up with npm init svelte@next my-app if that matters). Would I put them in static?

Thanks!

3 Answers

I added the images in static/images and referenced them with src="/images/photo.jpg" like @b2m9 said and it works perfectly.

As Sveltekit uses Vitejs, there is a easy solution mentioned in Vitejs official web site (Click Here).
First inside the script tag :

<script>
const imgUrl = new URL('./img.png', import.meta.url).href
</script>

then inside your Image tag just use that variable,

<img src="{imgUrl}" alt="" />

or,

<div class=" h-screen w-full" style="background-image: url('{bgUrl}') ;">
</div>

You can import static images from any relative path.

there is also svelte-image.

"Svelte image is a pre-processor which automates image optimization using sharp.

It parses your img tags, optimizes or inlines them and replaces src accordingly. (External images are not optimized.)

Image component enables lazyloading and serving multiple sizes via srcset.

This package is heavily inspired by gatsby image.

Kudos to @jkdoshi for great video tutorial to Svelte Image."

-https://github.com/matyunya/svelte-image

Related