Leptos <Image/> (Batteries included)
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.
- Size Optimization: Resize images, and use modern
.webpformat for optimal size and quality. - Low-Quality Image Placeholders (LQIP): With this feature, the Image component embeds SVGs, extracted from the original images, into the initial SSR HTML. This placeholder is shown while the optimized version is loading.
- Faster Page Load: Prioritize key images, such as those contributing to the Largest Contentful Paint (LCP) with the
priorityprop. The component adds a preload<link>to the document head, improving load times and enhancing your site's performance.
Quick Start
REQUIRES SSR + AXUM
In the base of your App, wrap everything (including Router) in <ImageProvider/>
// In your main App.
use *;
use *;
use ;
Next go to your SSR Main Function in main.rs
Before you create your router, call the cache_app_images function with the project root. This will cache all the images in your app, and generate the LQIPs
use *;
let conf = get_configuration.await.unwrap;
let leptos_options = conf.leptos_options;
let root = leptos_options.site_root.clone;
use cache_app_images;
cache_app_images
.await
.expect;
Next add an endpoint to your router that serves the cached images. For now, the endpoint path must be /cache/image and is not configurable
```rust
use ;
let router = ...
router.route;
The final router should look something like this!
let router = new
.route
.leptos_routes
// Here's the new route!.
.route
.fallback
.with_state;
And that's it. You're all set to use the Image Component.
Caveats:
- Images will only be retrieved from routes that are non-dynamic (meaning not
api/post/:idin Route path). - Images can take a few seconds to optimize, meaning first startup of server will be slower.
- Actix Support is coming soon!