A comprehensive demonstration of modern image optimization techniques using SvelteKit's recommended best practices. This project showcases four different approaches to image optimization, each with their specific benefits and use cases.
Before running this project, make sure you have:
git clone https://github.com/dharmveer97/svelte-image-speed-test.git
cd static-site
npm install
Create the following directory structure and add your sample images:
static/
āāā images/
āāā first.jpg
āāā two.jpg
āāā three.jpg
āāā four.jpg
āāā five.jpg
Image Requirements:
npm run dev
Open http://localhost:5173 in your browser.
npm run build
npm run preview
āāā src/
ā āāā lib/
ā ā āāā components/
ā ā āāā Gallery.svelte # Main showcase component
ā āāā routes/
ā ā āāā +page.svelte # Home page
ā āāā app.html # HTML template
āāā static/
ā āāā images/ # Static image assets
āāā package.json
āāā vite.config.ts # Vite configuration
āāā svelte.config.js # SvelteKit configuration
āāā tailwind.config.js # Tailwind CSS configuration
āāā postcss.config.js # PostCSS configuration
Best for: Most static images in SvelteKit projects
Features:
Installation:
npm install @sveltejs/enhanced-img
Best for: Fine control over optimization parameters
Features:
Already included in this project's dependencies.
Best for: User-generated content and dynamic transformations
Features:
Providers demonstrated:
Best for: Blur-up placeholders and smooth loading transitions
Features:
fetchpriority="high"
for hero imagesā Format Optimization
ā Responsive Design
ā Performance
The project uses Vite with imagetools plugin:
// vite.config.ts
import { sveltekit } from '@sveltejs/kit/vite';
import { imagetools } from 'vite-imagetools';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [sveltekit(), imagetools()],
});
Static site generation is configured in svelte.config.js
:
import adapter from '@sveltejs/adapter-static';
const config = {
kit: {
adapter: adapter(),
},
};
static/images/
Gallery.svelte
For Vite-imagetools, modify the query parameters:
// Example: Different sizes and formats
import myImg from '/images/photo.jpg?w=300;600;900&format=webp;avif;jpg&as=picture';
The project uses Tailwind CSS. Modify classes in Gallery.svelte
or extend the Tailwind configuration in tailwind.config.js
.
Run Lighthouse audits to measure performance:
# Install Lighthouse CLI
npm install -g lighthouse
# Run audit on local build
npm run build
npm run preview
lighthouse http://localhost:4173 --output html --output-path ./lighthouse-report.html
Use WebPageTest for detailed performance analysis of your deployed site.
This project is configured for static site generation. Deploy to:
vercel --prod
For CDN integrations, you may need:
# .env.local
CLOUDINARY_CLOUD_NAME=your_cloud_name
IMGIX_DOMAIN=your_domain.imgix.net
Module not found errors:
npm install
Images not loading:
static/images/
Build failures:
.svelte-kit
directory: rm -rf .svelte-kit
rm -rf node_modules && npm install
TypeScript errors:
git checkout -b feature/amazing-feature
git commit -m 'Add amazing feature'
git push origin feature/amazing-feature
This project is licensed under the MIT License - see the LICENSE file for details.
Happy coding! š
For questions or support, please open an issue in the GitHub repository.