Leptos Image
Crafted with inspiration from Next.js
Images make a substantial impact on the size and performance of a website, so why not get them right?
Enter Leptos <Image/>, a component that enhances the standard HTML <img> element with automatic image optimization features.
Features
- Size Optimization: Automatically resizes images and converts them to the modern
.webpformat for an optimal balance of size and quality. - Low-Quality Image Placeholders (LQIP): Embeds SVG placeholders extracted from original images into server-side rendered HTML, improving perceived performance during image loading.
- Faster Page Load: Prioritizes critical images, impacting Largest Contentful Paint (LCP), with the
priorityprop by adding a preload<link>to the document head, accelerating load times.
Version compatibility for Leptos and Leptos Image
The table below shows the compatible versions of leptos_image for each leptos version. Ensure you are using compatible versions to avoid potential issues.
leptos version |
leptos_image version |
|---|---|
| 0.6.* | 0.2.* |
Installation
To add leptos_image to your project, use cargo:
Enable the SSR feature in your Cargo.toml:
[]
= [
"leptos_image/ssr",
# other dependencies...
]
= [
"leptos_image/hydrate",
# other dependencies...
]
Quick Start
This requires SSR + Leptos Axum integration
-
Provide Image Context: Initialize your Leptos application with
leptos_image::provide_image_contextto grant it read access to the image cache.use *; -
Axum State Configuration: Incorporate
ImageOptimizerinto your app's Axum state.// Composite App State with the optimizer and leptos options. -
Integrate with Router: Ensure your
ImageOptimizeris available during SSR of your Leptos App.- Add Image Cache Route: Use
image_cache_routeto serve cached images. - Add
ImageOptimizerto your App state. - Add
ImageOptimizerto Leptos Context: Provide the optimizer to Leptos context usingleptos_routes_with_context.
use *; use *; use *; async - Add Image Cache Route: Use
A full working example is available in the examples directory.
Now you can use the Image Component anywhere in your app!
This setup ensures your Leptos application is fully equipped to deliver optimized images, enhancing the performance and user experience of your web projects.