Optimize Svelte Images — Compress Once, Not Every Build
Compress the images in your SvelteKit or Svelte and Vite project, from static/ and your imports, before you build. Smaller output without a build-time image plugin.
Quick answer
SvelteKit serves anything in static/ unchanged and lets Vite hash anything you import from src. Neither path shrinks the image. Compress both with TinyPixels first; if you use @sveltejs/enhanced-img, it then generates its responsive variants from a lean source.
How to compress images before a SvelteKit build
Download and open TinyPixels
Free to install on Mac or Windows — no account needed to start.
Cut camera and design originals down
Resize anything much larger than its display box, especially if enhanced-img will process it.
Compress the folders
Drop static/ and your assets folder into TinyPixels. Convert big banners to WebP if you serve them directly.
Commit and build
Prerendered pages and adapter output now ship the smaller files.
Small JavaScript does not mean a small site
Svelte compiles components away at build time, which keeps your JavaScript small, but images are not part of that compilation. A 3 MB PNG in static/ costs the same on a Svelte site as on any other stack, and on a content-heavy page it usually dwarfs the framework code.
SvelteKit gives you two places for images. Files in static/ are served from the site root with no processing, so you reference them as /hero.png. Files inside src, such as src/lib/assets, can be imported, which gives them a content hash for long-term caching.
The @sveltejs/enhanced-img package adds an <enhanced:img> element that produces modern formats and multiple sizes at build time from your source file. It is a good fit, and its build time grows with source size, so trimming oversized originals first keeps builds quick. Check the current Svelte docs for the options it supports.
static/ and imports
Both are covered with one pass, since neither is re-encoded for you.
Feeds enhanced-img
Lean originals make build-time variant generation faster and the variants smaller.
Prerendered output
Static adapters ship exactly the files you put in, so source size equals delivered size.
Format conversion
Convert large directly-served images to WebP or AVIF and update the references.
Where images live in a SvelteKit project
| Location | What happens | Your move |
|---|---|---|
| static/ | Served as-is from the site root. | Compress the original |
| src/lib/assets (imported) | Vite hashes and emits the file; very small files may be inlined. | Compress before commit |
| <enhanced:img src="./x.jpg"> | Build-time variants in modern formats and sizes. | Start from a reasonably sized source |
| CSS background url() | Handled by Vite like an import. | Compress the referenced file |
| adapter-static output | Ships whatever the build collected. | Nothing more to do after compressing |
Behaviour shown here follows the SvelteKit and Vite docs at the time of writing; check them for your version.
Common mistakes with Svelte project images
Treating "Svelte is small" as "my site is small"
The framework's size is a rounding error next to a few uncompressed hero images. Audit image weight separately from the JavaScript bundle.
Importing assets from static/
Files in static/ are meant to be referenced by URL, not imported. Mixing the two leads to broken paths. Decide per file and compress either way.
Feeding 8000 px originals to enhanced-img
It will generate smaller variants, but at the cost of slow builds. Resize and compress the original to something sensible first.
Forgetting prerendered pages ship the same images
Prerendering fixes HTML delivery, not image weight. The first paint still waits on the hero if it is huge.
Frequently asked questions
Does SvelteKit optimize images automatically?
Not by default. Vite hashes imported images, and static/ files are served as-is. The @sveltejs/enhanced-img package can generate formats and sizes at build time.
Should I use static/ or import images in SvelteKit?
Import images you want hashed for long-term caching or processed by enhanced-img; use static/ for files that need a fixed URL such as favicons and social images. Compress both.
Do I still need to compress if I use enhanced-img?
It is still worth it. enhanced-img works from your source file, so a lean original makes builds faster and gives you a better starting point.
Does compression affect SvelteKit build time?
Pre-compressing doesn't add to build time, since it happens before the build. It can shorten builds that process images, because the inputs are smaller.
Can I batch compress my whole SvelteKit project at once?
Yes. Drop static/ and your assets folder into TinyPixels and every image compresses in one local pass.
Keep your Svelte site as light as its JavaScript
Free to start. No credit card, no account, no cloud. See Pro pricing →
Related tools
Optimize Vue Images
Compress images for your Vue app before build.
Compress Vite Assets
Compress public assets before deploying with Vite.
WebP Converter
Convert any format to WebP locally.
Optimize Astro Images
The same approach for Astro's static/ folder.
Image Compressor Hub
Every format, platform, and framework in one place.
Optimize React Images
Compress images for your React app before build.
Optimize Next.js Images
Compress public assets before deploying Next.js.
Optimize Angular Images
Compress images for your Angular app before build.
Read more: Fix your Lighthouse score where it actually matters → · Best image format for websites in 2026 →